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. |
11 * 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. |
12 * | 12 * |
13 * Equivalent code is available from RSA Data Security, Inc. | 13 * Equivalent code is available from RSA Data Security, Inc. |
14 * This code has been tested against that, and is equivalent, | 14 * This code has been tested against that, and is equivalent, |
15 * 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 |
16 * with every copy. | 16 * with every copy. |
17 * | 17 * |
18 * To compute the message digest of a chunk of bytes, declare an | 18 * To compute the message digest of a chunk of bytes, declare an |
19 * MD5Context structure, pass it to MD5Init, call MD5Update as | 19 * MD5Context structure, pass it to MD5Init, call MD5Update as |
20 * needed on buffers full of bytes, and then call MD5Final, which | 20 * needed on buffers full of bytes, and then call MD5Final, which |
21 * will fill a supplied 16-byte array with the digest. | 21 * will fill a supplied 16-byte array with the digest. |
22 */ | 22 */ |
23 | 23 |
24 #include "base/md5.h" | 24 #include "base/md5.h" |
25 | 25 |
26 #include <stddef.h> | 26 #include <stddef.h> |
27 #include <stdint.h> | |
28 | 27 |
29 namespace { | 28 namespace { |
30 | 29 |
31 struct Context { | 30 struct Context { |
32 uint32_t buf[4]; | 31 uint32_t buf[4]; |
33 uint32_t bits[2]; | 32 uint32_t bits[2]; |
34 unsigned char in[64]; | 33 uint8_t in[64]; |
35 }; | 34 }; |
36 | 35 |
37 /* | 36 /* |
38 * Note: this code is harmless on little-endian machines. | 37 * Note: this code is harmless on little-endian machines. |
39 */ | 38 */ |
40 void byteReverse(unsigned char *buf, unsigned longs) { | 39 void byteReverse(uint8_t* buf, unsigned longs) { |
41 uint32_t t; | 40 uint32_t t; |
42 do { | 41 do { |
43 t = (uint32_t)((unsigned)buf[3] << 8 | buf[2]) << 16 | | 42 t = (uint32_t)((unsigned)buf[3] << 8 | buf[2]) << 16 | |
44 ((unsigned)buf[1] << 8 | buf[0]); | 43 ((unsigned)buf[1] << 8 | buf[0]); |
45 *(uint32_t*)buf = t; | 44 *(uint32_t*)buf = t; |
46 buf += 4; | 45 buf += 4; |
47 } while (--longs); | 46 } while (--longs); |
48 } | 47 } |
49 | 48 |
50 /* The four core functions - F1 is optimized somewhat */ | 49 /* The four core functions - F1 is optimized somewhat */ |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 ctx->buf[3] = 0x10325476; | 161 ctx->buf[3] = 0x10325476; |
163 ctx->bits[0] = 0; | 162 ctx->bits[0] = 0; |
164 ctx->bits[1] = 0; | 163 ctx->bits[1] = 0; |
165 } | 164 } |
166 | 165 |
167 /* | 166 /* |
168 * Update context to reflect the concatenation of another buffer full | 167 * Update context to reflect the concatenation of another buffer full |
169 * of bytes. | 168 * of bytes. |
170 */ | 169 */ |
171 void MD5Update(MD5Context* context, const StringPiece& data) { | 170 void MD5Update(MD5Context* context, const StringPiece& data) { |
172 const unsigned char* inbuf = (const unsigned char*)data.data(); | 171 const uint8_t* inbuf = (const uint8_t*)data.data(); |
173 size_t len = data.size(); | 172 size_t len = data.size(); |
174 struct Context* ctx = (struct Context*)context; | 173 struct Context* ctx = (struct Context*)context; |
175 const unsigned char* buf = (const unsigned char*)inbuf; | 174 const uint8_t* buf = (const uint8_t*)inbuf; |
176 uint32_t t; | 175 uint32_t t; |
177 | 176 |
178 /* Update bitcount */ | 177 /* Update bitcount */ |
179 | 178 |
180 t = ctx->bits[0]; | 179 t = ctx->bits[0]; |
181 if ((ctx->bits[0] = t + ((uint32_t)len << 3)) < t) | 180 if ((ctx->bits[0] = t + ((uint32_t)len << 3)) < t) |
182 ctx->bits[1]++; /* Carry from low to high */ | 181 ctx->bits[1]++; /* Carry from low to high */ |
183 ctx->bits[1] += static_cast<uint32_t>(len >> 29); | 182 ctx->bits[1] += static_cast<uint32_t>(len >> 29); |
184 | 183 |
185 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ | 184 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ |
186 | 185 |
187 /* Handle any leading odd-sized chunks */ | 186 /* Handle any leading odd-sized chunks */ |
188 | 187 |
189 if (t) { | 188 if (t) { |
190 unsigned char* p = (unsigned char*)ctx->in + t; | 189 uint8_t* p = (uint8_t*)ctx->in + t; |
191 | 190 |
192 t = 64 - t; | 191 t = 64 - t; |
193 if (len < t) { | 192 if (len < t) { |
194 memcpy(p, buf, len); | 193 memcpy(p, buf, len); |
195 return; | 194 return; |
196 } | 195 } |
197 memcpy(p, buf, t); | 196 memcpy(p, buf, t); |
198 byteReverse(ctx->in, 16); | 197 byteReverse(ctx->in, 16); |
199 MD5Transform(ctx->buf, (uint32_t*)ctx->in); | 198 MD5Transform(ctx->buf, (uint32_t*)ctx->in); |
200 buf += t; | 199 buf += t; |
(...skipping 15 matching lines...) Expand all Loading... |
216 memcpy(ctx->in, buf, len); | 215 memcpy(ctx->in, buf, len); |
217 } | 216 } |
218 | 217 |
219 /* | 218 /* |
220 * Final wrapup - pad to 64-byte boundary with the bit pattern | 219 * Final wrapup - pad to 64-byte boundary with the bit pattern |
221 * 1 0* (64-bit count of bits processed, MSB-first) | 220 * 1 0* (64-bit count of bits processed, MSB-first) |
222 */ | 221 */ |
223 void MD5Final(MD5Digest* digest, MD5Context* context) { | 222 void MD5Final(MD5Digest* digest, MD5Context* context) { |
224 struct Context* ctx = (struct Context*)context; | 223 struct Context* ctx = (struct Context*)context; |
225 unsigned count; | 224 unsigned count; |
226 unsigned char* p; | 225 uint8_t* p; |
227 | 226 |
228 /* Compute number of bytes mod 64 */ | 227 /* Compute number of bytes mod 64 */ |
229 count = (ctx->bits[0] >> 3) & 0x3F; | 228 count = (ctx->bits[0] >> 3) & 0x3F; |
230 | 229 |
231 /* Set the first char of padding to 0x80. This is safe since there is | 230 /* Set the first char of padding to 0x80. This is safe since there is |
232 always at least one byte free */ | 231 always at least one byte free */ |
233 p = ctx->in + count; | 232 p = ctx->in + count; |
234 *p++ = 0x80; | 233 *p++ = 0x80; |
235 | 234 |
236 /* Bytes of padding needed to make 64 bytes */ | 235 /* Bytes of padding needed to make 64 bytes */ |
(...skipping 14 matching lines...) Expand all Loading... |
251 } | 250 } |
252 byteReverse(ctx->in, 14); | 251 byteReverse(ctx->in, 14); |
253 | 252 |
254 /* Append length in bits and transform */ | 253 /* Append length in bits and transform */ |
255 memcpy(&ctx->in[14 * sizeof(ctx->bits[0])], &ctx->bits[0], | 254 memcpy(&ctx->in[14 * sizeof(ctx->bits[0])], &ctx->bits[0], |
256 sizeof(ctx->bits[0])); | 255 sizeof(ctx->bits[0])); |
257 memcpy(&ctx->in[15 * sizeof(ctx->bits[1])], &ctx->bits[1], | 256 memcpy(&ctx->in[15 * sizeof(ctx->bits[1])], &ctx->bits[1], |
258 sizeof(ctx->bits[1])); | 257 sizeof(ctx->bits[1])); |
259 | 258 |
260 MD5Transform(ctx->buf, (uint32_t*)ctx->in); | 259 MD5Transform(ctx->buf, (uint32_t*)ctx->in); |
261 byteReverse((unsigned char*)ctx->buf, 4); | 260 byteReverse((uint8_t*)ctx->buf, 4); |
262 memcpy(digest->a, ctx->buf, 16); | 261 memcpy(digest->a, ctx->buf, 16); |
263 memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */ | 262 memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */ |
264 } | 263 } |
265 | 264 |
266 void MD5IntermediateFinal(MD5Digest* digest, const MD5Context* context) { | 265 void MD5IntermediateFinal(MD5Digest* digest, const MD5Context* context) { |
267 /* MD5Final mutates the MD5Context*. Make a copy for generating the | 266 /* MD5Final mutates the MD5Context*. Make a copy for generating the |
268 intermediate value. */ | 267 intermediate value. */ |
269 MD5Context context_copy; | 268 MD5Context context_copy; |
270 memcpy(&context_copy, context, sizeof(context_copy)); | 269 memcpy(&context_copy, context, sizeof(context_copy)); |
271 MD5Final(digest, &context_copy); | 270 MD5Final(digest, &context_copy); |
(...skipping 21 matching lines...) Expand all Loading... |
293 MD5Final(digest, &ctx); | 292 MD5Final(digest, &ctx); |
294 } | 293 } |
295 | 294 |
296 std::string MD5String(const StringPiece& str) { | 295 std::string MD5String(const StringPiece& str) { |
297 MD5Digest digest; | 296 MD5Digest digest; |
298 MD5Sum(str.data(), str.length(), &digest); | 297 MD5Sum(str.data(), str.length(), &digest); |
299 return MD5DigestToBase16(digest); | 298 return MD5DigestToBase16(digest); |
300 } | 299 } |
301 | 300 |
302 } // namespace base | 301 } // namespace base |
OLD | NEW |