| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // The original file was copied from sqlite, and was in the public domain. | 5 // The original file was copied from sqlite, and was in the public domain. |
| 6 | 6 |
| 7 /* | 7 /* |
| 8 * This code implements the MD5 message-digest algorithm. | 8 * This code implements the MD5 message-digest algorithm. |
| 9 * The algorithm is due to Ron Rivest. This code was | 9 * The algorithm is due to Ron Rivest. This code was |
| 10 * written by Colin Plumb in 1993, no copyright is claimed. | 10 * written by Colin Plumb in 1993, no copyright is claimed. |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 byteReverse(ctx->in, 14); | 249 byteReverse(ctx->in, 14); |
| 250 | 250 |
| 251 /* Append length in bits and transform */ | 251 /* Append length in bits and transform */ |
| 252 memcpy(&ctx->in[14 * sizeof(ctx->bits[0])], &ctx->bits[0], | 252 memcpy(&ctx->in[14 * sizeof(ctx->bits[0])], &ctx->bits[0], |
| 253 sizeof(ctx->bits[0])); | 253 sizeof(ctx->bits[0])); |
| 254 memcpy(&ctx->in[15 * sizeof(ctx->bits[1])], &ctx->bits[1], | 254 memcpy(&ctx->in[15 * sizeof(ctx->bits[1])], &ctx->bits[1], |
| 255 sizeof(ctx->bits[1])); | 255 sizeof(ctx->bits[1])); |
| 256 | 256 |
| 257 MD5Transform(ctx->buf, (uint32_t*)ctx->in); | 257 MD5Transform(ctx->buf, (uint32_t*)ctx->in); |
| 258 byteReverse((uint8_t*)ctx->buf, 4); | 258 byteReverse((uint8_t*)ctx->buf, 4); |
| 259 memcpy(digest->a, ctx->buf, 16); | 259 memcpy(digest, ctx->buf, 16); |
| 260 memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */ | 260 memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */ |
| 261 } | 261 } |
| 262 | 262 |
| 263 void MD5IntermediateFinal(MD5Digest* digest, const MD5Context* context) { | 263 void MD5IntermediateFinal(MD5Digest* digest, const MD5Context* context) { |
| 264 /* MD5Final mutates the MD5Context*. Make a copy for generating the | 264 /* MD5Final mutates the MD5Context*. Make a copy for generating the |
| 265 intermediate value. */ | 265 intermediate value. */ |
| 266 MD5Context context_copy; | 266 MD5Context context_copy; |
| 267 memcpy(&context_copy, context, sizeof(context_copy)); | 267 memcpy(&context_copy, context, sizeof(context_copy)); |
| 268 MD5Final(digest, &context_copy); | 268 MD5Final(digest, &context_copy); |
| 269 } | 269 } |
| 270 | 270 |
| 271 std::string MD5DigestToBase16(const MD5Digest& digest) { | 271 std::string MD5DigestToBase16(const MD5Digest& digest) { |
| 272 static char const zEncode[] = "0123456789abcdef"; | 272 static char const zEncode[] = "0123456789abcdef"; |
| 273 | 273 |
| 274 std::string ret; | 274 std::string ret; |
| 275 ret.resize(32); | 275 ret.resize(32); |
| 276 | 276 |
| 277 for (int i = 0, j = 0; i < 16; i++, j += 2) { | 277 for (int i = 0, j = 0; i < 16; i++, j += 2) { |
| 278 uint8_t a = digest.a[i]; | 278 ret[j] = zEncode[(digest[i] >> 4) & 0xf]; |
| 279 ret[j] = zEncode[(a >> 4) & 0xf]; | 279 ret[j + 1] = zEncode[digest[i] & 0xf]; |
| 280 ret[j + 1] = zEncode[a & 0xf]; | |
| 281 } | 280 } |
| 282 return ret; | 281 return ret; |
| 283 } | 282 } |
| 284 | 283 |
| 285 void MD5Sum(const void* data, size_t length, MD5Digest* digest) { | 284 void MD5Sum(const void* data, size_t length, MD5Digest* digest) { |
| 286 MD5Context ctx; | 285 MD5Context ctx; |
| 287 MD5Init(&ctx); | 286 MD5Init(&ctx); |
| 288 MD5Update(&ctx, StringPiece(reinterpret_cast<const char*>(data), length)); | 287 MD5Update(&ctx, StringPiece(reinterpret_cast<const char*>(data), length)); |
| 289 MD5Final(digest, &ctx); | 288 MD5Final(digest, &ctx); |
| 290 } | 289 } |
| 291 | 290 |
| 292 std::string MD5String(const StringPiece& str) { | 291 std::string MD5String(const StringPiece& str) { |
| 293 MD5Digest digest; | 292 MD5Digest digest; |
| 294 MD5Sum(str.data(), str.length(), &digest); | 293 MD5Sum(str.data(), str.length(), &digest); |
| 295 return MD5DigestToBase16(digest); | 294 return MD5DigestToBase16(digest); |
| 296 } | 295 } |
| 297 | 296 |
| 298 } // namespace base | 297 } // namespace base |
| OLD | NEW |