OLD | NEW |
1 // The original file was copied from sqlite, and was in the public domain. | 1 // The original file was copied from sqlite, and was in the public domain. |
2 // Modifications Copyright 2006 Google Inc. All Rights Reserved | 2 // Modifications Copyright 2006 Google Inc. All Rights Reserved |
3 | 3 |
4 /* | 4 /* |
5 * This code implements the MD5 message-digest algorithm. | 5 * This code implements the MD5 message-digest algorithm. |
6 * The algorithm is due to Ron Rivest. This code was | 6 * The algorithm is due to Ron Rivest. This code was |
7 * written by Colin Plumb in 1993, no copyright is claimed. | 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. | 8 * This code is in the public domain; do with it what you wish. |
9 * | 9 * |
10 * Equivalent code is available from RSA Data Security, Inc. | 10 * Equivalent code is available from RSA Data Security, Inc. |
(...skipping 15 matching lines...) Expand all Loading... |
26 | 26 |
27 struct Context { | 27 struct Context { |
28 uint32 buf[4]; | 28 uint32 buf[4]; |
29 uint32 bits[2]; | 29 uint32 bits[2]; |
30 unsigned char in[64]; | 30 unsigned char in[64]; |
31 }; | 31 }; |
32 | 32 |
33 /* | 33 /* |
34 * Note: this code is harmless on little-endian machines. | 34 * Note: this code is harmless on little-endian machines. |
35 */ | 35 */ |
36 static void byteReverse(unsigned char *buf, unsigned longs){ | 36 static void byteReverse (unsigned char *buf, unsigned longs){ |
37 uint32 t; | 37 uint32 t; |
38 do { | 38 do { |
39 t = (uint32)((unsigned)buf[3]<<8 | buf[2]) << 16 | | 39 t = (uint32)((unsigned)buf[3]<<8 | buf[2]) << 16 | |
40 ((unsigned)buf[1]<<8 | buf[0]); | 40 ((unsigned)buf[1]<<8 | buf[0]); |
41 *(uint32 *)buf = t; | 41 *(uint32 *)buf = t; |
42 buf += 4; | 42 buf += 4; |
43 } while (--longs); | 43 } while (--longs); |
44 } | 44 } |
45 /* The four core functions - F1 is optimized somewhat */ | 45 /* The four core functions - F1 is optimized somewhat */ |
46 | 46 |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 | 168 |
169 t = ctx->bits[0]; | 169 t = ctx->bits[0]; |
170 if ((ctx->bits[0] = t + ((uint32)len << 3)) < t) | 170 if ((ctx->bits[0] = t + ((uint32)len << 3)) < t) |
171 ctx->bits[1]++; /* Carry from low to high */ | 171 ctx->bits[1]++; /* Carry from low to high */ |
172 ctx->bits[1] += static_cast<uint32>(len >> 29); | 172 ctx->bits[1] += static_cast<uint32>(len >> 29); |
173 | 173 |
174 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ | 174 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ |
175 | 175 |
176 /* Handle any leading odd-sized chunks */ | 176 /* Handle any leading odd-sized chunks */ |
177 | 177 |
178 if (t) { | 178 if ( t ) { |
179 unsigned char *p = (unsigned char *)ctx->in + t; | 179 unsigned char *p = (unsigned char *)ctx->in + t; |
180 | 180 |
181 t = 64-t; | 181 t = 64-t; |
182 if (len < t) { | 182 if (len < t) { |
183 memcpy(p, buf, len); | 183 memcpy(p, buf, len); |
184 return; | 184 return; |
185 } | 185 } |
186 memcpy(p, buf, t); | 186 memcpy(p, buf, t); |
187 byteReverse(ctx->in, 16); | 187 byteReverse(ctx->in, 16); |
188 MD5Transform(ctx->buf, (uint32 *)ctx->in); | 188 MD5Transform(ctx->buf, (uint32 *)ctx->in); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */ | 250 memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */ |
251 } | 251 } |
252 | 252 |
253 std::string MD5DigestToBase16(const MD5Digest& digest){ | 253 std::string MD5DigestToBase16(const MD5Digest& digest){ |
254 static char const zEncode[] = "0123456789abcdef"; | 254 static char const zEncode[] = "0123456789abcdef"; |
255 | 255 |
256 std::string ret; | 256 std::string ret; |
257 ret.resize(32); | 257 ret.resize(32); |
258 | 258 |
259 int j = 0; | 259 int j = 0; |
260 for (int i = 0; i < 16; i ++) { | 260 for(int i = 0; i < 16; i ++){ |
261 int a = digest.a[i]; | 261 int a = digest.a[i]; |
262 ret[j++] = zEncode[(a>>4)&0xf]; | 262 ret[j++] = zEncode[(a>>4)&0xf]; |
263 ret[j++] = zEncode[a & 0xf]; | 263 ret[j++] = zEncode[a & 0xf]; |
264 } | 264 } |
265 return ret; | 265 return ret; |
266 } | 266 } |
267 | 267 |
268 void MD5Sum(const void* data, size_t length, MD5Digest* digest) { | 268 void MD5Sum(const void* data, size_t length, MD5Digest* digest) { |
269 MD5Context ctx; | 269 MD5Context ctx; |
270 MD5Init(&ctx); | 270 MD5Init(&ctx); |
271 MD5Update(&ctx, static_cast<const unsigned char*>(data), length); | 271 MD5Update(&ctx, static_cast<const unsigned char*>(data), length); |
272 MD5Final(digest, &ctx); | 272 MD5Final(digest, &ctx); |
273 } | 273 } |
274 | 274 |
275 std::string MD5String(const std::string& str) { | 275 std::string MD5String(const std::string& str) { |
276 MD5Digest digest; | 276 MD5Digest digest; |
277 MD5Sum(str.data(), str.length(), &digest); | 277 MD5Sum(str.data(), str.length(), &digest); |
278 return MD5DigestToBase16(digest); | 278 return MD5DigestToBase16(digest); |
279 } | 279 } |
OLD | NEW |