Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-25 02:29:37

0001 #ifndef FWCore_Utilities_Digest_h
0002 #define FWCore_Utilities_Digest_h
0003 
0004 #include "md5.h"
0005 
0006 #include <iosfwd>
0007 #include <string>
0008 #include <string_view>
0009 #include <array>
0010 
0011 namespace cms {
0012 
0013   struct MD5Result {
0014     // The default-constructed MD5Result is invalid; all others are
0015     // valid. The MD5 digest of the empty string is the value of the
0016     // default-constructed MD5Result.
0017     MD5Result();
0018 
0019     // This is the MD5 digest.
0020     std::array<unsigned char, 16> bytes;
0021 
0022     // Convert the digest to a printable string (the 'hexdigest')
0023     std::string toString() const;
0024 
0025     // The MD5 digest (not hexdigest) in string form
0026     // 'std::basic_string<char>', rather than
0027     // 'unsigned char [16]'
0028     std::string compactForm() const;
0029 
0030     // Set our data from the given hexdigest string.
0031     void fromHexifiedString(std::string_view);
0032 
0033     bool isValid() const;
0034   };
0035 
0036   bool operator==(MD5Result const& a, MD5Result const& b);
0037   bool operator<(MD5Result const& a, MD5Result const& b);
0038 
0039   inline bool operator!=(MD5Result const& a, MD5Result const& b) { return !(a == b); }
0040 
0041   inline std::ostream& operator<<(std::ostream& os, MD5Result const& r) {
0042     os << r.toString();
0043     return os;
0044   }
0045 
0046   // Digest creates an MD5 digest of the given string. The digest can
0047   // be updated by using 'append'.
0048   class Digest {
0049   public:
0050     Digest();
0051     explicit Digest(std::string const& s);
0052     explicit Digest(std::string_view);
0053     explicit Digest(const char*);
0054 
0055     void append(std::string const& s);
0056     void append(const char* data, size_t size);
0057     void append(std::string_view v);
0058 
0059     MD5Result digest();
0060 
0061   private:
0062     md5_state_t state_;
0063   };
0064 }  // namespace cms
0065 
0066 #endif