This library implements some hash and cryptographic algorithms.
Dependents: ES_CW2_Starter_JIN EMBEDDED_CW2 EMBEDDED_CW2_Final Spinnybois ... more
Fork of Crypto by
Diff: SHA1.cpp
- Revision:
- 6:19aa835f2bbb
- Parent:
- 5:06cd9c8afa0b
--- a/SHA1.cpp Thu Sep 12 15:08:51 2013 +0000 +++ b/SHA1.cpp Thu Sep 12 16:03:43 2013 +0000 @@ -59,34 +59,34 @@ return 20; } -void SHA1::update(uint8_t *in, uint32_t length) +void SHA1::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); computeBlock(&h0,&h1,&h2,&h3,&h4, buffer); while(length-offset > 64) { - memcpy(buffer, &in[offset], 64); + memcpy(buffer, &data[offset], 64); computeBlock(&h0,&h1,&h2,&h3,&h4, buffer); offset += 64; } if(offset > length) offset -= 64; bufferLength = length - offset; - memcpy(buffer, &in[offset], bufferLength); + memcpy(buffer, &data[offset], bufferLength); totalBufferLength += length; } -void SHA1::finalize(uint8_t *digest) +void SHA1::finalize(uint8_t *hash) { - uint32_t *digest2 = (uint32_t*)digest; + uint32_t *hash2 = (uint32_t*)hash; uint16_t padding; if(totalBufferLength % 64 < 56) padding = 56 - (totalBufferLength % 64); @@ -113,11 +113,11 @@ memcpy(&buffer[60], &lengthBitLow, 4); computeBlock(&h0,&h1,&h2,&h3,&h4, buffer); - digest2[0] = __rev(h0); - digest2[1] = __rev(h1); - digest2[2] = __rev(h2); - digest2[3] = __rev(h3); - digest2[4] = __rev(h4); + hash2[0] = __rev(h0); + hash2[1] = __rev(h1); + hash2[2] = __rev(h2); + hash2[3] = __rev(h3); + hash2[4] = __rev(h4); // reset state h0 = H0; @@ -129,6 +129,54 @@ bufferLength = 0; } + +void SHA1::computeHash(uint8_t *hash, uint8_t *data, uint32_t length) +{ + uint32_t *hash2 = (uint32_t*)hash; + uint64_t lengthBit = length << 3; + uint32_t padding; + if(length % 64 < 56) + padding = 56 - (length % 64); + else + padding = 56 + (64 - (length % 64)); + + uint32_t h0 = H0, h1 = H1, h2 = H2, h3 = H3, h4 = H4; + while(length >= 64) + { + computeBlock(&h0,&h1,&h2,&h3,&h4, data); + length -= 64; + data += 64; + } + + uint8_t buffer[64]; + memcpy(buffer, data, length); + buffer[length++] = 0x80; + padding--; + if(padding+length+8 == 64) + memset(&buffer[length], 0, padding); + else + { + memset(&buffer[length], 0, 64-length); + computeBlock(&h0,&h1,&h2,&h3,&h4, buffer); + memset(buffer, 0, 56); + } + + uint32_t lengthBitLow = lengthBit; + uint32_t lengthBitHigh = lengthBit >> 32; + lengthBitLow = __rev(lengthBitLow); + lengthBitHigh = __rev(lengthBitHigh); + memcpy(&buffer[60], &lengthBitLow, 4); + memcpy(&buffer[56], &lengthBitHigh, 4); + + computeBlock(&h0,&h1,&h2,&h3,&h4, buffer); + + hash2[0] = __rev(h0); + hash2[1] = __rev(h1); + hash2[2] = __rev(h2); + hash2[3] = __rev(h3); + hash2[4] = __rev(h4); +} + void SHA1::computeBlock(uint32_t *h02, uint32_t *h12, uint32_t *h22, uint32_t *h32, uint32_t *h42, uint8_t *buffer) { uint32_t *buffer2 = (uint32_t*)buffer; @@ -171,58 +219,3 @@ *h42 += e; } - - -/* method 1 */ -void SHA1::computeDigest(uint8_t *digest, uint8_t *in, uint32_t length) -{ - uint64_t lengthBit = length << 3; - uint32_t padding; - if(length % 64 < 56) - padding = 56 - (length % 64); - else - padding = 56 + (64 - (length % 64)); - - uint32_t h0 = H0, h1 = H1, h2 = H2, h3 = H3, h4 = H4; - while(length >= 64) - { - computeBlock(&h0,&h1,&h2,&h3,&h4, in); - length -= 64; - in += 64; - } - - uint8_t buffer[64]; - memcpy(buffer, in, length); - buffer[length++] = 0x80; - padding--; - if(padding+length+8 == 64) - memset(&buffer[length], 0, padding); - else - { - memset(&buffer[length], 0, 64-length); - computeBlock(&h0,&h1,&h2,&h3,&h4, buffer); - memset(buffer, 0, 56); - } - - uint32_t lengthBitLow = lengthBit; - uint32_t lengthBitHigh = lengthBit >> 32; - lengthBitLow = __rev(lengthBitLow); - lengthBitHigh = __rev(lengthBitHigh); - memcpy(&buffer[60], &lengthBitLow, 4); - memcpy(&buffer[56], &lengthBitHigh, 4); - - computeBlock(&h0,&h1,&h2,&h3,&h4, buffer); - - h0 = __rev(h0); - h1 = __rev(h1); - h2 = __rev(h2); - h3 = __rev(h3); - h4 = __rev(h4); - - memcpy(digest, &h0, 4); - memcpy(&digest[4], &h1, 4); - memcpy(&digest[8], &h2, 4); - memcpy(&digest[12], &h3, 4); - memcpy(&digest[16], &h4, 4); -} -