|
pred@2142
|
1 |
// MD5.CC - source code for the C++/object oriented translation and |
|
pred@2142
|
2 |
// modification of MD5. |
|
pred@2142
|
3 |
|
|
pred@2142
|
4 |
// Translation and modification (c) 1995 by Mordechai T. Abzug |
|
pred@2142
|
5 |
|
|
pred@2142
|
6 |
// This translation/ modification is provided "as is," without express or |
|
pred@2142
|
7 |
// implied warranty of any kind. |
|
pred@2142
|
8 |
|
|
pred@2142
|
9 |
// The translator/ modifier does not claim (1) that MD5 will do what you think |
|
pred@2142
|
10 |
// it does; (2) that this translation/ modification is accurate; or (3) that |
|
pred@2142
|
11 |
// this software is "merchantible." (Language for this disclaimer partially |
|
pred@2142
|
12 |
// copied from the disclaimer below). |
|
pred@2142
|
13 |
|
|
pred@2142
|
14 |
/* based on: |
|
pred@2142
|
15 |
|
|
pred@2142
|
16 |
MD5.H - header file for MD5C.C |
|
pred@2142
|
17 |
MDDRIVER.C - test driver for MD2, MD4 and MD5 |
|
pred@2142
|
18 |
|
|
pred@2142
|
19 |
Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All |
|
pred@2142
|
20 |
rights reserved. |
|
pred@2142
|
21 |
|
|
pred@2142
|
22 |
License to copy and use this software is granted provided that it |
|
pred@2142
|
23 |
is identified as the "RSA Data Security, Inc. MD5 Message-Digest |
|
pred@2142
|
24 |
Algorithm" in all material mentioning or referencing this software |
|
pred@2142
|
25 |
or this function. |
|
pred@2142
|
26 |
|
|
pred@2142
|
27 |
License is also granted to make and use derivative works provided |
|
pred@2142
|
28 |
that such works are identified as "derived from the RSA Data |
|
pred@2142
|
29 |
Security, Inc. MD5 Message-Digest Algorithm" in all material |
|
pred@2142
|
30 |
mentioning or referencing the derived work. |
|
pred@2142
|
31 |
|
|
pred@2142
|
32 |
RSA Data Security, Inc. makes no representations concerning either |
|
pred@2142
|
33 |
the merchantability of this software or the suitability of this |
|
pred@2142
|
34 |
software for any particular purpose. It is provided "as is" |
|
pred@2142
|
35 |
without express or implied warranty of any kind. |
|
pred@2142
|
36 |
|
|
pred@2142
|
37 |
These notices must be retained in any copies of any part of this |
|
pred@2142
|
38 |
documentation and/or software. |
|
pred@2142
|
39 |
|
|
pred@2142
|
40 |
*/ |
|
pred@2142
|
41 |
|
|
pred@2142
|
42 |
#include <stdio.h> |
|
pred@2142
|
43 |
//#include <fstream.h> |
|
pred@2142
|
44 |
//#include <iostream.h> |
|
pred@2142
|
45 |
|
|
pred@2142
|
46 |
class MD5 { |
|
pred@2142
|
47 |
|
|
pred@2142
|
48 |
public: |
|
pred@2142
|
49 |
// methods for controlled operation: |
|
pred@2142
|
50 |
MD5 (); // simple initializer |
|
pred@2142
|
51 |
void update (unsigned char *input, unsigned int input_length); |
|
pred@2142
|
52 |
void update (FILE *file); |
|
pred@2142
|
53 |
void finalize (); |
|
pred@2142
|
54 |
|
|
pred@2142
|
55 |
// constructors for special circumstances. All these constructors finalize |
|
pred@2142
|
56 |
// the MD5 context. |
|
pred@2142
|
57 |
MD5 (unsigned char *string); // digest string, finalize |
|
pred@2142
|
58 |
MD5 (FILE *file); // digest file, close, finalize |
|
pred@2142
|
59 |
|
|
pred@2142
|
60 |
// methods to acquire finalized result |
|
pred@2142
|
61 |
unsigned char *raw_digest (); // digest as a 16-byte binary array |
|
pred@2142
|
62 |
unsigned char *raw_digest(unsigned char buffer[16]); |
|
pred@2142
|
63 |
char * hex_digest (); // digest as a 33-byte ascii-hex string |
|
pred@2142
|
64 |
char * hex_digest (char buffer[33]); //same as above, passing buffer |
|
pred@2142
|
65 |
|
|
pred@2142
|
66 |
|
|
pred@2142
|
67 |
|
|
pred@2142
|
68 |
private: |
|
pred@2142
|
69 |
|
|
pred@2142
|
70 |
// first, some types: |
|
pred@2142
|
71 |
typedef unsigned int uint4; // assumes integer is 4 words long |
|
pred@2142
|
72 |
typedef unsigned short int uint2; // assumes short integer is 2 words long |
|
pred@2142
|
73 |
typedef unsigned char uint1; // assumes char is 1 word long |
|
pred@2142
|
74 |
|
|
pred@2142
|
75 |
// next, the private data: |
|
pred@2142
|
76 |
uint4 state[4]; |
|
pred@2142
|
77 |
uint4 count[2]; // number of *bits*, mod 2^64 |
|
pred@2142
|
78 |
uint1 buffer[64]; // input buffer |
|
pred@2142
|
79 |
uint1 digest[16]; |
|
pred@2142
|
80 |
uint1 finalized; |
|
pred@2142
|
81 |
|
|
pred@2142
|
82 |
// last, the private methods, mostly static: |
|
pred@2142
|
83 |
void init (); // called by all constructors |
|
pred@2142
|
84 |
void transform (uint1 *buffer); // does the real update work. Note |
|
pred@2142
|
85 |
// that length is implied to be 64. |
|
pred@2142
|
86 |
|
|
pred@2142
|
87 |
static void encode (uint1 *dest, uint4 *src, uint4 length); |
|
pred@2142
|
88 |
static void decode (uint4 *dest, uint1 *src, uint4 length); |
|
pred@2142
|
89 |
static void memcpy (uint1 *dest, uint1 *src, uint4 length); |
|
pred@2142
|
90 |
static void memset (uint1 *start, uint1 val, uint4 length); |
|
pred@2142
|
91 |
|
|
pred@2142
|
92 |
static inline uint4 rotate_left (uint4 x, uint4 n); |
|
pred@2142
|
93 |
static inline uint4 F (uint4 x, uint4 y, uint4 z); |
|
pred@2142
|
94 |
static inline uint4 G (uint4 x, uint4 y, uint4 z); |
|
pred@2142
|
95 |
static inline uint4 H (uint4 x, uint4 y, uint4 z); |
|
pred@2142
|
96 |
static inline uint4 I (uint4 x, uint4 y, uint4 z); |
|
pred@2142
|
97 |
static inline void FF (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, |
|
pred@2142
|
98 |
uint4 s, uint4 ac); |
|
pred@2142
|
99 |
static inline void GG (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, |
|
pred@2142
|
100 |
uint4 s, uint4 ac); |
|
pred@2142
|
101 |
static inline void HH (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, |
|
pred@2142
|
102 |
uint4 s, uint4 ac); |
|
pred@2142
|
103 |
static inline void II (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, |
|
pred@2142
|
104 |
uint4 s, uint4 ac); |
|
pred@2142
|
105 |
|
|
pred@2142
|
106 |
}; |