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

Side by Side Diff: base/md5.cc

Issue 1065733005: base: Use a simple tricky to remove a temporary variable in MD5DigestToBase16. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: use thestig's tricky 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 | « no previous file | no next file » | 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 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 memcpy(&context_copy, context, sizeof(context_copy)); 269 memcpy(&context_copy, context, sizeof(context_copy));
270 MD5Final(digest, &context_copy); 270 MD5Final(digest, &context_copy);
271 } 271 }
272 272
273 std::string MD5DigestToBase16(const MD5Digest& digest) { 273 std::string MD5DigestToBase16(const MD5Digest& digest) {
274 static char const zEncode[] = "0123456789abcdef"; 274 static char const zEncode[] = "0123456789abcdef";
275 275
276 std::string ret; 276 std::string ret;
277 ret.resize(32); 277 ret.resize(32);
278 278
279 int j = 0; 279 for (int i = 0, j = 0; i < 16; i++, j += 2) {
280 for (int i = 0; i < 16; i++) {
281 int a = digest.a[i]; 280 int a = digest.a[i];
282 ret[j++] = zEncode[(a >> 4) & 0xf]; 281 ret[j] = zEncode[(a >> 4) & 0xf];
283 ret[j++] = zEncode[a & 0xf]; 282 ret[j + 1] = zEncode[a & 0xf];
284 } 283 }
285 return ret; 284 return ret;
286 } 285 }
287 286
288 void MD5Sum(const void* data, size_t length, MD5Digest* digest) { 287 void MD5Sum(const void* data, size_t length, MD5Digest* digest) {
289 MD5Context ctx; 288 MD5Context ctx;
290 MD5Init(&ctx); 289 MD5Init(&ctx);
291 MD5Update(&ctx, StringPiece(reinterpret_cast<const char*>(data), length)); 290 MD5Update(&ctx, StringPiece(reinterpret_cast<const char*>(data), length));
292 MD5Final(digest, &ctx); 291 MD5Final(digest, &ctx);
293 } 292 }
294 293
295 std::string MD5String(const StringPiece& str) { 294 std::string MD5String(const StringPiece& str) {
296 MD5Digest digest; 295 MD5Digest digest;
297 MD5Sum(str.data(), str.length(), &digest); 296 MD5Sum(str.data(), str.length(), &digest);
298 return MD5DigestToBase16(digest); 297 return MD5DigestToBase16(digest);
299 } 298 }
300 299
301 } // namespace base 300 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698