LogCabin
Core/Checksum.h
Go to the documentation of this file.
00001 /* Copyright (c) 2012 Stanford University
00002  *
00003  * Permission to use, copy, modify, and distribute this software for any
00004  * purpose with or without fee is hereby granted, provided that the above
00005  * copyright notice and this permission notice appear in all copies.
00006  *
00007  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIM ALL WARRANTIES
00008  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00009  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL AUTHORS BE LIABLE FOR
00010  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00011  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00012  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00013  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00014  */
00015 
00016 /**
00017  * \file
00018  * Contains checksum utilities.
00019  */
00020 
00021 #include <cinttypes>
00022 #include <string>
00023 #include <vector>
00024 
00025 #ifndef LOGCABIN_CORE_CHECKSUM_H
00026 #define LOGCABIN_CORE_CHECKSUM_H
00027 
00028 namespace LogCabin {
00029 namespace Core {
00030 namespace Checksum {
00031 
00032 /**
00033  * The number of bytes any checksum algorithm name and digest may occupy.
00034  */
00035 enum { MAX_LENGTH = 256 };
00036 
00037 /**
00038  * Returns the names of all available hash functions.
00039  * You can use any of these names in the calculate function.
00040  * \return
00041  *      The names in sorted order.
00042  */
00043 std::vector<std::string>
00044 listAlgorithms();
00045 
00046 /**
00047  * Calculate the checksum for a chunk of data.
00048  * \param algorithm
00049  *      The checksum algorithm to use. Most crypto++ hash algorithms may be
00050  *      used, for example, "SHA-256". This function will PANIC if the algorithm
00051  *      is not available (see listAlgorithms()).
00052  * \param data
00053  *      The first byte of the data.
00054  * \param dataLength
00055  *      The number of bytes in the data.
00056  * \param[out] output
00057  *      The result of the hash function will be placed here.
00058  *      This will be a null-terminated, printable C-string.
00059  * \return
00060  *      The number of valid characters in 'output', including the null
00061  *      terminator. This is guaranteed to be greater than 1.
00062  */
00063 uint32_t
00064 calculate(const char* algorithm,
00065           const void* data, uint64_t dataLength,
00066           char output[MAX_LENGTH]);
00067 
00068 /**
00069  * Calculate the checksum for a chunk of data.
00070  * \param algorithm
00071  *      The checksum algorithm to use. Most crypto++ hash algorithms may be
00072  *      used, for example, "SHA-256". This function will PANIC if the algorithm
00073  *      is not available (see listAlgorithms()).
00074  * \param data
00075  *      An list of (pointer, length) pairs describing what to checksum.
00076  * \param[out] output
00077  *      The result of the hash function will be placed here.
00078  *      This will be a null-terminated, printable C-string.
00079  * \return
00080  *      The number of valid characters in 'output', including the null
00081  *      terminator. This is guaranteed to be greater than 1.
00082  */
00083 uint32_t
00084 calculate(const char* algorithm,
00085           std::initializer_list<std::pair<const void*, uint64_t>> data,
00086           char output[MAX_LENGTH]);
00087 
00088 /**
00089  * Check to see if the checksum format looks plausible.
00090  * \param checksum
00091  *      What might be a checksum as generated by calculate().
00092  * \param maxChecksumLength
00093  *      The total number of bytes in the buffer containing checksum, including
00094  *      the null terminator (if any).
00095  * \return
00096  *      If 'checksum' looks plausible and is safe to pass to verify(), returns
00097  *      the number of bytes in 'checksum' up to and including its null
00098  *      terminator. If 'checksum' looks malformed, this returns 0, which the
00099  *      caller should treat as a corrupt checksum.
00100  */
00101 uint32_t
00102 length(const char* checksum,
00103        uint32_t maxChecksumLength);
00104 
00105 /**
00106  * Verify data against an existing checksum.
00107  * \param checksum
00108  *      A null-terminated checksum returned from calculate() or whose format
00109  *      has been verified by length(). The algorithm name in this checksum will
00110  *      determine which checksumming algorithm to use.
00111  * \param data
00112  *      The first byte of the data.
00113  * \param dataLength
00114  *      The number of bytes in the data.
00115  * \return
00116  *      An empty string if the checksum matches, or an error string if the
00117  *      checksum does not match or the checksum algorithm is not available.
00118  */
00119 std::string
00120 verify(const char* checksum,
00121        const void* data, uint64_t dataLength);
00122 
00123 /**
00124  * Verify data against an existing checksum.
00125  * \param checksum
00126  *      A null-terminated checksum returned from calculate() or whose format
00127  *      has been verified by length(). The algorithm name in this checksum will
00128  *      determine which checksumming algorithm to use.
00129  * \param data
00130  *      An list of (pointer, length) pairs describing what to checksum.
00131  * \return
00132  *      An empty string if the checksum matches, or an error string if the
00133  *      checksum does not match or the checksum algorithm is not available.
00134  */
00135 std::string
00136 verify(const char* checksum,
00137        std::initializer_list<std::pair<const void*, uint64_t>> data);
00138 
00139 } // namespace LogCabin::Core::Checksum
00140 } // namespace LogCabin::Core
00141 } // namespace LogCabin
00142 
00143 #endif /* LOGCABIN_CORE_CHECKSUM_H */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines