#ifndef _utl_Md5Sum_h_ #define _utl_Md5Sum_h_ #include #include #include #include namespace utl { /** \class Md5Sum Md5Sum.h "utl/Md5Sum.h" \brief Class to compute MD5 checksum Based on the RSA C code, wrapped in an OO fashion. \note See MD5 wiki for the reference \author Lukas Nellen \date 03 Dec 2008 \date 20 Aug 2009 DV \ingroup utl */ class Md5Sum { public: typedef const unsigned char* Iterator; typedef std::pair IteratorPair; static const unsigned int kBufferSize = 64; static const unsigned int kDigestSize = 16; class WrongStageException : public AugerException { public: /// Construct wrong stage exception with error message WrongStageException(const std::string& message = std::string()) : AugerException(message) { } /// Retrieve verbose exception name virtual std::string GetExceptionName() const { return "Invalid processing stage exception"; } }; Md5Sum() { Clear(); } Md5Sum(const std::string& data) { Clear(); AddData(data); } void AddDataRange(const unsigned char* const begin, const unsigned char* const end); void AddDataRange(const char* const begin, const char* const end) { AddDataRange((const unsigned char*)begin, (const unsigned char*)end); } /// Process the data in a string void AddData(const std::string& s) { const char* const p = s.c_str(); AddDataRange(p, p + s.length()); } void AddData(const char* const s) { const char* p; for (p = s; *p; ++p) { } AddDataRange(s, p); } IteratorPair GetRawDigest(); std::string GetHexDigest(); bool IsFinalized() const { return fFinalized; } /// Set up internal state void Clear(); private: void ProcessBuffer(); unsigned int BufferIndex() const { return (fCountLow >> 3) % kBufferSize; } bool fFinalized; /// internal state (A), 4 of 16 bytes, 64 bit total unsigned int fStateA; /// internal state (B), 4 of 16 bytes, 64 bit total unsigned int fStateB; /// internal state (C), 4 of 16 bytes, 64 bit total unsigned int fStateC; /// internal state (D), 4 of 16 bytes, 64 bit total unsigned int fStateD; /// Number of bits, mod 2^64 (lsb first) unsigned int fCountLow; unsigned int fCountHigh; /// input buffer unsigned char fBuffer[kBufferSize]; /// buffer for final digest unsigned char fDigest[kDigestSize]; }; } #endif