MD5Digest.cc
Go to the documentation of this file.
1 #include "cetlib/MD5Digest.h"
2 #include "cetlib/nybbler.h"
3 
4 #include <algorithm>
5 
6 namespace cet {
7  namespace {
8  MD5Result const&
9  invalidResult()
10  {
11  static const MD5Result val;
12  return val;
13  }
14  }
15 
16  //--------------------------------------------------------------------
17  //
18  // MD5Result and associated free functions
19  //
20 
21  void
23  {
24  val.bytes[0] = 0xd4;
25  val.bytes[1] = 0x1d;
26  val.bytes[2] = 0x8c;
27  val.bytes[3] = 0xd9;
28  val.bytes[4] = 0x8f;
29  val.bytes[5] = 0x00;
30  val.bytes[6] = 0xb2;
31  val.bytes[7] = 0x04;
32  val.bytes[8] = 0xe9;
33  val.bytes[9] = 0x80;
34  val.bytes[10] = 0x09;
35  val.bytes[11] = 0x98;
36  val.bytes[12] = 0xec;
37  val.bytes[13] = 0xf8;
38  val.bytes[14] = 0x42;
39  val.bytes[15] = 0x7e;
40  }
41 
43 
46  {
47  // Note: The code used to be this:
48  //
49  // std::ostringstream os;
50  // os << std::hex << std::setfill('0');
51  // for (size_t i = 0; i < sizeof(bytes); ++i)
52  // os << std::setw(2) << static_cast<int>(bytes[i]);
53  // return os.str();
54  //
55  // However profiling shows that this causes a measurable slowdown.
56  // The following code does the same thing, but is much faster.
57  constexpr char hex_bytes[] = "0123456789abcdef";
58  std::string ret;
59  ret.resize(sizeof(bytes) << 1);
60  for (size_t i = 0; i < sizeof(bytes); ++i) {
61  ret[i << 1] = hex_bytes[bytes[i] >> 4];
62  ret[(i << 1) + 1] = hex_bytes[bytes[i] & 0x0F];
63  }
64  return ret;
65  }
66 
69  {
70  // This is somewhat dangerous, because the conversion of 'unsigned
71  // char' to 'char' may be undefined if 'char' is a signed type
72  // (4.7p3 in the Standard).
73  const char* p = reinterpret_cast<const char*>(&bytes[0]);
74  return std::string(p, p + sizeof(bytes));
75  }
76 
77  void
79  {
80  switch (hexy.size()) {
81  case 0: {
82  set_to_default(*this);
83  } break;
84  case 32: {
85  std::string const chars = cet::nybbler(hexy).as_char();
86  std::string::const_iterator it = chars.begin();
87  for (size_t i = 0; i != 16; ++i)
88  bytes[i] = *it++;
89  } break;
90  default: {
91  throw "String of illegal length given to MD5Result::fromHexifiedString";
92  }
93  }
94  }
95 
96  bool
98  {
99  return (*this != invalidResult());
100  }
101 
102  bool
103  operator==(MD5Result const& a, MD5Result const& b)
104  {
105  return std::equal(a.bytes, a.bytes + sizeof(a.bytes), b.bytes);
106  }
107 
108  bool
109  operator<(MD5Result const& a, MD5Result const& b)
110  {
111  return std::lexicographical_compare(
112  a.bytes, a.bytes + sizeof(a.bytes), b.bytes, b.bytes + sizeof(b.bytes));
113  }
114 
115  //--------------------------------------------------------------------
116  //
117  // MD5Digest
118  //
119 
120  MD5Digest::MD5Digest() : context_() { MD5_Init(&context_); }
121 
123  {
124  MD5_Init(&context_);
125  this->append(s);
126  }
127 
128  void
130  {
131  using md5_byte_t = unsigned char;
132  md5_byte_t const* data = reinterpret_cast<md5_byte_t const*>(s.data());
133  MD5_Update(&context_, const_cast<md5_byte_t*>(data), s.size());
134  }
135 
136  MD5Result
138  {
139  MD5Result aDigest;
140  MD5_Final(aDigest.bytes, &context_);
141  return aDigest;
142  }
143 }
bool isValid() const
Definition: MD5Digest.cc:97
void append(std::string const &s)
Definition: MD5Digest.cc:129
MD5Result digest() const
Definition: MD5Digest.cc:137
std::string string
Definition: nybbler.cc:12
std::string toString() const
Definition: MD5Digest.cc:45
intermediate_table::const_iterator const_iterator
MD5_CTX context_
Definition: MD5Digest.h:67
void set_to_default(MD5Result &val)
Definition: MD5Digest.cc:22
void fromHexifiedString(std::string const &s)
Definition: MD5Digest.cc:78
constexpr bool operator<(exempt_ptr< E >, exempt_ptr< E >)
Definition: exempt_ptr.h:256
const double a
p
Definition: test.py:223
constexpr bool operator==(exempt_ptr< E >, exempt_ptr< E >) noexcept
Definition: exempt_ptr.h:211
unsigned char bytes[16]
Definition: MD5Digest.h:24
string as_char() const
Definition: nybbler.cc:31
static bool * b
Definition: config.cpp:1043
std::string compactForm() const
Definition: MD5Digest.cc:68
static QCString * s
Definition: config.cpp:1042