1 |
|
#include "MD5.h"
|
2 |
|
|
3 |
1 |
#include <cstring>
|
4 |
2 |
|
5 |
3 |
using namespace std;
|
6 |
4 |
|
7 |
|
/******************************************************************************/
|
8 |
|
|
9 |
5 |
/*
|
10 |
|
MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
|
11 |
|
|
12 |
|
Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All rights
|
13 |
|
reserved.
|
14 |
|
|
15 |
|
License to copy and use this software is granted provided that it is
|
16 |
|
identified as the "RSA Data Security, Inc. MD5 Message-Digest Algorithm" in
|
17 |
|
all material mentioning or referencing this software or this function.
|
18 |
|
|
19 |
|
License is also granted to make and use derivative works provided that such
|
20 |
|
works are identified as "derived from the RSA Data Security, Inc. MD5
|
21 |
|
Message-Digest Algorithm" in all material mentioning or referencing the
|
22 |
|
derived work.
|
23 |
|
|
24 |
|
RSA Data Security, Inc. makes no representations concerning either the
|
25 |
|
merchantability of this software or the suitability of this software for
|
26 |
|
any particular purpose. It is provided "as is" without express or implied
|
27 |
|
warranty of any kind.
|
28 |
|
|
29 |
|
These notices must be retained in any copies of any part of this
|
30 |
|
documentation and/or software.
|
31 |
|
*/
|
32 |
|
|
33 |
|
/******************************************************************************/
|
34 |
|
|
35 |
|
/* Constants for MD5Transform routine. */
|
36 |
|
|
37 |
|
#define S11 7
|
38 |
|
#define S12 12
|
39 |
|
#define S13 17
|
40 |
|
#define S14 22
|
41 |
|
#define S21 5
|
42 |
|
#define S22 9
|
43 |
|
#define S23 14
|
44 |
|
#define S24 20
|
45 |
|
#define S31 4
|
46 |
|
#define S32 11
|
47 |
|
#define S33 16
|
48 |
|
#define S34 23
|
49 |
|
#define S41 6
|
50 |
|
#define S42 10
|
51 |
|
#define S43 15
|
52 |
|
#define S44 21
|
53 |
|
|
54 |
|
static void MD5Transform (unsigned long [4], unsigned char [64]);
|
55 |
|
static void Encode (unsigned char *, unsigned long *, unsigned int);
|
56 |
|
static void Decode (unsigned long *, unsigned char *, unsigned int);
|
57 |
|
|
58 |
|
static unsigned char PADDING[64] =
|
59 |
|
{
|
60 |
|
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
61 |
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
62 |
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
63 |
|
};
|
64 |
|
|
65 |
|
/* F, G, H and I are basic MD5 functions.
|
66 |
|
|
67 |
|
*/
|
68 |
|
|
69 |
|
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
|
70 |
|
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
|
71 |
|
#define H(x, y, z) ((x) ^ (y) ^ (z))
|
72 |
|
#define I(x, y, z) ((y) ^ ((x) | (~z)))
|
73 |
|
|
74 |
|
/* ROTATE_LEFT rotates x left n bits.
|
75 |
|
|
76 |
|
*/
|
|
6 |
* This code implements the MD5 message-digest algorithm.
|
|
7 |
* The algorithm is due to Ron Rivest. This code was
|
|
8 |
* written by Colin Plumb in 1993, no copyright is claimed.
|
|
9 |
* This code is in the public domain; do with it what you wish.
|
|
10 |
*
|
|
11 |
* Equivalent code is available from RSA Data Security, Inc.
|
|
12 |
* This code has been tested against that, and is equivalent,
|
|
13 |
* except that you don't need to include two pages of legalese
|
|
14 |
* with every copy.
|
|
15 |
*
|
|
16 |
* To compute the message digest of a chunk of bytes, declare an
|
|
17 |
* MD5_CTX structure, pass it to MD5Init, call MD5Update as
|
|
18 |
* needed on buffers full of bytes, and then call MD5Final, which
|
|
19 |
* will fill a supplied 16-byte array with the digest.
|
|
20 |
*
|
|
21 |
* Changed so as no longer to depend on Colin Plumb's `usual.h' header
|
|
22 |
* definitions; now uses stuff from dpkg's config.h.
|
|
23 |
* - Ian Jackson <ian@chiark.greenend.org.uk>.
|
|
24 |
* Still in the public domain.
|
|
25 |
*/
|
|
26 |
|
|
27 |
#include <sys/types.h>
|
|
28 |
#include <stdint.h>
|
77 |
29 |
|
78 |
|
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
|
79 |
|
|
80 |
|
/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
|
81 |
|
Rotation is separate from addition to prevent recomputation.
|
82 |
|
|
83 |
|
*/
|
84 |
|
|
85 |
|
#define FF(a, b, c, d, x, s, ac) { \
|
86 |
|
(a) += F ((b), (c), (d)) + (x) + (unsigned long)(ac); \
|
87 |
|
(a) = ROTATE_LEFT ((a), (s)); \
|
88 |
|
(a) += (b); \
|
89 |
|
}
|
90 |
|
#define GG(a, b, c, d, x, s, ac) { \
|
91 |
|
(a) += G ((b), (c), (d)) + (x) + (unsigned long)(ac); \
|
92 |
|
(a) = ROTATE_LEFT ((a), (s)); \
|
93 |
|
(a) += (b); \
|
94 |
|
}
|
95 |
|
#define HH(a, b, c, d, x, s, ac) { \
|
96 |
|
(a) += H ((b), (c), (d)) + (x) + (unsigned long)(ac); \
|
97 |
|
(a) = ROTATE_LEFT ((a), (s)); \
|
98 |
|
(a) += (b); \
|
99 |
|
}
|
100 |
|
#define II(a, b, c, d, x, s, ac) { \
|
101 |
|
(a) += I ((b), (c), (d)) + (x) + (unsigned long)(ac); \
|
102 |
|
(a) = ROTATE_LEFT ((a), (s)); \
|
103 |
|
(a) += (b); \
|
104 |
|
}
|
105 |
|
|
106 |
|
/* MD5 initialization. Begins an MD5 operation, writing a new context.
|
107 |
|
|
108 |
|
*/
|
|
30 |
#include "MD5.h"
|
109 |
31 |
|
110 |
|
void MD5Init (MD5_CTX * context)
|
|
32 |
static void
|
|
33 |
byteSwap(UWORD32 *buf, unsigned words)
|
111 |
34 |
{
|
112 |
|
context->count[0] = context->count[1] = 0;
|
113 |
|
|
114 |
|
/* Load magic initialization constants.
|
115 |
|
|
116 |
|
*/
|
117 |
|
context->state[0] = 0x67452301;
|
118 |
|
context->state[1] = 0xefcdab89;
|
119 |
|
context->state[2] = 0x98badcfe;
|
120 |
|
context->state[3] = 0x10325476;
|
|
35 |
const uint32_t byteOrderTest = 0x1;
|
|
36 |
if (((char *)&byteOrderTest)[0] == 0) {
|
|
37 |
md5byte *p = (md5byte *)buf;
|
|
38 |
|
|
39 |
do {
|
|
40 |
*buf++ = (UWORD32)((unsigned)p[3] << 8 | p[2]) << 16 |
|
|
41 |
((unsigned)p[1] << 8 | p[0]);
|
|
42 |
p += 4;
|
|
43 |
} while (--words);
|
|
44 |
}
|
121 |
45 |
}
|
122 |
46 |
|
123 |
|
/* MD5 block update operation. Continues an MD5 message-digest
|
124 |
|
operation, processing another message block, and updating the context.
|
125 |
|
|
126 |
|
*/
|
127 |
|
|
128 |
|
void MD5Update ( MD5_CTX *context, /* context */
|
129 |
|
unsigned char *input, /* input block */
|
130 |
|
unsigned int inputLen) /* length of input block */
|
|
47 |
/*
|
|
48 |
* Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
|
|
49 |
* initialization constants.
|
|
50 |
*/
|
|
51 |
void
|
|
52 |
MD5Init(struct MD5_CTX *ctx)
|
131 |
53 |
{
|
132 |
|
using namespace std;
|
133 |
|
|
134 |
|
unsigned int i, index, partLen;
|
135 |
|
|
136 |
|
/* Compute number of bytes mod 64 */
|
137 |
|
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
|
138 |
|
|
139 |
|
/* Update number of bits */
|
140 |
|
if ((context->count[0] += ((unsigned long)inputLen << 3))
|
141 |
|
< ((unsigned long)inputLen << 3))
|
142 |
|
context->count[1]++;
|
143 |
|
context->count[1] += ((unsigned long)inputLen >> 29);
|
144 |
|
|
145 |
|
partLen = 64 - index;
|
146 |
|
|
147 |
|
/* Transform as many times as possible.
|
148 |
|
|
149 |
|
*/
|
150 |
|
if (inputLen >= partLen) {
|
151 |
|
std::memcpy (&context->buffer[index], input, partLen);
|
152 |
|
MD5Transform (context->state, context->buffer);
|
153 |
|
|
154 |
|
for (i = partLen; i + 63 < inputLen; i += 64)
|
155 |
|
MD5Transform (context->state, &input[i]);
|
156 |
|
|
157 |
|
index = 0;
|
158 |
|
}
|
159 |
|
else
|
160 |
|
i = 0;
|
161 |
|
|
162 |
|
/* Buffer remaining input */
|
163 |
|
std::memcpy (&context->buffer[index], &input[i], inputLen-i);
|
|
54 |
ctx->buf[0] = 0x67452301;
|
|
55 |
ctx->buf[1] = 0xefcdab89;
|
|
56 |
ctx->buf[2] = 0x98badcfe;
|
|
57 |
ctx->buf[3] = 0x10325476;
|
164 |
58 |
|
|
59 |
ctx->bytes[0] = 0;
|
|
60 |
ctx->bytes[1] = 0;
|
165 |
61 |
}
|
166 |
62 |
|
167 |
|
/* MD5 finalization. Ends an MD5 message-digest operation, writing the
|
168 |
|
the message digest and zeroizing the context.
|
169 |
|
|
170 |
|
*/
|
171 |
|
|
172 |
|
void MD5Final ( unsigned char digest[16], /* message digest */
|
173 |
|
MD5_CTX *context /* context */)
|
|
63 |
/*
|
|
64 |
* Update context to reflect the concatenation of another buffer full
|
|
65 |
* of bytes.
|
|
66 |
*/
|
|
67 |
void
|
|
68 |
MD5Update(struct MD5_CTX *ctx, md5byte const *buf, unsigned len)
|
174 |
69 |
{
|
175 |
|
unsigned char bits[8];
|
176 |
|
unsigned int index, padLen;
|
177 |
|
|
178 |
|
/* Save number of bits */
|
179 |
|
Encode (bits, context->count, 8);
|
180 |
|
|
181 |
|
/* Pad out to 56 mod 64.
|
182 |
|
|
183 |
|
*/
|
184 |
|
index = (unsigned int)((context->count[0] >> 3) & 0x3f);
|
185 |
|
padLen = (index < 56) ? (56 - index) : (120 - index);
|
186 |
|
MD5Update (context, PADDING, padLen);
|
187 |
|
|
188 |
|
/* Append length (before padding) */
|
189 |
|
MD5Update (context, bits, 8);
|
190 |
|
/* Store state in digest */
|
191 |
|
Encode (digest, context->state, 16);
|
192 |
|
|
193 |
|
/* Zeroize sensitive information.
|
194 |
|
|
195 |
|
*/
|
196 |
|
memset (context, 0, sizeof (*context));
|
|
70 |
UWORD32 t;
|
|
71 |
|
|
72 |
/* Update byte count */
|
|
73 |
|
|
74 |
t = ctx->bytes[0];
|
|
75 |
if ((ctx->bytes[0] = t + len) < t)
|
|
76 |
ctx->bytes[1]++; /* Carry from low to high */
|
|
77 |
|
|
78 |
t = 64 - (t & 0x3f); /* Space available in ctx->in (at least 1) */
|
|
79 |
if (t > len) {
|
|
80 |
memcpy((md5byte *)ctx->in + 64 - t, buf, len);
|
|
81 |
return;
|
|
82 |
}
|
|
83 |
/* First chunk is an odd size */
|
|
84 |
memcpy((md5byte *)ctx->in + 64 - t, buf, t);
|
|
85 |
byteSwap(ctx->in, 16);
|
|
86 |
MD5Transform(ctx->buf, ctx->in);
|
|
87 |
buf += t;
|
|
88 |
len -= t;
|
|
89 |
|
|
90 |
/* Process data in 64-byte chunks */
|
|
91 |
while (len >= 64) {
|
|
92 |
memcpy(ctx->in, buf, 64);
|
|
93 |
byteSwap(ctx->in, 16);
|
|
94 |
MD5Transform(ctx->buf, ctx->in);
|
|
95 |
buf += 64;
|
|
96 |
len -= 64;
|
|
97 |
}
|
|
98 |
|
|
99 |
/* Handle any remaining bytes of data. */
|
|
100 |
memcpy(ctx->in, buf, len);
|
197 |
101 |
}
|
198 |
102 |
|
199 |
|
/* MD5 basic transformation. Transforms state based on block.
|
200 |
|
|
201 |
|
*/
|
202 |
|
|
203 |
|
static void MD5Transform ( unsigned long state[4],
|
204 |
|
unsigned char block[64])
|
|
103 |
/*
|
|
104 |
* Final wrapup - pad to 64-byte boundary with the bit pattern
|
|
105 |
* 1 0* (64-bit count of bits processed, MSB-first)
|
|
106 |
*/
|
|
107 |
void
|
|
108 |
MD5Final(md5byte digest[16], struct MD5_CTX *ctx)
|
205 |
109 |
{
|
206 |
|
unsigned long a = state[0], b = state[1], c = state[2], d = state[3], x[16];
|
207 |
|
|
208 |
|
Decode (x, block, 64);
|
209 |
|
|
210 |
|
/* Round 1 */
|
211 |
|
FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
|
212 |
|
FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
|
213 |
|
FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
|
214 |
|
FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
|
215 |
|
FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
|
216 |
|
FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
|
217 |
|
FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
|
218 |
|
FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
|
219 |
|
FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
|
220 |
|
FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
|
221 |
|
FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
|
222 |
|
FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
|
223 |
|
FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
|
224 |
|
FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
|
225 |
|
FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
|
226 |
|
FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
|
227 |
|
|
228 |
|
/* Round 2 */
|
229 |
|
GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
|
230 |
|
GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
|
231 |
|
GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
|
232 |
|
GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
|
233 |
|
GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
|
234 |
|
GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
|
235 |
|
GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
|
236 |
|
GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
|
237 |
|
GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
|
238 |
|
GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
|
239 |
|
GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
|
240 |
|
GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
|
241 |
|
GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
|
242 |
|
GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
|
243 |
|
GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
|
244 |
|
GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
|
245 |
|
|
246 |
|
/* Round 3 */
|
247 |
|
HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
|
248 |
|
HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
|
249 |
|
HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
|
250 |
|
HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
|
251 |
|
HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
|
252 |
|
HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
|
253 |
|
HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
|
254 |
|
HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
|
255 |
|
HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
|
256 |
|
HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
|
257 |
|
HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
|
258 |
|
HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
|
259 |
|
HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
|
260 |
|
HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
|
261 |
|
HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
|
262 |
|
HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
|
263 |
|
|
264 |
|
/* Round 4 */
|
265 |
|
II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
|
266 |
|
II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
|
267 |
|
II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
|
268 |
|
II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
|
269 |
|
II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
|
270 |
|
II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
|
271 |
|
II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
|
272 |
|
II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
|
273 |
|
II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
|
274 |
|
II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
|
275 |
|
II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
|
276 |
|
II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
|
277 |
|
II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
|
278 |
|
II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
|
279 |
|
II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
|
280 |
|
II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
|
281 |
|
|
282 |
|
state[0] += a;
|
283 |
|
state[1] += b;
|
284 |
|
state[2] += c;
|
285 |
|
state[3] += d;
|
286 |
|
|
287 |
|
/* Zeroize sensitive information.
|
288 |
|
*/
|
289 |
|
memset (x, 0, sizeof (x));
|
|
110 |
int count = ctx->bytes[0] & 0x3f; /* Number of bytes in ctx->in */
|
|
111 |
md5byte *p = (md5byte *)ctx->in + count;
|
|
112 |
|
|
113 |
/* Set the first char of padding to 0x80. There is always room. */
|
|
114 |
*p++ = 0x80;
|
|
115 |
|
|
116 |
/* Bytes of padding needed to make 56 bytes (-8..55) */
|
|
117 |
count = 56 - 1 - count;
|
|
118 |
|
|
119 |
if (count < 0) { /* Padding forces an extra block */
|
|
120 |
memset(p, 0, count + 8);
|
|
121 |
byteSwap(ctx->in, 16);
|
|
122 |
MD5Transform(ctx->buf, ctx->in);
|
|
123 |
p = (md5byte *)ctx->in;
|
|
124 |
count = 56;
|
|
125 |
}
|
|
126 |
memset(p, 0, count);
|
|
127 |
byteSwap(ctx->in, 14);
|
|
128 |
|
|
129 |
/* Append length in bits and transform */
|
|
130 |
ctx->in[14] = ctx->bytes[0] << 3;
|
|
131 |
ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29;
|
|
132 |
MD5Transform(ctx->buf, ctx->in);
|
|
133 |
|
|
134 |
byteSwap(ctx->buf, 4);
|
|
135 |
memcpy(digest, ctx->buf, 16);
|
|
136 |
memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
|
290 |
137 |
}
|
291 |
138 |
|
292 |
|
/* Encodes input (unsigned long) into output (unsigned char). Assumes len is
|
293 |
|
a multiple of 4.
|
|
139 |
/* The four core functions - F1 is optimized somewhat */
|
294 |
140 |
|
295 |
|
*/
|
|
141 |
/* #define F1(x, y, z) (x & y | ~x & z) */
|
|
142 |
#define F1(x, y, z) (z ^ (x & (y ^ z)))
|
|
143 |
#define F2(x, y, z) F1(z, x, y)
|
|
144 |
#define F3(x, y, z) (x ^ y ^ z)
|
|
145 |
#define F4(x, y, z) (y ^ (x | ~z))
|
296 |
146 |
|
297 |
|
static void Encode ( unsigned char *output,
|
298 |
|
unsigned long *input,
|
299 |
|
unsigned int len)
|
300 |
|
{
|
301 |
|
unsigned int i, j;
|
302 |
|
|
303 |
|
for (i = 0, j = 0; j < len; i++, j += 4) {
|
304 |
|
output[j] = (unsigned char)(input[i] & 0xff);
|
305 |
|
output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
|
306 |
|
output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
|
307 |
|
output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
|
308 |
|
}
|
309 |
|
}
|
310 |
|
|
311 |
|
/* Decodes input (unsigned char) into output (unsigned long). Assumes len is
|
312 |
|
a multiple of 4.
|
|
147 |
/* This is the central step in the MD5 algorithm. */
|
|
148 |
#define MD5STEP(f,w,x,y,z,in,s) \
|
|
149 |
(w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
|
313 |
150 |
|
314 |
|
*/
|
315 |
|
|
316 |
|
static void Decode ( unsigned long *output,
|
317 |
|
unsigned char *input,
|
318 |
|
unsigned int len)
|
|
151 |
/*
|
|
152 |
* The core of the MD5 algorithm, this alters an existing MD5 hash to
|
|
153 |
* reflect the addition of 16 longwords of new data. MD5Update blocks
|
|
154 |
* the data and converts bytes into longwords for this routine.
|
|
155 |
*/
|
|
156 |
void
|
|
157 |
MD5Transform(UWORD32 buf[4], UWORD32 const in[16])
|
319 |
158 |
{
|
320 |
|
unsigned int i, j;
|
321 |
|
|
322 |
|
for (i = 0, j = 0; j < len; i++, j += 4)
|
323 |
|
output[i] = ((unsigned long)input[j]) | (((unsigned long)input[j+1]) << 8) |
|
324 |
|
(((unsigned long)input[j+2]) << 16) | (((unsigned long)input[j+3]) << 24);
|
|
159 |
register UWORD32 a, b, c, d;
|
|
160 |
|
|
161 |
a = buf[0];
|
|
162 |
b = buf[1];
|
|
163 |
c = buf[2];
|
|
164 |
d = buf[3];
|
|
165 |
|
|
166 |
MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
|
|
167 |
MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
|
|
168 |
MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
|
|
169 |
MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
|
|
170 |
MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
|
|
171 |
MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
|
|
172 |
MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
|
|
173 |
MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
|
|
174 |
MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
|
|
175 |
MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
|
|
176 |
MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
|
|
177 |
MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
|
|
178 |
MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
|
|
179 |
MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
|
|
180 |
MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
|
|
181 |
MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
|
|
182 |
|
|
183 |
MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
|
|
184 |
MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
|
|
185 |
MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
|
|
186 |
MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
|
|
187 |
MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
|
|
188 |
MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
|
|
189 |
MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
|
|
190 |
MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
|
|
191 |
MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
|
|
192 |
MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
|
|
193 |
MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
|
|
194 |
MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
|
|
195 |
MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
|
|
196 |
MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
|
|
197 |
MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
|
|
198 |
MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
|
|
199 |
|
|
200 |
MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
|
|
201 |
MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
|
|
202 |
MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
|
|
203 |
MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
|
|
204 |
MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
|
|
205 |
MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
|
|
206 |
MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
|
|
207 |
MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
|
|
208 |
MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
|
|
209 |
MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
|
|
210 |
MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
|
|
211 |
MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
|
|
212 |
MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
|
|
213 |
MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
|
|
214 |
MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
|
|
215 |
MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
|
|
216 |
|
|
217 |
MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
|
|
218 |
MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
|
|
219 |
MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
|
|
220 |
MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
|
|
221 |
MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
|
|
222 |
MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
|
|
223 |
MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
|
|
224 |
MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
|
|
225 |
MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
|
|
226 |
MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
|
|
227 |
MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
|
|
228 |
MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
|
|
229 |
MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
|
|
230 |
MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
|
|
231 |
MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
|
|
232 |
MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
|
|
233 |
|
|
234 |
buf[0] += a;
|
|
235 |
buf[1] += b;
|
|
236 |
buf[2] += c;
|
|
237 |
buf[3] += d;
|
325 |
238 |
}
|
326 |
|
-- xmpsdk/src/MD5.h
|
|
239 |
++ xmpsdk/src/MD5.h
|
... | ... | |
1 |
1 |
#ifndef __MD5_h__
|
2 |
2 |
#define __MD5_h__
|
3 |
3 |
|
4 |
|
/******************************************************************************/
|
5 |
|
|
6 |
4 |
/*
|
7 |
|
MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
|
8 |
|
|
9 |
|
Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All rights
|
10 |
|
reserved.
|
11 |
|
|
12 |
|
License to copy and use this software is granted provided that it is
|
13 |
|
identified as the "RSA Data Security, Inc. MD5 Message-Digest Algorithm" in
|
14 |
|
all material mentioning or referencing this software or this function.
|
15 |
|
|
16 |
|
License is also granted to make and use derivative works provided that such
|
17 |
|
works are identified as "derived from the RSA Data Security, Inc. MD5
|
18 |
|
Message-Digest Algorithm" in all material mentioning or referencing the
|
19 |
|
derived work.
|
20 |
|
|
21 |
|
RSA Data Security, Inc. makes no representations concerning either the
|
22 |
|
merchantability of this software or the suitability of this software for
|
23 |
|
any particular purpose. It is provided "as is" without express or implied
|
24 |
|
warranty of any kind.
|
25 |
|
|
26 |
|
These notices must be retained in any copies of any part of this
|
27 |
|
documentation and/or software.
|
28 |
|
*/
|
29 |
|
|
30 |
|
/******************************************************************************/
|
31 |
|
|
32 |
|
/* MD5 context. */
|
33 |
|
struct MD5_CTX
|
34 |
|
{
|
35 |
|
unsigned long state[4]; /* state (ABCD) */
|
36 |
|
unsigned long count[2]; /* number of bits, modulo 2^64 (lsb first) */
|
37 |
|
unsigned char buffer[64]; /* input buffer */
|
38 |
|
};
|
39 |
|
|
40 |
|
extern void MD5Init (MD5_CTX *);
|
41 |
|
extern void MD5Update (MD5_CTX *, unsigned char *, unsigned int);
|
42 |
|
extern void MD5Final(unsigned char [16], MD5_CTX *);
|
43 |
|
|
44 |
|
/******************************************************************************/
|
|
5 |
* This is the header file for the MD5 message-digest algorithm.
|
|
6 |
* The algorithm is due to Ron Rivest. This code was
|
|
7 |
* written by Colin Plumb in 1993, no copyright is claimed.
|
|
8 |
* This code is in the public domain; do with it what you wish.
|
|
9 |
*
|
|
10 |
* Equivalent code is available from RSA Data Security, Inc.
|
|
11 |
* This code has been tested against that, and is equivalent,
|
|
12 |
* except that you don't need to include two pages of legalese
|
|
13 |
* with every copy.
|
|
14 |
*
|
|
15 |
* To compute the message digest of a chunk of bytes, declare an
|
|
16 |
* MD5_CTX structure, pass it to MD5Init, call MD5Update as
|
|
17 |
* needed on buffers full of bytes, and then call MD5Final, which
|
|
18 |
* will fill a supplied 16-byte array with the digest.
|
|
19 |
*
|
|
20 |
* Changed so as no longer to depend on Colin Plumb's `usual.h'
|
|
21 |
* header definitions; now uses stuff from dpkg's config.h
|
|
22 |
* - Ian Jackson <ian@chiark.greenend.org.uk>.
|
|
23 |
* Still in the public domain.
|
|
24 |
*/
|
|
25 |
|
|
26 |
typedef unsigned char md5byte;
|
|
27 |
typedef uint32_t UWORD32;
|
|
28 |
|
|
29 |
struct MD5_CTX {
|
|
30 |
UWORD32 buf[4];
|
|
31 |
UWORD32 bytes[2];
|
|
32 |
UWORD32 in[16];
|
|
33 |
};
|
|
34 |
|
|
35 |
extern void MD5Init(struct MD5_CTX *context);
|
|
36 |
extern void MD5Update(struct MD5_CTX *context, md5byte const *buf, unsigned len);
|
|
37 |
extern void MD5Final(unsigned char digest[16], struct MD5_CTX *context);
|
|
38 |
extern void MD5Transform(UWORD32 buf[4], UWORD32 const in[16]);
|
45 |
39 |
|
46 |
40 |
#endif
|