|
a/libupnpp/md5.cxx |
|
b/libupnpp/md5.cxx |
|
... |
|
... |
54 |
*/
|
54 |
*/
|
55 |
|
55 |
|
56 |
static void
|
56 |
static void
|
57 |
Encode (unsigned char *output, md5uint32 *input, unsigned int len)
|
57 |
Encode (unsigned char *output, md5uint32 *input, unsigned int len)
|
58 |
{
|
58 |
{
|
59 |
unsigned int i, j;
|
59 |
unsigned int i, j;
|
60 |
|
60 |
|
61 |
for (i = 0, j = 0; j < len; i++, j += 4) {
|
61 |
for (i = 0, j = 0; j < len; i++, j += 4) {
|
62 |
output[j] = (unsigned char)(input[i] & 0xff);
|
62 |
output[j] = (unsigned char)(input[i] & 0xff);
|
63 |
output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
|
63 |
output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
|
64 |
output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
|
64 |
output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
|
65 |
output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
|
65 |
output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
|
66 |
}
|
66 |
}
|
67 |
}
|
67 |
}
|
68 |
|
68 |
|
69 |
/*
|
69 |
/*
|
70 |
* Decodes input (unsigned char) into output (md5uint32). Assumes len is
|
70 |
* Decodes input (unsigned char) into output (md5uint32). Assumes len is
|
71 |
* a multiple of 4.
|
71 |
* a multiple of 4.
|
72 |
*/
|
72 |
*/
|
73 |
|
73 |
|
74 |
static void
|
74 |
static void
|
75 |
Decode (md5uint32 *output, const unsigned char *input, unsigned int len)
|
75 |
Decode (md5uint32 *output, const unsigned char *input, unsigned int len)
|
76 |
{
|
76 |
{
|
77 |
unsigned int i, j;
|
77 |
unsigned int i, j;
|
78 |
|
78 |
|
79 |
for (i = 0, j = 0; j < len; i++, j += 4)
|
79 |
for (i = 0, j = 0; j < len; i++, j += 4)
|
80 |
output[i] = ((md5uint32)input[j]) | (((md5uint32)input[j+1]) << 8) |
|
80 |
output[i] = ((md5uint32)input[j]) | (((md5uint32)input[j+1]) << 8) |
|
81 |
(((md5uint32)input[j+2]) << 16) | (((md5uint32)input[j+3]) << 24);
|
81 |
(((md5uint32)input[j+2]) << 16) | (((md5uint32)input[j+3]) << 24);
|
82 |
}
|
82 |
}
|
83 |
#endif /* i386 */
|
83 |
#endif /* i386 */
|
84 |
|
84 |
|
85 |
static unsigned char PADDING[64] = {
|
85 |
static unsigned char PADDING[64] = {
|
86 |
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
86 |
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
87 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
87 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
88 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
88 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
89 |
};
|
89 |
};
|
90 |
|
90 |
|
91 |
/* F, G, H and I are basic MD5 functions. */
|
91 |
/* F, G, H and I are basic MD5 functions. */
|
92 |
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
|
92 |
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
|
93 |
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
|
93 |
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
|
|
... |
|
... |
126 |
|
126 |
|
127 |
void
|
127 |
void
|
128 |
MD5Init (MD5_CTX *context)
|
128 |
MD5Init (MD5_CTX *context)
|
129 |
{
|
129 |
{
|
130 |
|
130 |
|
131 |
context->count[0] = context->count[1] = 0;
|
131 |
context->count[0] = context->count[1] = 0;
|
132 |
|
132 |
|
133 |
/* Load magic initialization constants. */
|
133 |
/* Load magic initialization constants. */
|
134 |
context->state[0] = 0x67452301;
|
134 |
context->state[0] = 0x67452301;
|
135 |
context->state[1] = 0xefcdab89;
|
135 |
context->state[1] = 0xefcdab89;
|
136 |
context->state[2] = 0x98badcfe;
|
136 |
context->state[2] = 0x98badcfe;
|
137 |
context->state[3] = 0x10325476;
|
137 |
context->state[3] = 0x10325476;
|
138 |
}
|
138 |
}
|
139 |
|
139 |
|
140 |
/*
|
140 |
/*
|
141 |
* MD5 block update operation. Continues an MD5 message-digest
|
141 |
* MD5 block update operation. Continues an MD5 message-digest
|
142 |
* operation, processing another message block, and updating the
|
142 |
* operation, processing another message block, and updating the
|
143 |
* context.
|
143 |
* context.
|
144 |
*/
|
144 |
*/
|
145 |
|
145 |
|
146 |
void
|
146 |
void
|
147 |
MD5Update (MD5_CTX *context, const unsigned char *input, unsigned int inputLen)
|
147 |
MD5Update (MD5_CTX *context, const unsigned char *input, unsigned int inputLen)
|
148 |
{
|
148 |
{
|
149 |
unsigned int i, index, partLen;
|
149 |
unsigned int i, index, partLen;
|
150 |
|
150 |
|
151 |
/* Compute number of bytes mod 64 */
|
151 |
/* Compute number of bytes mod 64 */
|
152 |
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
|
152 |
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
|
153 |
|
153 |
|
154 |
/* Update number of bits */
|
154 |
/* Update number of bits */
|
155 |
if ((context->count[0] += ((md5uint32)inputLen << 3))
|
155 |
if ((context->count[0] += ((md5uint32)inputLen << 3))
|
156 |
< ((md5uint32)inputLen << 3))
|
156 |
< ((md5uint32)inputLen << 3))
|
157 |
context->count[1]++;
|
157 |
context->count[1]++;
|
158 |
context->count[1] += ((md5uint32)inputLen >> 29);
|
158 |
context->count[1] += ((md5uint32)inputLen >> 29);
|
159 |
|
159 |
|
160 |
partLen = 64 - index;
|
160 |
partLen = 64 - index;
|
161 |
|
161 |
|
162 |
/* Transform as many times as possible. */
|
162 |
/* Transform as many times as possible. */
|
163 |
if (inputLen >= partLen) {
|
163 |
if (inputLen >= partLen) {
|
164 |
memcpy((void *)&context->buffer[index], (const void *)input,
|
164 |
memcpy((void *)&context->buffer[index], (const void *)input,
|
165 |
partLen);
|
165 |
partLen);
|
166 |
MD5Transform (context->state, context->buffer);
|
166 |
MD5Transform (context->state, context->buffer);
|
167 |
|
167 |
|
168 |
for (i = partLen; i + 63 < inputLen; i += 64)
|
168 |
for (i = partLen; i + 63 < inputLen; i += 64)
|
169 |
MD5Transform (context->state, &input[i]);
|
169 |
MD5Transform (context->state, &input[i]);
|
170 |
|
170 |
|
171 |
index = 0;
|
171 |
index = 0;
|
172 |
}
|
172 |
}
|
173 |
else
|
173 |
else
|
174 |
i = 0;
|
174 |
i = 0;
|
175 |
|
175 |
|
176 |
/* Buffer remaining input */
|
176 |
/* Buffer remaining input */
|
177 |
memcpy ((void *)&context->buffer[index], (const void *)&input[i],
|
177 |
memcpy ((void *)&context->buffer[index], (const void *)&input[i],
|
178 |
inputLen-i);
|
178 |
inputLen-i);
|
179 |
}
|
179 |
}
|
180 |
|
180 |
|
181 |
/*
|
181 |
/*
|
182 |
* MD5 padding. Adds padding followed by original length.
|
182 |
* MD5 padding. Adds padding followed by original length.
|
183 |
*/
|
183 |
*/
|
184 |
|
184 |
|
185 |
void
|
185 |
void
|
186 |
MD5Pad (MD5_CTX *context)
|
186 |
MD5Pad (MD5_CTX *context)
|
187 |
{
|
187 |
{
|
188 |
unsigned char bits[8];
|
188 |
unsigned char bits[8];
|
189 |
unsigned int index, padLen;
|
189 |
unsigned int index, padLen;
|
190 |
|
190 |
|
191 |
/* Save number of bits */
|
191 |
/* Save number of bits */
|
192 |
Encode (bits, context->count, 8);
|
192 |
Encode (bits, context->count, 8);
|
193 |
|
193 |
|
194 |
/* Pad out to 56 mod 64. */
|
194 |
/* Pad out to 56 mod 64. */
|
195 |
index = (unsigned int)((context->count[0] >> 3) & 0x3f);
|
195 |
index = (unsigned int)((context->count[0] >> 3) & 0x3f);
|
196 |
padLen = (index < 56) ? (56 - index) : (120 - index);
|
196 |
padLen = (index < 56) ? (56 - index) : (120 - index);
|
197 |
MD5Update (context, PADDING, padLen);
|
197 |
MD5Update (context, PADDING, padLen);
|
198 |
|
198 |
|
199 |
/* Append length (before padding) */
|
199 |
/* Append length (before padding) */
|
200 |
MD5Update (context, bits, 8);
|
200 |
MD5Update (context, bits, 8);
|
201 |
}
|
201 |
}
|
202 |
|
202 |
|
203 |
/*
|
203 |
/*
|
204 |
* MD5 finalization. Ends an MD5 message-digest operation, writing the
|
204 |
* MD5 finalization. Ends an MD5 message-digest operation, writing the
|
205 |
* the message digest and zeroizing the context.
|
205 |
* the message digest and zeroizing the context.
|
206 |
*/
|
206 |
*/
|
207 |
|
207 |
|
208 |
void
|
208 |
void
|
209 |
MD5Final (unsigned char digest[16],MD5_CTX *context)
|
209 |
MD5Final (unsigned char digest[16],MD5_CTX *context)
|
210 |
{
|
210 |
{
|
211 |
/* Do padding. */
|
211 |
/* Do padding. */
|
212 |
MD5Pad (context);
|
212 |
MD5Pad (context);
|
213 |
|
213 |
|
214 |
/* Store state in digest */
|
214 |
/* Store state in digest */
|
215 |
Encode (digest, context->state, 16);
|
215 |
Encode (digest, context->state, 16);
|
216 |
|
216 |
|
217 |
/* Zeroize sensitive information. */
|
217 |
/* Zeroize sensitive information. */
|
218 |
memset ((void *)context, 0, sizeof (*context));
|
218 |
memset ((void *)context, 0, sizeof (*context));
|
219 |
}
|
219 |
}
|
220 |
|
220 |
|
221 |
/* MD5 basic transformation. Transforms state based on block. */
|
221 |
/* MD5 basic transformation. Transforms state based on block. */
|
222 |
|
222 |
|
223 |
static void
|
223 |
static void
|
224 |
MD5Transform (md5uint32 state[4], const unsigned char block[64])
|
224 |
MD5Transform (md5uint32 state[4], const unsigned char block[64])
|
225 |
{
|
225 |
{
|
226 |
md5uint32 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
|
226 |
md5uint32 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
|
227 |
|
227 |
|
228 |
Decode (x, block, 64);
|
228 |
Decode (x, block, 64);
|
229 |
|
229 |
|
230 |
/* Round 1 */
|
230 |
/* Round 1 */
|
231 |
#define S11 7
|
231 |
#define S11 7
|
232 |
#define S12 12
|
232 |
#define S12 12
|
233 |
#define S13 17
|
233 |
#define S13 17
|
234 |
#define S14 22
|
234 |
#define S14 22
|
235 |
FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
|
235 |
FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
|
236 |
FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
|
236 |
FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
|
237 |
FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
|
237 |
FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
|
238 |
FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
|
238 |
FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
|
239 |
FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
|
239 |
FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
|
240 |
FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
|
240 |
FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
|
241 |
FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
|
241 |
FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
|
242 |
FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
|
242 |
FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
|
243 |
FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
|
243 |
FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
|
244 |
FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
|
244 |
FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
|
245 |
FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
|
245 |
FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
|
246 |
FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
|
246 |
FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
|
247 |
FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
|
247 |
FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
|
248 |
FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
|
248 |
FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
|
249 |
FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
|
249 |
FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
|
250 |
FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
|
250 |
FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
|
251 |
|
251 |
|
252 |
/* Round 2 */
|
252 |
/* Round 2 */
|
253 |
#define S21 5
|
253 |
#define S21 5
|
254 |
#define S22 9
|
254 |
#define S22 9
|
255 |
#define S23 14
|
255 |
#define S23 14
|
256 |
#define S24 20
|
256 |
#define S24 20
|
257 |
GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
|
257 |
GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
|
258 |
GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
|
258 |
GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
|
259 |
GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
|
259 |
GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
|
260 |
GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
|
260 |
GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
|
261 |
GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
|
261 |
GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
|
262 |
GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
|
262 |
GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
|
263 |
GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
|
263 |
GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
|
264 |
GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
|
264 |
GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
|
265 |
GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
|
265 |
GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
|
266 |
GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
|
266 |
GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
|
267 |
GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
|
267 |
GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
|
268 |
GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
|
268 |
GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
|
269 |
GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
|
269 |
GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
|
270 |
GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
|
270 |
GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
|
271 |
GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
|
271 |
GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
|
272 |
GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
|
272 |
GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
|
273 |
|
273 |
|
274 |
/* Round 3 */
|
274 |
/* Round 3 */
|
275 |
#define S31 4
|
275 |
#define S31 4
|
276 |
#define S32 11
|
276 |
#define S32 11
|
277 |
#define S33 16
|
277 |
#define S33 16
|
278 |
#define S34 23
|
278 |
#define S34 23
|
279 |
HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
|
279 |
HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
|
280 |
HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
|
280 |
HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
|
281 |
HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
|
281 |
HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
|
282 |
HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
|
282 |
HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
|
283 |
HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
|
283 |
HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
|
284 |
HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
|
284 |
HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
|
285 |
HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
|
285 |
HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
|
286 |
HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
|
286 |
HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
|
287 |
HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
|
287 |
HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
|
288 |
HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
|
288 |
HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
|
289 |
HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
|
289 |
HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
|
290 |
HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
|
290 |
HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
|
291 |
HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
|
291 |
HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
|
292 |
HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
|
292 |
HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
|
293 |
HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
|
293 |
HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
|
294 |
HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
|
294 |
HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
|
295 |
|
295 |
|
296 |
/* Round 4 */
|
296 |
/* Round 4 */
|
297 |
#define S41 6
|
297 |
#define S41 6
|
298 |
#define S42 10
|
298 |
#define S42 10
|
299 |
#define S43 15
|
299 |
#define S43 15
|
300 |
#define S44 21
|
300 |
#define S44 21
|
301 |
II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
|
301 |
II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
|
302 |
II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
|
302 |
II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
|
303 |
II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
|
303 |
II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
|
304 |
II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
|
304 |
II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
|
305 |
II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
|
305 |
II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
|
306 |
II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
|
306 |
II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
|
307 |
II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
|
307 |
II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
|
308 |
II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
|
308 |
II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
|
309 |
II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
|
309 |
II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
|
310 |
II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
|
310 |
II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
|
311 |
II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
|
311 |
II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
|
312 |
II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
|
312 |
II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
|
313 |
II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
|
313 |
II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
|
314 |
II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
|
314 |
II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
|
315 |
II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
|
315 |
II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
|
316 |
II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
|
316 |
II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
|
317 |
|
317 |
|
318 |
state[0] += a;
|
318 |
state[0] += a;
|
319 |
state[1] += b;
|
319 |
state[1] += b;
|
320 |
state[2] += c;
|
320 |
state[2] += c;
|
321 |
state[3] += d;
|
321 |
state[3] += d;
|
322 |
|
322 |
|
323 |
/* Zeroize sensitive information. */
|
323 |
/* Zeroize sensitive information. */
|
324 |
memset ((void *)x, 0, sizeof (x));
|
324 |
memset ((void *)x, 0, sizeof (x));
|
325 |
}
|
325 |
}
|
326 |
|
326 |
|
327 |
/*************** Convenience / utilities */
|
327 |
/*************** Convenience / utilities */
|
328 |
void MD5Final(string &digest, MD5_CTX *context)
|
328 |
void MD5Final(string &digest, MD5_CTX *context)
|
329 |
{
|
329 |
{
|
|
... |
|
... |
334 |
|
334 |
|
335 |
string& MD5String(const string& data, string& digest)
|
335 |
string& MD5String(const string& data, string& digest)
|
336 |
{
|
336 |
{
|
337 |
MD5_CTX ctx;
|
337 |
MD5_CTX ctx;
|
338 |
MD5Init(&ctx);
|
338 |
MD5Init(&ctx);
|
339 |
MD5Update(&ctx, (const unsigned char*)data.c_str(), data.length());
|
339 |
MD5Update(&ctx, (const unsigned char*)data.c_str(),
|
|
|
340 |
(unsigned int)data.length());
|
340 |
MD5Final(digest, &ctx);
|
341 |
MD5Final(digest, &ctx);
|
341 |
return digest;
|
342 |
return digest;
|
342 |
}
|
343 |
}
|
343 |
|
344 |
|
344 |
string& MD5HexPrint(const string& digest, string &out)
|
345 |
string& MD5HexPrint(const string& digest, string &out)
|
|
... |
|
... |
346 |
out.erase();
|
347 |
out.erase();
|
347 |
out.reserve(33);
|
348 |
out.reserve(33);
|
348 |
static const char hex[]="0123456789abcdef";
|
349 |
static const char hex[]="0123456789abcdef";
|
349 |
const unsigned char *hash = (const unsigned char *)digest.c_str();
|
350 |
const unsigned char *hash = (const unsigned char *)digest.c_str();
|
350 |
for (int i = 0; i < 16; i++) {
|
351 |
for (int i = 0; i < 16; i++) {
|
351 |
out.append(1, hex[hash[i] >> 4]);
|
352 |
out.append(1, hex[hash[i] >> 4]);
|
352 |
out.append(1, hex[hash[i] & 0x0f]);
|
353 |
out.append(1, hex[hash[i] & 0x0f]);
|
353 |
}
|
354 |
}
|
354 |
return out;
|
355 |
return out;
|
355 |
}
|
356 |
}
|
356 |
string& MD5HexScan(const string& xdigest, string& digest)
|
357 |
string& MD5HexScan(const string& xdigest, string& digest)
|
357 |
{
|
358 |
{
|
358 |
digest.erase();
|
359 |
digest.erase();
|
359 |
if (xdigest.length() != 32) {
|
360 |
if (xdigest.length() != 32) {
|
360 |
return digest;
|
361 |
return digest;
|
361 |
}
|
362 |
}
|
362 |
for (unsigned int i = 0; i < 16; i++) {
|
363 |
for (unsigned int i = 0; i < 16; i++) {
|
363 |
unsigned int val;
|
364 |
unsigned int val;
|
364 |
if (sscanf(xdigest.c_str() + 2*i, "%2x", &val) != 1) {
|
365 |
if (sscanf(xdigest.c_str() + 2*i, "%2x", &val) != 1) {
|
365 |
digest.erase();
|
366 |
digest.erase();
|
366 |
return digest;
|
367 |
return digest;
|
367 |
}
|
368 |
}
|
368 |
digest.append(1, (unsigned char)val);
|
369 |
digest.append(1, (unsigned char)val);
|
369 |
}
|
370 |
}
|
370 |
return digest;
|
371 |
return digest;
|
371 |
}
|
372 |
}
|
372 |
|
373 |
|
373 |
}
|
374 |
}
|