CMS 3D CMS Logo

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