CMS 3D CMS Logo

Digest.cc
Go to the documentation of this file.
1 #include <iomanip>
2 #include <sstream>
3 
6 
7 namespace cms {
8  namespace {
9  MD5Result const& invalidResult() {
10  static const MD5Result val;
11  return val;
12  }
13 
14  char unhexify(char hexed) {
15  switch (hexed) {
16  case '0':
17  case '1':
18  case '2':
19  case '3':
20  case '4':
21  case '5':
22  case '6':
23  case '7':
24  case '8':
25  case '9':
26  return hexed - '0';
27  case 'a':
28  case 'b':
29  case 'c':
30  case 'd':
31  case 'e':
32  case 'f':
33  return hexed - 'a' + 10;
34  case 'A':
35  case 'B':
36  case 'C':
37  case 'D':
38  case 'E':
39  case 'F':
40  return hexed - 'A' + 10;
41  default:
42  throw edm::Exception(edm::errors::LogicError) << "Non-hex character in Hash "
43  << "Please report this to the core framework developers";
44  }
45  // We never get here; return put in place to calm the compiler's
46  // anxieties.
47  return '\0';
48  }
49  } // namespace
50 
51  //--------------------------------------------------------------------
52  //
53  // MD5Result and associated free functions
54  //
55 
57  val.bytes[0] = 0xd4;
58  val.bytes[1] = 0x1d;
59  val.bytes[2] = 0x8c;
60  val.bytes[3] = 0xd9;
61  val.bytes[4] = 0x8f;
62  val.bytes[5] = 0x00;
63  val.bytes[6] = 0xb2;
64  val.bytes[7] = 0x04;
65  val.bytes[8] = 0xe9;
66  val.bytes[9] = 0x80;
67  val.bytes[10] = 0x09;
68  val.bytes[11] = 0x98;
69  val.bytes[12] = 0xec;
70  val.bytes[13] = 0xf8;
71  val.bytes[14] = 0x42;
72  val.bytes[15] = 0x7e;
73  }
74 
76 
77  static const char* s_hexValues =
78  "000102030405060708090a0b0c0d0e0f"
79  "101112131415161718191a1b1c1d1e1f"
80  "202122232425262728292a2b2c2d2e2f"
81  "303132333435363738393a3b3c3d3e3f"
82  "404142434445464748494a4b4c4d4e4f"
83  "505152535455565758595a5b5c5d5e5f"
84  "606162636465666768696a6b6c6d6e6f"
85  "707172737475767778797a7b7c7d7e7f"
86  "808182838485868788898a8b8c8d8e8f"
87  "909192939495969798999a9b9c9d9e9f"
88  "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf"
89  "b0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
90  "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf"
91  "d0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
92  "e0e1e2e3e4e5e6e7e8e9eaebecedeeef"
93  "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff";
94 
96  char buf[16 * 2];
97  char* pBuf = buf;
98  for (unsigned int i = 0; i < sizeof(bytes); ++i) {
99  const char* p = s_hexValues + 2 * bytes[i];
100  *pBuf = *p;
101  ++pBuf;
102  ++p;
103  *pBuf = *p;
104  ++pBuf;
105  }
106  return std::string(buf, sizeof(buf));
107  }
108 
110  // This is somewhat dangerous, because the conversion of 'unsigned
111  // char' to 'char' may be undefined if 'char' is a signed type
112  // (4.7p3 in the Standard).
113  const char* p = reinterpret_cast<const char*>(&bytes[0]);
114  return std::string(p, p + sizeof(bytes));
115  }
116 
118  switch (hexy.size()) {
119  case 0: {
120  set_to_default(*this);
121  } break;
122  case 32: {
123  std::string::const_iterator it = hexy.begin();
124  for (size_t i = 0; i != 16; ++i) {
125  // first nybble
126  bytes[i] = (unhexify(*it++) << 4);
127  // second nybble
128  bytes[i] += (unhexify(*it++));
129  }
130  } break;
131  default: {
132  // Not really sure of what sort of exception to throw...
134  << "String of illegal length: " << hexy.size() << " given to MD5Result::fromHexifiedString";
135  }
136  }
137  }
138 
139  bool MD5Result::isValid() const { return (*this != invalidResult()); }
140 
141  bool operator==(MD5Result const& a, MD5Result const& b) {
142  return std::equal(a.bytes, a.bytes + sizeof(a.bytes), b.bytes);
143  }
144 
145  bool operator<(MD5Result const& a, MD5Result const& b) {
146  return std::lexicographical_compare(a.bytes, a.bytes + sizeof(a.bytes), b.bytes, b.bytes + sizeof(b.bytes));
147  }
148 
149  //--------------------------------------------------------------------
150  //
151  // Digest
152  //
153 
154  Digest::Digest() : state_() { md5_init(&state_); }
155 
156  Digest::Digest(std::string const& s) : state_() {
157  md5_init(&state_);
158  this->append(s);
159  }
160 
161  void Digest::append(std::string const& s) {
162  const md5_byte_t* data = reinterpret_cast<const md5_byte_t*>(s.data());
163  md5_append(&state_, const_cast<md5_byte_t*>(data), s.size());
164  }
165 
166  void Digest::append(const char* s, size_t size) {
167  const md5_byte_t* data = reinterpret_cast<const md5_byte_t*>(s);
168  md5_append(&state_, const_cast<md5_byte_t*>(data), size);
169  }
170 
172  MD5Result aDigest;
173  md5_finish(&state_, aDigest.bytes);
174  return aDigest;
175  }
176 } // namespace cms
cms::Digest::append
void append(std::string const &s)
Definition: Digest.cc:161
cms::MD5Result::isValid
bool isValid() const
Definition: Digest.cc:139
mps_fire.i
i
Definition: mps_fire.py:428
cms::Digest::digest
MD5Result digest()
Definition: Digest.cc:171
edm::errors::LogicError
Definition: EDMException.h:37
cms::MD5Result::fromHexifiedString
void fromHexifiedString(std::string const &s)
Definition: Digest.cc:117
cms::s_hexValues
static const char * s_hexValues
Definition: Digest.cc:77
cms::MD5Result::bytes
unsigned char bytes[16]
Definition: Digest.h:18
EDMException.h
alignCSCRings.s
s
Definition: alignCSCRings.py:92
cms::operator==
bool operator==(MD5Result const &a, MD5Result const &b)
Definition: Digest.cc:141
b
double b
Definition: hdecay.h:118
cms::Digest::state_
md5_state_t state_
Definition: Digest.h:57
cms::MD5Result
Definition: Digest.h:11
a
double a
Definition: hdecay.h:119
AlCaHLTBitMon_ParallelJobs.p
def p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
cms::set_to_default
void set_to_default(MD5Result &val)
Definition: Digest.cc:56
visDQMUpload.buf
buf
Definition: visDQMUpload.py:160
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
Digest.h
heppy_batch.val
val
Definition: heppy_batch.py:351
cms::operator<
bool operator<(MD5Result const &a, MD5Result const &b)
Definition: Digest.cc:145
cms::Digest::Digest
Digest()
Definition: Digest.cc:154
Exception
Definition: hltDiff.cc:245
cms::MD5Result::compactForm
std::string compactForm() const
Definition: Digest.cc:109
cms::MD5Result::MD5Result
MD5Result()
Definition: Digest.cc:75
data
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
cms::MD5Result::toString
std::string toString() const
Definition: Digest.cc:95
cond::serialization::equal
bool equal(const T &first, const T &second)
Definition: Equal.h:32
cms
Namespace of DDCMS conversion namespace.
Definition: ProducerAnalyzer.cc:21
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443