Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(24)

Side by Side Diff: base/md5.cc

Issue 1099453002: base: Simplify the construction of MD5Digest. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: REBASE Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/md5.h ('k') | base/md5_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
OLDNEW
« no previous file with comments | « base/md5.h ('k') | base/md5_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698