25 #include "utility/RotateUtil.h"
26 #include "utility/EndianUtil.h"
76 state.h[0] = 0x67452301;
77 state.h[1] = 0xEFCDAB89;
78 state.h[2] = 0x98BADCFE;
79 state.h[3] = 0x10325476;
80 state.h[4] = 0xC3D2E1F0;
88 state.length += ((uint64_t)len) << 3;
91 const uint8_t *d = (
const uint8_t *)data;
93 uint8_t size = 64 - state.chunkSize;
96 memcpy(((uint8_t *)state.w) + state.chunkSize, d, size);
97 state.chunkSize += size;
100 if (state.chunkSize == 64) {
111 uint8_t *wbytes = (uint8_t *)state.w;
112 if (state.chunkSize <= (64 - 9)) {
113 wbytes[state.chunkSize] = 0x80;
114 memset(wbytes + state.chunkSize + 1, 0x00, 64 - 8 - (state.chunkSize + 1));
115 state.w[14] = htobe32((uint32_t)(state.length >> 32));
116 state.w[15] = htobe32((uint32_t)state.length);
119 wbytes[state.chunkSize] = 0x80;
120 memset(wbytes + state.chunkSize + 1, 0x00, 64 - (state.chunkSize + 1));
122 memset(wbytes, 0x00, 64 - 8);
123 state.w[14] = htobe32((uint32_t)(state.length >> 32));
124 state.w[15] = htobe32((uint32_t)state.length);
129 for (uint8_t posn = 0; posn < 5; ++posn)
130 state.w[posn] = htobe32(state.h[posn]);
135 memcpy(hash, state.w, len);
147 state.length += 64 * 8;
156 state.length += 64 * 8;
158 update(temp,
sizeof(temp));
168 void SHA1::processChunk()
173 for (index = 0; index < 16; ++index)
174 state.w[index] = be32toh(state.w[index]);
177 uint32_t a = state.h[0];
178 uint32_t b = state.h[1];
179 uint32_t c = state.h[2];
180 uint32_t d = state.h[3];
181 uint32_t e = state.h[4];
185 for (index = 0; index < 16; ++index) {
186 temp = leftRotate5(a) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + state.w[index];
197 for (; index < 20; ++index) {
198 temp = state.w[index & 0x0F] = leftRotate1
199 (state.w[(index - 3) & 0x0F] ^ state.w[(index - 8) & 0x0F] ^
200 state.w[(index - 14) & 0x0F] ^ state.w[(index - 16) & 0x0F]);
201 temp = leftRotate5(a) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + temp;
208 for (; index < 40; ++index) {
209 temp = state.w[index & 0x0F] = leftRotate1
210 (state.w[(index - 3) & 0x0F] ^ state.w[(index - 8) & 0x0F] ^
211 state.w[(index - 14) & 0x0F] ^ state.w[(index - 16) & 0x0F]);
212 temp = leftRotate5(a) + (b ^ c ^ d) + e + 0x6ED9EBA1 + temp;
219 for (; index < 60; ++index) {
220 temp = state.w[index & 0x0F] = leftRotate1
221 (state.w[(index - 3) & 0x0F] ^ state.w[(index - 8) & 0x0F] ^
222 state.w[(index - 14) & 0x0F] ^ state.w[(index - 16) & 0x0F]);
223 temp = leftRotate5(a) + ((b & c) | (b & d) | (c & d)) + e + 0x8F1BBCDC + temp;
230 for (; index < 80; ++index) {
231 temp = state.w[index & 0x0F] = leftRotate1
232 (state.w[(index - 3) & 0x0F] ^ state.w[(index - 8) & 0x0F] ^
233 state.w[(index - 14) & 0x0F] ^ state.w[(index - 16) & 0x0F]);
234 temp = leftRotate5(a) + (b ^ c ^ d) + e + 0xCA62C1D6 + temp;
250 a = b = c = d = e = temp = 0;
void formatHMACKey(void *block, const void *key, size_t len, uint8_t pad)
Formats a HMAC key into a block.
void clear()
Clears the hash state, removing all sensitive data, and then resets the hash ready for a new hashing ...
void finalize(void *hash, size_t len)
Finalizes the hashing process and returns the hash.
size_t hashSize() const
Size of the hash result from finalize().
void finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t hashLen)
Finalizes the HMAC hashing process and returns the hash.
virtual ~SHA1()
Destroys this SHA-1 hash object after clearing sensitive information.
size_t blockSize() const
Size of the internal block used by the hash algorithm.
void reset()
Resets the hash ready for a new hashing process.
void resetHMAC(const void *key, size_t keyLen)
Resets the hash ready for a new HMAC hashing process.
SHA1()
Constructs a SHA-1 hash object.
void update(const void *data, size_t len)
Updates the hash with more data.