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 // Note that this treats the buffer as a series of uint8_t values. |
tomhudson
2016/04/22 13:35:59
Not sure this comment is helpful - it's sitting he
hal.canary
2016/04/22 14:13:15
Done.
| |
40 bool SkMD5::write(const void* buf, size_t inputLength) { | |
41 const uint8_t* input = reinterpret_cast<const uint8_t*>(buf); | |
36 unsigned int bufferIndex = (unsigned int)(this->byteCount & 0x3F); | 42 unsigned int bufferIndex = (unsigned int)(this->byteCount & 0x3F); |
37 unsigned int bufferAvailable = 64 - bufferIndex; | 43 unsigned int bufferAvailable = 64 - bufferIndex; |
38 | 44 |
39 unsigned int inputIndex; | 45 unsigned int inputIndex; |
40 if (inputLength >= bufferAvailable) { | 46 if (inputLength >= bufferAvailable) { |
41 if (bufferIndex) { | 47 if (bufferIndex) { |
42 memcpy(&this->buffer[bufferIndex], input, bufferAvailable); | 48 memcpy(&this->buffer[bufferIndex], input, bufferAvailable); |
43 transform(this->state, this->buffer); | 49 transform(this->state, this->buffer); |
44 inputIndex = bufferAvailable; | 50 inputIndex = bufferAvailable; |
45 } else { | 51 } else { |
46 inputIndex = 0; | 52 inputIndex = 0; |
47 } | 53 } |
48 | 54 |
49 for (; inputIndex + 63 < inputLength; inputIndex += 64) { | 55 for (; inputIndex + 63 < inputLength; inputIndex += 64) { |
50 transform(this->state, &input[inputIndex]); | 56 transform(this->state, &input[inputIndex]); |
51 } | 57 } |
52 | 58 |
53 bufferIndex = 0; | 59 bufferIndex = 0; |
54 } else { | 60 } else { |
55 inputIndex = 0; | 61 inputIndex = 0; |
56 } | 62 } |
57 | 63 |
58 memcpy(&this->buffer[bufferIndex], &input[inputIndex], inputLength - inputIn dex); | 64 memcpy(&this->buffer[bufferIndex], &input[inputIndex], inputLength - inputIn dex); |
59 | 65 |
60 this->byteCount += inputLength; | 66 this->byteCount += inputLength; |
67 return true; | |
61 } | 68 } |
62 | 69 |
63 void SkMD5::finish(Digest& digest) { | 70 void SkMD5::finish(Digest& digest) { |
64 // Get the number of bits before padding. | 71 // Get the number of bits before padding. |
65 uint8_t bits[8]; | 72 uint8_t bits[8]; |
66 encode(bits, this->byteCount << 3); | 73 encode(bits, this->byteCount << 3); |
67 | 74 |
68 // Pad out to 56 mod 64. | 75 // Pad out to 56 mod 64. |
69 unsigned int bufferIndex = (unsigned int)(this->byteCount & 0x3F); | 76 unsigned int bufferIndex = (unsigned int)(this->byteCount & 0x3F); |
70 unsigned int paddingLength = (bufferIndex < 56) ? (56 - bufferIndex) : (120 - bufferIndex); | 77 unsigned int paddingLength = (bufferIndex < 56) ? (56 - bufferIndex) : (120 - bufferIndex); |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
243 #endif | 250 #endif |
244 for (size_t i = 0, j = 0; j < 64; i++, j += 4) { | 251 for (size_t i = 0, j = 0; j < 64; i++, j += 4) { |
245 storage[i] = ((uint32_t)input[j ]) | | 252 storage[i] = ((uint32_t)input[j ]) | |
246 (((uint32_t)input[j+1]) << 8) | | 253 (((uint32_t)input[j+1]) << 8) | |
247 (((uint32_t)input[j+2]) << 16) | | 254 (((uint32_t)input[j+2]) << 16) | |
248 (((uint32_t)input[j+3]) << 24); | 255 (((uint32_t)input[j+3]) << 24); |
249 } | 256 } |
250 return storage; | 257 return storage; |
251 #endif | 258 #endif |
252 } | 259 } |
OLD | NEW |