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 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 } | 240 } |
241 byteReverse(ctx->in, 14); | 241 byteReverse(ctx->in, 14); |
242 | 242 |
243 /* Append length in bits and transform */ | 243 /* Append length in bits and transform */ |
244 ((uint32 *)ctx->in)[ 14 ] = ctx->bits[0]; | 244 ((uint32 *)ctx->in)[ 14 ] = ctx->bits[0]; |
245 ((uint32 *)ctx->in)[ 15 ] = ctx->bits[1]; | 245 ((uint32 *)ctx->in)[ 15 ] = ctx->bits[1]; |
246 | 246 |
247 MD5Transform(ctx->buf, (uint32 *)ctx->in); | 247 MD5Transform(ctx->buf, (uint32 *)ctx->in); |
248 byteReverse((unsigned char *)ctx->buf, 4); | 248 byteReverse((unsigned char *)ctx->buf, 4); |
249 memcpy(digest->a, ctx->buf, 16); | 249 memcpy(digest->a, ctx->buf, 16); |
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 |