OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 * | 6 * |
7 * The following code is based on the description in RFC 1321. | 7 * The following code is based on the description in RFC 1321. |
8 * http://www.ietf.org/rfc/rfc1321.txt | 8 * http://www.ietf.org/rfc/rfc1321.txt |
9 */ | 9 */ |
10 | 10 |
11 #include "SkTypes.h" | 11 //The following macros can be defined to affect the MD5 code generated. |
| 12 //SK_MD5_CLEAR_DATA causes all intermediate state to be overwritten with 0's. |
| 13 //SK_CPU_LENDIAN allows 32 bit <=> 8 bit conversions without copies (if alligned
). |
| 14 //SK_CPU_FAST_UNALIGNED_ACCESS allows 32 bit <=> 8 bit conversions without copie
s if SK_CPU_LENDIAN. |
| 15 |
12 #include "SkMD5.h" | 16 #include "SkMD5.h" |
13 #include <string.h> | 17 #include <string.h> |
14 | 18 |
15 /** MD5 basic transformation. Transforms state based on block. */ | 19 /** MD5 basic transformation. Transforms state based on block. */ |
16 static void transform(uint32_t state[4], const uint8_t block[64]); | 20 static void transform(uint32_t state[4], const uint8_t block[64]); |
17 | 21 |
18 /** Encodes input into output (4 little endian 32 bit values). */ | 22 /** Encodes input into output (4 little endian 32 bit values). */ |
19 static void encode(uint8_t output[16], const uint32_t input[4]); | 23 static void encode(uint8_t output[16], const uint32_t input[4]); |
20 | 24 |
21 /** Encodes input into output (little endian 64 bit value). */ | 25 /** Encodes input into output (little endian 64 bit value). */ |
22 static void encode(uint8_t output[8], const uint64_t input); | 26 static void encode(uint8_t output[8], const uint64_t input); |
23 | 27 |
24 /** Decodes input (4 little endian 32 bit values) into storage, if required. */ | 28 /** Decodes input (4 little endian 32 bit values) into storage, if required. */ |
25 static const uint32_t* decode(uint32_t storage[16], const uint8_t input[64]); | 29 static const uint32_t* decode(uint32_t storage[16], const uint8_t input[64]); |
26 | 30 |
27 SkMD5::SkMD5() : byteCount(0) { | 31 SkMD5::SkMD5() : byteCount(0) { |
28 // These are magic numbers from the specification. | 32 // These are magic numbers from the specification. |
29 this->state[0] = 0x67452301; | 33 this->state[0] = 0x67452301; |
30 this->state[1] = 0xefcdab89; | 34 this->state[1] = 0xefcdab89; |
31 this->state[2] = 0x98badcfe; | 35 this->state[2] = 0x98badcfe; |
32 this->state[3] = 0x10325476; | 36 this->state[3] = 0x10325476; |
33 } | 37 } |
34 | 38 |
35 void SkMD5::update(const uint8_t* input, size_t inputLength) { | 39 bool SkMD5::write(const void* buf, size_t inputLength) { |
| 40 const uint8_t* input = reinterpret_cast<const uint8_t*>(buf); |
36 unsigned int bufferIndex = (unsigned int)(this->byteCount & 0x3F); | 41 unsigned int bufferIndex = (unsigned int)(this->byteCount & 0x3F); |
37 unsigned int bufferAvailable = 64 - bufferIndex; | 42 unsigned int bufferAvailable = 64 - bufferIndex; |
38 | 43 |
39 unsigned int inputIndex; | 44 unsigned int inputIndex; |
40 if (inputLength >= bufferAvailable) { | 45 if (inputLength >= bufferAvailable) { |
41 if (bufferIndex) { | 46 if (bufferIndex) { |
42 memcpy(&this->buffer[bufferIndex], input, bufferAvailable); | 47 memcpy(&this->buffer[bufferIndex], input, bufferAvailable); |
43 transform(this->state, this->buffer); | 48 transform(this->state, this->buffer); |
44 inputIndex = bufferAvailable; | 49 inputIndex = bufferAvailable; |
45 } else { | 50 } else { |
46 inputIndex = 0; | 51 inputIndex = 0; |
47 } | 52 } |
48 | 53 |
49 for (; inputIndex + 63 < inputLength; inputIndex += 64) { | 54 for (; inputIndex + 63 < inputLength; inputIndex += 64) { |
50 transform(this->state, &input[inputIndex]); | 55 transform(this->state, &input[inputIndex]); |
51 } | 56 } |
52 | 57 |
53 bufferIndex = 0; | 58 bufferIndex = 0; |
54 } else { | 59 } else { |
55 inputIndex = 0; | 60 inputIndex = 0; |
56 } | 61 } |
57 | 62 |
58 memcpy(&this->buffer[bufferIndex], &input[inputIndex], inputLength - inputIn
dex); | 63 memcpy(&this->buffer[bufferIndex], &input[inputIndex], inputLength - inputIn
dex); |
59 | 64 |
60 this->byteCount += inputLength; | 65 this->byteCount += inputLength; |
| 66 return true; |
61 } | 67 } |
62 | 68 |
63 void SkMD5::finish(Digest& digest) { | 69 void SkMD5::finish(Digest& digest) { |
64 // Get the number of bits before padding. | 70 // Get the number of bits before padding. |
65 uint8_t bits[8]; | 71 uint8_t bits[8]; |
66 encode(bits, this->byteCount << 3); | 72 encode(bits, this->byteCount << 3); |
67 | 73 |
68 // Pad out to 56 mod 64. | 74 // Pad out to 56 mod 64. |
69 unsigned int bufferIndex = (unsigned int)(this->byteCount & 0x3F); | 75 unsigned int bufferIndex = (unsigned int)(this->byteCount & 0x3F); |
70 unsigned int paddingLength = (bufferIndex < 56) ? (56 - bufferIndex) : (120
- bufferIndex); | 76 unsigned int paddingLength = (bufferIndex < 56) ? (56 - bufferIndex) : (120
- bufferIndex); |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 #endif | 249 #endif |
244 for (size_t i = 0, j = 0; j < 64; i++, j += 4) { | 250 for (size_t i = 0, j = 0; j < 64; i++, j += 4) { |
245 storage[i] = ((uint32_t)input[j ]) | | 251 storage[i] = ((uint32_t)input[j ]) | |
246 (((uint32_t)input[j+1]) << 8) | | 252 (((uint32_t)input[j+1]) << 8) | |
247 (((uint32_t)input[j+2]) << 16) | | 253 (((uint32_t)input[j+2]) << 16) | |
248 (((uint32_t)input[j+3]) << 24); | 254 (((uint32_t)input[j+3]) << 24); |
249 } | 255 } |
250 return storage; | 256 return storage; |
251 #endif | 257 #endif |
252 } | 258 } |
OLD | NEW |