25 #include "utility/RotateUtil.h"
26 #include "utility/EndianUtil.h"
66 state.h[0] = 0x67452301;
67 state.h[1] = 0xEFCDAB89;
68 state.h[2] = 0x98BADCFE;
69 state.h[3] = 0x10325476;
70 state.h[4] = 0xC3D2E1F0;
78 state.length += ((uint64_t)len) << 3;
81 const uint8_t *d = (
const uint8_t *)data;
83 uint8_t size = 64 - state.chunkSize;
86 memcpy(((uint8_t *)state.w) + state.chunkSize, d, size);
87 state.chunkSize += size;
90 if (state.chunkSize == 64) {
101 uint8_t *wbytes = (uint8_t *)state.w;
102 if (state.chunkSize <= (64 - 9)) {
103 wbytes[state.chunkSize] = 0x80;
104 memset(wbytes + state.chunkSize + 1, 0x00, 64 - 8 - (state.chunkSize + 1));
105 state.w[14] = htobe32((uint32_t)(state.length >> 32));
106 state.w[15] = htobe32((uint32_t)state.length);
109 wbytes[state.chunkSize] = 0x80;
110 memset(wbytes + state.chunkSize + 1, 0x00, 64 - (state.chunkSize + 1));
112 memset(wbytes, 0x00, 64 - 8);
113 state.w[14] = htobe32((uint32_t)(state.length >> 32));
114 state.w[15] = htobe32((uint32_t)state.length);
119 for (uint8_t posn = 0; posn < 5; ++posn)
120 state.w[posn] = htobe32(state.h[posn]);
125 memcpy(hash, state.w, len);
137 state.length += 64 * 8;
146 state.length += 64 * 8;
148 update(temp,
sizeof(temp));
158 void SHA1::processChunk()
163 for (index = 0; index < 16; ++index)
164 state.w[index] = be32toh(state.w[index]);
167 uint32_t a = state.h[0];
168 uint32_t b = state.h[1];
169 uint32_t c = state.h[2];
170 uint32_t d = state.h[3];
171 uint32_t e = state.h[4];
175 for (index = 0; index < 16; ++index) {
176 temp = leftRotate5(a) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + state.w[index];
187 for (; index < 20; ++index) {
188 temp = state.w[index & 0x0F] = leftRotate1
189 (state.w[(index - 3) & 0x0F] ^ state.w[(index - 8) & 0x0F] ^
190 state.w[(index - 14) & 0x0F] ^ state.w[(index - 16) & 0x0F]);
191 temp = leftRotate5(a) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + temp;
198 for (; index < 40; ++index) {
199 temp = state.w[index & 0x0F] = leftRotate1
200 (state.w[(index - 3) & 0x0F] ^ state.w[(index - 8) & 0x0F] ^
201 state.w[(index - 14) & 0x0F] ^ state.w[(index - 16) & 0x0F]);
202 temp = leftRotate5(a) + (b ^ c ^ d) + e + 0x6ED9EBA1 + temp;
209 for (; index < 60; ++index) {
210 temp = state.w[index & 0x0F] = leftRotate1
211 (state.w[(index - 3) & 0x0F] ^ state.w[(index - 8) & 0x0F] ^
212 state.w[(index - 14) & 0x0F] ^ state.w[(index - 16) & 0x0F]);
213 temp = leftRotate5(a) + ((b & c) | (b & d) | (c & d)) + e + 0x8F1BBCDC + temp;
220 for (; index < 80; ++index) {
221 temp = state.w[index & 0x0F] = leftRotate1
222 (state.w[(index - 3) & 0x0F] ^ state.w[(index - 8) & 0x0F] ^
223 state.w[(index - 14) & 0x0F] ^ state.w[(index - 16) & 0x0F]);
224 temp = leftRotate5(a) + (b ^ c ^ d) + e + 0xCA62C1D6 + temp;
240 a = b = c = d = e = temp = 0;
void resetHMAC(const void *key, size_t keyLen)
Resets the hash ready for a new HMAC hashing process.
virtual ~SHA1()
Destroys this SHA-1 hash object after clearing sensitive information.
void reset()
Resets the hash ready for a new hashing process.
void update(const void *data, size_t len)
Updates the hash with more data.
void clear()
Clears the hash state, removing all sensitive data, and then resets the hash ready for a new hashing ...
size_t blockSize() const
Size of the internal block used by the hash algorithm.
void finalize(void *hash, size_t len)
Finalizes the hashing process and returns the hash.
void finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t hashLen)
Finalizes the HMAC hashing process and returns the hash.
size_t hashSize() const
Size of the hash result from finalize().
SHA1()
Constructs a SHA-1 hash object.
void formatHMACKey(void *block, const void *key, size_t len, uint8_t pad)
Formats a HMAC key into a block.