TAY
Fork of Crypto_light by
Diff: MD5.cpp
- Revision:
- 6:19aa835f2bbb
- Parent:
- 5:06cd9c8afa0b
--- a/MD5.cpp Thu Sep 12 15:08:51 2013 +0000 +++ b/MD5.cpp Thu Sep 12 16:03:43 2013 +0000 @@ -50,33 +50,34 @@ return 16; } -void MD5::update(uint8_t *in, uint32_t length) +void MD5::update(uint8_t *data, uint32_t length) { if(length < 64-bufferLength) { - memcpy(&buffer[bufferLength], in, length); + memcpy(&buffer[bufferLength], data, length); bufferLength += length; totalBufferLength += length; return; } int offset = 64-bufferLength; - memcpy(&buffer[bufferLength], in, offset); + memcpy(&buffer[bufferLength], data, offset); computeRounds(&a, &b, &c, &d, buffer); while(length-offset > 64) { - memcpy(buffer, &in[offset], 64); + memcpy(buffer, &data[offset], 64); computeRounds(&a, &b, &c, &d, buffer); offset += 64; } if(offset > length) offset -= 64; bufferLength = length - offset; - memcpy(buffer, &in[offset], bufferLength); + memcpy(buffer, &data[offset], bufferLength); totalBufferLength += length; } -void MD5::finalize(uint8_t *digest) +void MD5::finalize(uint8_t *hash) { + uint32_t *hash2 = (uint32_t*)hash; uint16_t padding; if(totalBufferLength % 64 < 56) padding = 56 - (totalBufferLength % 64); @@ -99,10 +100,10 @@ memcpy(&buffer[60], &lengthBitHigh, 4); computeRounds(&a, &b, &c, &d, buffer); - memcpy(digest, &a, 4); - memcpy(&digest[4], &b, 4); - memcpy(&digest[8], &c, 4); - memcpy(&digest[12], &d, 4); + hash2[0] = a; + hash2[1] = b; + hash2[2] = c; + hash2[3] = d; // reset state a = A; b = B; @@ -112,9 +113,52 @@ bufferLength = 0; } + +void MD5::computeHash(uint8_t *hash, uint8_t *data, uint32_t length) +{ + uint32_t *hash2 = (uint32_t*)hash; + uint64_t lengthBit = length << 3; + uint16_t padding; + if(length % 64 < 56) + padding = 56 - (length % 64); + else + padding = 56 + (64 - (length % 64)); + + uint32_t a = A, b = B, c = C, d = D; + while(length >= 64) + { + computeRounds(&a, &b, &c, &d, data); + data += 64; + length -= 64; + } + uint8_t buffer[64]; + memcpy(buffer, data, length); + buffer[length++] = 0x80; + padding--; + if(padding+length == 56) + memset(&buffer[length], 0, padding); + else + { + memset(&buffer[length], 0, 64-length); + computeRounds(&a, &b, &c, &d, data); + memset(buffer, 0, 56); + } + + uint32_t lengthBitLow = lengthBit; + uint32_t lengthBitHigh = lengthBit >> 32; + memcpy(&buffer[56], &lengthBitLow, 4); + memcpy(&buffer[60], &lengthBitHigh, 4); + + computeRounds(&a, &b, &c, &d, buffer); + + hash2[0] = a; + hash2[1] = b; + hash2[2] = c; + hash2[3] = d; +} + void MD5::computeRounds(uint32_t *a2, uint32_t *b2, uint32_t *c2, uint32_t *d2, uint8_t *buffer) { - uint32_t a = *a2, b = *b2, c = *c2, d = *d2; uint32_t tmpA = a, tmpB = b, tmpC = c, tmpD = d; @@ -152,45 +196,3 @@ *c2 = c + tmpC; *d2 = d + tmpD; } - -void MD5::computeDigest(uint8_t *digest, uint8_t *msg, uint32_t length) -{ - uint64_t lengthBit = length << 3; - uint16_t padding; - if(length % 64 < 56) - padding = 56 - (length % 64); - else - padding = 56 + (64 - (length % 64)); - - uint32_t a = A, b = B, c = C, d = D; - while(length >= 64) - { - computeRounds(&a, &b, &c, &d, msg); - msg += 64; - length -= 64; - } - uint8_t buffer[64]; - memcpy(buffer, msg, length); - buffer[length++] = 0x80; - padding--; - if(padding+length == 56) - memset(&buffer[length], 0, padding); - else - { - memset(&buffer[length], 0, 64-length); - computeRounds(&a, &b, &c, &d, msg); - memset(buffer, 0, 56); - } - - uint32_t lengthBitLow = lengthBit; - uint32_t lengthBitHigh = lengthBit >> 32; - memcpy(&buffer[56], &lengthBitLow, 4); - memcpy(&buffer[60], &lengthBitHigh, 4); - - computeRounds(&a, &b, &c, &d, buffer); - - memcpy(digest, &a, 4); - memcpy(&digest[4], &b, 4); - memcpy(&digest[8], &c, 4); - memcpy(&digest[12], &d, 4); -} \ No newline at end of file