| 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. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 struct Context { | 30 struct Context { |
| 31 uint32_t buf[4]; | 31 uint32_t buf[4]; |
| 32 uint32_t bits[2]; | 32 uint32_t bits[2]; |
| 33 uint8_t in[64]; | 33 uint8_t in[64]; |
| 34 }; | 34 }; |
| 35 | 35 |
| 36 /* | 36 /* |
| 37 * Note: this code is harmless on little-endian machines. | 37 * Note: this code is harmless on little-endian machines. |
| 38 */ | 38 */ |
| 39 void byteReverse(uint8_t* buf, unsigned longs) { | 39 void byteReverse(uint8_t* buf, unsigned longs) { |
| 40 uint32_t t; | |
| 41 do { | 40 do { |
| 42 t = (uint32_t)((unsigned)buf[3] << 8 | buf[2]) << 16 | | 41 uint32_t temp = static_cast<uint32_t>( |
| 43 ((unsigned)buf[1] << 8 | buf[0]); | 42 static_cast<unsigned>(buf[3]) << 8 | |
| 44 *(uint32_t*)buf = t; | 43 buf[2]) << 16 | |
| 44 (static_cast<unsigned>(buf[1]) << 8 | buf[0]); |
| 45 *reinterpret_cast<uint32_t*>(buf) = temp; |
| 45 buf += 4; | 46 buf += 4; |
| 46 } while (--longs); | 47 } while (--longs); |
| 47 } | 48 } |
| 48 | 49 |
| 49 /* The four core functions - F1 is optimized somewhat */ | 50 /* The four core functions - F1 is optimized somewhat */ |
| 50 | 51 |
| 51 /* #define F1(x, y, z) (x & y | ~x & z) */ | 52 /* #define F1(x, y, z) (x & y | ~x & z) */ |
| 52 #define F1(x, y, z) (z ^ (x & (y ^ z))) | 53 #define F1(x, y, z) (z ^ (x & (y ^ z))) |
| 53 #define F2(x, y, z) F1(z, x, y) | 54 #define F2(x, y, z) F1(z, x, y) |
| 54 #define F3(x, y, z) (x ^ y ^ z) | 55 #define F3(x, y, z) (x ^ y ^ z) |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 | 148 |
| 148 } // namespace | 149 } // namespace |
| 149 | 150 |
| 150 namespace base { | 151 namespace base { |
| 151 | 152 |
| 152 /* | 153 /* |
| 153 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious | 154 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious |
| 154 * initialization constants. | 155 * initialization constants. |
| 155 */ | 156 */ |
| 156 void MD5Init(MD5Context* context) { | 157 void MD5Init(MD5Context* context) { |
| 157 struct Context* ctx = (struct Context*)context; | 158 struct Context* ctx = reinterpret_cast<struct Context*>(context); |
| 158 ctx->buf[0] = 0x67452301; | 159 ctx->buf[0] = 0x67452301; |
| 159 ctx->buf[1] = 0xefcdab89; | 160 ctx->buf[1] = 0xefcdab89; |
| 160 ctx->buf[2] = 0x98badcfe; | 161 ctx->buf[2] = 0x98badcfe; |
| 161 ctx->buf[3] = 0x10325476; | 162 ctx->buf[3] = 0x10325476; |
| 162 ctx->bits[0] = 0; | 163 ctx->bits[0] = 0; |
| 163 ctx->bits[1] = 0; | 164 ctx->bits[1] = 0; |
| 164 } | 165 } |
| 165 | 166 |
| 166 /* | 167 /* |
| 167 * Update context to reflect the concatenation of another buffer full | 168 * Update context to reflect the concatenation of another buffer full |
| 168 * of bytes. | 169 * of bytes. |
| 169 */ | 170 */ |
| 170 void MD5Update(MD5Context* context, const StringPiece& data) { | 171 void MD5Update(MD5Context* context, const StringPiece& data) { |
| 171 struct Context* ctx = (struct Context*)context; | 172 struct Context* ctx = reinterpret_cast<struct Context*>(context); |
| 172 const uint8_t* buf = (const uint8_t*)data.data(); | 173 const uint8_t* buf = reinterpret_cast<const uint8_t*>(data.data()); |
| 173 size_t len = data.size(); | 174 size_t len = data.size(); |
| 174 | 175 |
| 175 /* Update bitcount */ | 176 /* Update bitcount */ |
| 176 | 177 |
| 177 uint32_t t = ctx->bits[0]; | 178 uint32_t t = ctx->bits[0]; |
| 178 if ((ctx->bits[0] = t + ((uint32_t)len << 3)) < t) | 179 if ((ctx->bits[0] = t + (static_cast<uint32_t>(len) << 3)) < t) |
| 179 ctx->bits[1]++; /* Carry from low to high */ | 180 ctx->bits[1]++; /* Carry from low to high */ |
| 180 ctx->bits[1] += static_cast<uint32_t>(len >> 29); | 181 ctx->bits[1] += static_cast<uint32_t>(len >> 29); |
| 181 | 182 |
| 182 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ | 183 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ |
| 183 | 184 |
| 184 /* Handle any leading odd-sized chunks */ | 185 /* Handle any leading odd-sized chunks */ |
| 185 | 186 |
| 186 if (t) { | 187 if (t) { |
| 187 uint8_t* p = (uint8_t*)ctx->in + t; | 188 uint8_t* p = static_cast<uint8_t*>(ctx->in + t); |
| 188 | 189 |
| 189 t = 64 - t; | 190 t = 64 - t; |
| 190 if (len < t) { | 191 if (len < t) { |
| 191 memcpy(p, buf, len); | 192 memcpy(p, buf, len); |
| 192 return; | 193 return; |
| 193 } | 194 } |
| 194 memcpy(p, buf, t); | 195 memcpy(p, buf, t); |
| 195 byteReverse(ctx->in, 16); | 196 byteReverse(ctx->in, 16); |
| 196 MD5Transform(ctx->buf, (uint32_t*)ctx->in); | 197 MD5Transform(ctx->buf, reinterpret_cast<uint32_t*>(ctx->in)); |
| 197 buf += t; | 198 buf += t; |
| 198 len -= t; | 199 len -= t; |
| 199 } | 200 } |
| 200 | 201 |
| 201 /* Process data in 64-byte chunks */ | 202 /* Process data in 64-byte chunks */ |
| 202 | 203 |
| 203 while (len >= 64) { | 204 while (len >= 64) { |
| 204 memcpy(ctx->in, buf, 64); | 205 memcpy(ctx->in, buf, 64); |
| 205 byteReverse(ctx->in, 16); | 206 byteReverse(ctx->in, 16); |
| 206 MD5Transform(ctx->buf, (uint32_t*)ctx->in); | 207 MD5Transform(ctx->buf, reinterpret_cast<uint32_t*>(ctx->in)); |
| 207 buf += 64; | 208 buf += 64; |
| 208 len -= 64; | 209 len -= 64; |
| 209 } | 210 } |
| 210 | 211 |
| 211 /* Handle any remaining bytes of data. */ | 212 /* Handle any remaining bytes of data. */ |
| 212 | 213 |
| 213 memcpy(ctx->in, buf, len); | 214 memcpy(ctx->in, buf, len); |
| 214 } | 215 } |
| 215 | 216 |
| 216 /* | 217 /* |
| 217 * Final wrapup - pad to 64-byte boundary with the bit pattern | 218 * Final wrapup - pad to 64-byte boundary with the bit pattern |
| 218 * 1 0* (64-bit count of bits processed, MSB-first) | 219 * 1 0* (64-bit count of bits processed, MSB-first) |
| 219 */ | 220 */ |
| 220 void MD5Final(MD5Digest* digest, MD5Context* context) { | 221 void MD5Final(MD5Digest* digest, MD5Context* context) { |
| 221 struct Context* ctx = (struct Context*)context; | 222 struct Context* ctx = reinterpret_cast<struct Context*>(context); |
| 222 unsigned count; | 223 unsigned count; |
| 223 uint8_t* p; | 224 uint8_t* p; |
| 224 | 225 |
| 225 /* Compute number of bytes mod 64 */ | 226 /* Compute number of bytes mod 64 */ |
| 226 count = (ctx->bits[0] >> 3) & 0x3F; | 227 count = (ctx->bits[0] >> 3) & 0x3F; |
| 227 | 228 |
| 228 /* Set the first char of padding to 0x80. This is safe since there is | 229 /* Set the first char of padding to 0x80. This is safe since there is |
| 229 always at least one byte free */ | 230 always at least one byte free */ |
| 230 p = ctx->in + count; | 231 p = ctx->in + count; |
| 231 *p++ = 0x80; | 232 *p++ = 0x80; |
| 232 | 233 |
| 233 /* Bytes of padding needed to make 64 bytes */ | 234 /* Bytes of padding needed to make 64 bytes */ |
| 234 count = 64 - 1 - count; | 235 count = 64 - 1 - count; |
| 235 | 236 |
| 236 /* Pad out to 56 mod 64 */ | 237 /* Pad out to 56 mod 64 */ |
| 237 if (count < 8) { | 238 if (count < 8) { |
| 238 /* Two lots of padding: Pad the first block to 64 bytes */ | 239 /* Two lots of padding: Pad the first block to 64 bytes */ |
| 239 memset(p, 0, count); | 240 memset(p, 0, count); |
| 240 byteReverse(ctx->in, 16); | 241 byteReverse(ctx->in, 16); |
| 241 MD5Transform(ctx->buf, (uint32_t*)ctx->in); | 242 MD5Transform(ctx->buf, reinterpret_cast<uint32_t*>(ctx->in)); |
| 242 | 243 |
| 243 /* Now fill the next block with 56 bytes */ | 244 /* Now fill the next block with 56 bytes */ |
| 244 memset(ctx->in, 0, 56); | 245 memset(ctx->in, 0, 56); |
| 245 } else { | 246 } else { |
| 246 /* Pad block to 56 bytes */ | 247 /* Pad block to 56 bytes */ |
| 247 memset(p, 0, count - 8); | 248 memset(p, 0, count - 8); |
| 248 } | 249 } |
| 249 byteReverse(ctx->in, 14); | 250 byteReverse(ctx->in, 14); |
| 250 | 251 |
| 251 /* Append length in bits and transform */ | 252 /* Append length in bits and transform */ |
| 252 memcpy(&ctx->in[14 * sizeof(ctx->bits[0])], &ctx->bits[0], | 253 memcpy(&ctx->in[14 * sizeof(ctx->bits[0])], &ctx->bits[0], |
| 253 sizeof(ctx->bits[0])); | 254 sizeof(ctx->bits[0])); |
| 254 memcpy(&ctx->in[15 * sizeof(ctx->bits[1])], &ctx->bits[1], | 255 memcpy(&ctx->in[15 * sizeof(ctx->bits[1])], &ctx->bits[1], |
| 255 sizeof(ctx->bits[1])); | 256 sizeof(ctx->bits[1])); |
| 256 | 257 |
| 257 MD5Transform(ctx->buf, (uint32_t*)ctx->in); | 258 MD5Transform(ctx->buf, reinterpret_cast<uint32_t*>(ctx->in)); |
| 258 byteReverse((uint8_t*)ctx->buf, 4); | 259 byteReverse(reinterpret_cast<uint8_t*>(ctx->buf), 4); |
| 259 memcpy(digest->a, ctx->buf, 16); | 260 memcpy(digest->a, ctx->buf, 16); |
| 260 memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */ | 261 memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */ |
| 261 } | 262 } |
| 262 | 263 |
| 263 void MD5IntermediateFinal(MD5Digest* digest, const MD5Context* context) { | 264 void MD5IntermediateFinal(MD5Digest* digest, const MD5Context* context) { |
| 264 /* MD5Final mutates the MD5Context*. Make a copy for generating the | 265 /* MD5Final mutates the MD5Context*. Make a copy for generating the |
| 265 intermediate value. */ | 266 intermediate value. */ |
| 266 MD5Context context_copy; | 267 MD5Context context_copy; |
| 267 memcpy(&context_copy, context, sizeof(context_copy)); | 268 memcpy(&context_copy, context, sizeof(context_copy)); |
| 268 MD5Final(digest, &context_copy); | 269 MD5Final(digest, &context_copy); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 289 MD5Final(digest, &ctx); | 290 MD5Final(digest, &ctx); |
| 290 } | 291 } |
| 291 | 292 |
| 292 std::string MD5String(const StringPiece& str) { | 293 std::string MD5String(const StringPiece& str) { |
| 293 MD5Digest digest; | 294 MD5Digest digest; |
| 294 MD5Sum(str.data(), str.length(), &digest); | 295 MD5Sum(str.data(), str.length(), &digest); |
| 295 return MD5DigestToBase16(digest); | 296 return MD5DigestToBase16(digest); |
| 296 } | 297 } |
| 297 | 298 |
| 298 } // namespace base | 299 } // namespace base |
| OLD | NEW |