OLD | NEW |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
1 // 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. |
2 // Modifications Copyright 2006 Google Inc. All Rights Reserved | |
3 | 6 |
4 /* | 7 /* |
5 * This code implements the MD5 message-digest algorithm. | 8 * This code implements the MD5 message-digest algorithm. |
6 * The algorithm is due to Ron Rivest. This code was | 9 * The algorithm is due to Ron Rivest. This code was |
7 * written by Colin Plumb in 1993, no copyright is claimed. | 10 * written by Colin Plumb in 1993, no copyright is claimed. |
8 * This code is in the public domain; do with it what you wish. | 11 * This code is in the public domain; do with it what you wish. |
9 * | 12 * |
10 * Equivalent code is available from RSA Data Security, Inc. | 13 * Equivalent code is available from RSA Data Security, Inc. |
11 * This code has been tested against that, and is equivalent, | 14 * This code has been tested against that, and is equivalent, |
12 * except that you don't need to include two pages of legalese | 15 * except that you don't need to include two pages of legalese |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 ctx->buf[2] = 0x98badcfe; | 160 ctx->buf[2] = 0x98badcfe; |
158 ctx->buf[3] = 0x10325476; | 161 ctx->buf[3] = 0x10325476; |
159 ctx->bits[0] = 0; | 162 ctx->bits[0] = 0; |
160 ctx->bits[1] = 0; | 163 ctx->bits[1] = 0; |
161 } | 164 } |
162 | 165 |
163 /* | 166 /* |
164 * Update context to reflect the concatenation of another buffer full | 167 * Update context to reflect the concatenation of another buffer full |
165 * of bytes. | 168 * of bytes. |
166 */ | 169 */ |
167 void MD5Update(MD5Context* context, const void* inbuf, size_t len) { | 170 void MD5Update(MD5Context* context, const StringPiece& data) { |
| 171 const unsigned char* inbuf = (const unsigned char*)data.data(); |
| 172 size_t len = data.size(); |
168 struct Context *ctx = (struct Context *)context; | 173 struct Context *ctx = (struct Context *)context; |
169 const unsigned char* buf = (const unsigned char*)inbuf; | 174 const unsigned char* buf = (const unsigned char*)inbuf; |
170 uint32 t; | 175 uint32 t; |
171 | 176 |
172 /* Update bitcount */ | 177 /* Update bitcount */ |
173 | 178 |
174 t = ctx->bits[0]; | 179 t = ctx->bits[0]; |
175 if ((ctx->bits[0] = t + ((uint32)len << 3)) < t) | 180 if ((ctx->bits[0] = t + ((uint32)len << 3)) < t) |
176 ctx->bits[1]++; /* Carry from low to high */ | 181 ctx->bits[1]++; /* Carry from low to high */ |
177 ctx->bits[1] += static_cast<uint32>(len >> 29); | 182 ctx->bits[1] += static_cast<uint32>(len >> 29); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 int a = digest.a[i]; | 271 int a = digest.a[i]; |
267 ret[j++] = zEncode[(a>>4)&0xf]; | 272 ret[j++] = zEncode[(a>>4)&0xf]; |
268 ret[j++] = zEncode[a & 0xf]; | 273 ret[j++] = zEncode[a & 0xf]; |
269 } | 274 } |
270 return ret; | 275 return ret; |
271 } | 276 } |
272 | 277 |
273 void MD5Sum(const void* data, size_t length, MD5Digest* digest) { | 278 void MD5Sum(const void* data, size_t length, MD5Digest* digest) { |
274 MD5Context ctx; | 279 MD5Context ctx; |
275 MD5Init(&ctx); | 280 MD5Init(&ctx); |
276 MD5Update(&ctx, static_cast<const unsigned char*>(data), length); | 281 MD5Update(&ctx, |
| 282 StringPiece(reinterpret_cast<const char*>(data), length)); |
277 MD5Final(digest, &ctx); | 283 MD5Final(digest, &ctx); |
278 } | 284 } |
279 | 285 |
280 std::string MD5String(const std::string& str) { | 286 std::string MD5String(const StringPiece& str) { |
281 MD5Digest digest; | 287 MD5Digest digest; |
282 MD5Sum(str.data(), str.length(), &digest); | 288 MD5Sum(str.data(), str.length(), &digest); |
283 return MD5DigestToBase16(digest); | 289 return MD5DigestToBase16(digest); |
284 } | 290 } |
285 | 291 |
286 } // namespace base | 292 } // namespace base |
OLD | NEW |