RSTT: rstt/SLBM/include/MD50.h Source File

RSTT 3.1.0 Regional Seismic Travel Time

MD50.h
Go to the documentation of this file.
1 //- ****************************************************************************
2 //-
3 //- Copyright 2009 National Technology & Engineering Solutions of Sandia, LLC
4 //- (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
5 //- Government retains certain rights in this software.
6 //-
7 //- BSD Open Source License
8 //- All rights reserved.
9 //-
10 //- Redistribution and use in source and binary forms, with or without
11 //- modification, are permitted provided that the following conditions are met:
12 //-
13 //- 1. Redistributions of source code must retain the above copyright notice,
14 //- this list of conditions and the following disclaimer.
15 //-
16 //- 2. Redistributions in binary form must reproduce the above copyright
17 //- notice, this list of conditions and the following disclaimer in the
18 //- documentation and/or other materials provided with the distribution.
19 //-
20 //- 3. Neither the name of the copyright holder nor the names of its
21 //- contributors may be used to endorse or promote products derived from
22 //- this software without specific prior written permission.
23 //-
24 //- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 //- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 //- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 //- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
28 //- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 //- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 //- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 //- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 //- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 //- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 //- POSSIBILITY OF SUCH DAMAGE.
35 //-
36 //- ****************************************************************************
37
38 #ifndef MD50_H
39 #define MD50_H
40
41 // **** _SYSTEM INCLUDES_ ******************************************************
42
43 #include <string>
44
45 // **** _LOCAL INCLUDES_ *******************************************************
46
47 #include "UtilGlobals.h"
48
49 using namespace std;
50 // use standard library objects
51
52 // **** _BEGIN UTIL NAMESPACE_ *************************************************
53
54 namespace util {
55
56 typedef unsigned int uint32;
57 typedef unsigned char uchar;
58 typedef const unsigned char cuchar;
59
60 // **** _CLASS DESCRIPTION_ ****************************************************
61 //
66 //
79 //
80 //******************************************************************************
81 class UTIL_EXP_IMP MD50
82 {
83 public:
84
85 // **** _PUBLIC LIFECYCLES_ ************************************************
86
87 MD50();
88 //- Default constructor.
89
90 MD50(const MD50& md50);
91 //- Copy constructor.
92
93 virtual ~MD50();
94 //- Virtual destructor.
95
96 // **** _PUBLIC OPERATORS_ *************************************************
97
98 MD50& operator=(const MD50& md50);
99 //- Assignment operator.
100
101 // **** _PUBLIC METHODS_ ***************************************************
102
103 const string& getMD5HashHex(uchar const* data, int sze);
104 void getMD5HashHex(uchar const* data, int sze, string& hhstr)
105 {
106 // create the md5 binary hash string in hashbin and convert to hexadecimal
107 // in hashkey
108
109 getMD5HashBin(data, sze);
110 getMD5HashHex(hhstr);
111 }
112 void getMD5HashHex(const string& sin, string& hhstr);
113 //- Performs the md5 conversion from the input data into a 32 byte
114 //- hexadecimal string and returns the string in hhstr (the last two
115 //- functions).
116
117 const string& getMD5HashHex() const;
118 void getMD5HashHex(string& s) const;
119 //- Converts the input MD5 16 byte binary hash key into a 32 byte
120 //- hexadecimal hash key. These functions assume the MD5 conversion has
121 //- already occurred.
122
123 cuchar const* getMD5HashBin(uchar const* data, int sze);
124 //- Performs the md5 conversion from the input data into the internal 16
125 //- byte binary hash key and returns the key.
126
127 cuchar const* getMD5HashBin();
128 //- Returns the 16 byte binary hash key. This function assumes the MD5
129 //- conversion has already occurred.
130
131 static int hexStringSize();
132 static int hexSize();
133 static int binSize();
134 //- functions to return the hex string (32 + 4), hex (32), and bin (16)
135 //- size of the MD5 hash key.
136
137 void setDefaultEndian();
138 void setBigEndian();
139 void setLittleEndian();
140 bool getByteReverse() const;
141 //- Sets/gets the big-endian flag. This flag is generally set to true for
142 //- big-endian machines.
143
144 static bool isBigEndian();
145 //- Static function that returns true if the machine type is big-endian.
146
147 private:
148
149 // **** _PRIVATE METHODS_ **************************************************
150
151 void init();
152 //- Reinitializes this MD50 object
153
154 void update(uchar const *data, unsigned len);
155 //- Loops through all bytes in the input buffer (md5BinKey) 64 at a time
156 //- upto but short of len. The remaining bytes are processed by final.
157
158 void final();
159 //- Performs the final required padding of the md5 algorithm.
160
161 void transform();
162 //- The core of the MD5 algorithm. This function alters an existing MD5
163 //- hash to reflect the addition of 16 longwords of new data. update()
164 //- blocks the data and converts bytes into longwords for this routine.
165
166 void byteReverse(uchar* buf, unsigned n);
167 //- Reverses big-endian to little endian. Note: this code is harmless on
168 //- little-endian machines (Really?).
169
170 // **** _PRIVATE DATA_ *****************************************************
171
173 bool md5BigEndian;
174
176 uint32 md5BinKey[4];
177
179 uint32 md5Bits[2];
180
185 uchar md5Buf[64];
186
189 uint32 const* md5BufRef;
190
191 };
192
193 // **** _FUNCTION DESCRIPTION_ *************************************************
194 //
196 //
197 //******************************************************************************
198 inline int MD50::hexStringSize()
199 {
200 return hexSize() + sizeof(int);
201 }
202
203 // **** _FUNCTION DESCRIPTION_ *************************************************
204 //
206 //
207 //******************************************************************************
208 inline int MD50::hexSize()
209 {
210 return 32;
211 }
212
213 // **** _FUNCTION DESCRIPTION_ *************************************************
214 //
216 //
217 //******************************************************************************
218 inline int MD50::binSize()
219 {
220 return 16;
221 }
222
223 // **** _FUNCTION DESCRIPTION_ *************************************************
224 //
226 //
227 //******************************************************************************
228 inline void MD50::setDefaultEndian()
229 {
230 if (isBigEndian())
231 md5BigEndian = true;
232 else
233 md5BigEndian = false;
234 }
235
236 // **** _FUNCTION DESCRIPTION_ *************************************************
237 //
239 //
240 //******************************************************************************
241 inline void MD50::setBigEndian()
242 {
243 md5BigEndian = true;
244 }
245
246 // **** _FUNCTION DESCRIPTION_ *************************************************
247 //
249 //
250 //******************************************************************************
251 inline void MD50::setLittleEndian()
252 {
253 md5BigEndian = false;
254 }
255
256 // **** _FUNCTION DESCRIPTION_ *************************************************
257 //
259 //
260 //******************************************************************************
261 inline bool MD50::getByteReverse() const
262 {
263 return md5BigEndian;
264 }
265
266 // **** _FUNCTION DESCRIPTION_ *************************************************
267 //
272 //
273 //******************************************************************************
274 inline const string& MD50::getMD5HashHex(uchar const* data, int sze)
275 {
276 // create the md5 binary hash string in hashbin and convert to hexadecimal
277 // in hashkey
278
279 getMD5HashBin(data, sze);
280 return getMD5HashHex();
281 }
282
283 // **** _FUNCTION DESCRIPTION_ *************************************************
284 //
289 //
290 //******************************************************************************
291 //inline void MD50::getMD5HashHex(uchar const* data, int sze, string& hhstr)
292 //{
293 // // create the md5 binary hash string in hashbin and convert to hexadecimal
294 // // in hashkey
295 //
296 // getMD5HashBin(data, sze);
297 // getMD5HashHex(hhstr);
298 //}
299
300 // **** _FUNCTION DESCRIPTION_ *************************************************
301 //
305 //
306 //******************************************************************************
307 inline void MD50::getMD5HashHex(const string& sin, string& hhstr)
308 {
309 // create the md5 binary hash string in hashbin and converts to hexadecimal
310 // in hashkey
311
312 uchar const* data = (uchar const*) &sin[0];
313 getMD5HashBin(data, sin.size());
314 getMD5HashHex(hhstr);
315 }
316
317 // **** _FUNCTION DESCRIPTION_ *************************************************
318 //
323 //
324 //******************************************************************************
325 inline cuchar const* MD50::getMD5HashBin(uchar const* data, int sze)
326 {
327 init();
328 update(data, sze);
329 final();
330 return getMD5HashBin();
331 }
332
333 // **** _FUNCTION DESCRIPTION_ *************************************************
334 //
337 //
338 //******************************************************************************
339 inline cuchar const* MD50::getMD5HashBin()
340 {
341 return (cuchar* const) md5BinKey;
342 }
343
344 // **** _FUNCTION DESCRIPTION_ *************************************************
345 //
349 //
350 //******************************************************************************
351 template<class f>
352 class md5step
353 {
354 public:
355
357 void operator() (uint32& w, uint32 x, uint32 y, uint32 z,
358 uint32 data, uint32 s)
359 {
360 static f fnc;
361 w += fnc(x, y, z) + data;
362 w = w << s | w >> (32 - s);
363 w += x;
364 };
365 };
366
367 // **** _FUNCTION DESCRIPTION_ *************************************************
368 //
371 //
372 //******************************************************************************
373 class f1
374 {
375 public:
376
378 uint32 operator() (uint32 x, uint32 y, uint32 z)
379 {
380 return z ^ (x & (y ^ z));
381 };
382 };
383
384 // **** _FUNCTION DESCRIPTION_ *************************************************
385 //
387 //
388 //******************************************************************************
389 class f2
390 {
391 public:
392
394 uint32 operator() (uint32 x, uint32 y, uint32 z)
395 {
396 //5return f1(z, x, y);
397 return y ^ (z & (x ^ y));
398 };
399 };
400
401 // **** _FUNCTION DESCRIPTION_ *************************************************
402 //
404 //
405 //******************************************************************************
406 class f3
407 {
408 public:
409
411 uint32 operator() (uint32 x, uint32 y, uint32 z)
412 {
413 return x ^ y ^ z;
414 };
415 };
416
417 // **** _FUNCTION DESCRIPTION_ *************************************************
418 //
420 //
421 //******************************************************************************
422 class f4
423 {
424 public:
425
427 uint32 operator() (uint32 x, uint32 y, uint32 z)
428 {
429 return y ^ (x | ~z);
430 };
431 };
432
433 } // end namespace util
434
435 #endif // !MD50_H
util::MD50::getMD5HashHex
void getMD5HashHex(uchar const *data, int sze, string &hhstr)
Definition: MD50.h:104
UTIL_EXP_IMP
#define UTIL_EXP_IMP
Definition: UtilGlobals.h:65
util::f3
Third MD5 algorithmic operation function object.
Definition: MD50.h:407
util::MD50::MD50
MD50(const MD50 &md50)
util::MD50::MD50
MD50()
util::MD50::operator=
MD50 & operator=(const MD50 &md50)
util::MD50::~MD50
virtual ~MD50()
util::cuchar
const unsigned char cuchar
Definition: MD50.h:58
util::md5step
Primary MD5 algorithmic step function object. The function object is templated (class f) off of the f...
Definition: MD50.h:353
util::MD50
This code implements the MD5 message-digest algorithm. The algorithm is due to Ron Rivest....
Definition: MD50.h:82
util::f2
Second MD5 algorithmic operation function object.
Definition: MD50.h:390
util::uint32
unsigned int uint32
Definition: MD50.h:56
UtilGlobals.h
util::f4
Fourth MD5 algorithmic operation function object.
Definition: MD50.h:423
util::MD50::getMD5HashHex
void getMD5HashHex(string &s) const
util::f1
First MD5 algorithmic operation function object. Optimized from x & y | ~x & z.
Definition: MD50.h:374
util::MD50::isBigEndian
static bool isBigEndian()
util::uchar
unsigned char uchar
Definition: MD50.h:57
util
Definition: Brents.h:54
util::MD50::getMD5HashHex
const string & getMD5HashHex() const