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

Side by Side Diff: base/md5.cc

Issue 211273009: base::MD5IntermediateFinal() generates digest non-destructively. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Commentsmithing, mark context const. Created 6 years, 9 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 | Annotate | Revision Log
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 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
253 /* Append length in bits and transform */ 253 /* Append length in bits and transform */
254 ((uint32 *)ctx->in)[ 14 ] = ctx->bits[0]; 254 ((uint32 *)ctx->in)[ 14 ] = ctx->bits[0];
255 ((uint32 *)ctx->in)[ 15 ] = ctx->bits[1]; 255 ((uint32 *)ctx->in)[ 15 ] = ctx->bits[1];
256 256
257 MD5Transform(ctx->buf, (uint32 *)ctx->in); 257 MD5Transform(ctx->buf, (uint32 *)ctx->in);
258 byteReverse((unsigned char *)ctx->buf, 4); 258 byteReverse((unsigned char *)ctx->buf, 4);
259 memcpy(digest->a, ctx->buf, 16); 259 memcpy(digest->a, 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 /*
264 * Clone the context and use the clone to generate the digest.
awong 2014/03/31 20:22:19 This should be an impl comment, not a function com
Scott Hess - ex-Googler 2014/03/31 20:48:26 I'll try, but the commenting style is breaking my
awong 2014/03/31 21:04:32 I think it's critical to understand the MD5Final m
Scott Hess - ex-Googler 2014/03/31 21:29:59 Done.
265 */
266 void MD5IntermediateFinal(MD5Digest* digest, const MD5Context* context) {
267 MD5Context context_copy;
268 memcpy(&context_copy, context, sizeof(context_copy));
269 MD5Final(digest, &context_copy);
270 }
271
263 std::string MD5DigestToBase16(const MD5Digest& digest) { 272 std::string MD5DigestToBase16(const MD5Digest& digest) {
264 static char const zEncode[] = "0123456789abcdef"; 273 static char const zEncode[] = "0123456789abcdef";
265 274
266 std::string ret; 275 std::string ret;
267 ret.resize(32); 276 ret.resize(32);
268 277
269 int j = 0; 278 int j = 0;
270 for (int i = 0; i < 16; i ++) { 279 for (int i = 0; i < 16; i ++) {
271 int a = digest.a[i]; 280 int a = digest.a[i];
272 ret[j++] = zEncode[(a>>4)&0xf]; 281 ret[j++] = zEncode[(a>>4)&0xf];
(...skipping 10 matching lines...) Expand all
283 MD5Final(digest, &ctx); 292 MD5Final(digest, &ctx);
284 } 293 }
285 294
286 std::string MD5String(const StringPiece& str) { 295 std::string MD5String(const StringPiece& str) {
287 MD5Digest digest; 296 MD5Digest digest;
288 MD5Sum(str.data(), str.length(), &digest); 297 MD5Sum(str.data(), str.length(), &digest);
289 return MD5DigestToBase16(digest); 298 return MD5DigestToBase16(digest);
290 } 299 }
291 300
292 } // namespace base 301 } // namespace base
OLDNEW
« no previous file with comments | « base/md5.h ('k') | base/md5_unittest.cc » ('j') | base/md5_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698