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;
72 state.finalized =
false;
83 state.length += ((uint64_t)len) << 3;
86 const uint8_t *d = (
const uint8_t *)data;
88 uint8_t size = 64 - state.chunkSize;
91 memcpy(((uint8_t *)state.w) + state.chunkSize, d, size);
92 state.chunkSize += size;
95 if (state.chunkSize == 64) {
105 if (!state.finalized) {
108 uint8_t *wbytes = (uint8_t *)state.w;
109 if (state.chunkSize <= (64 - 9)) {
110 wbytes[state.chunkSize] = 0x80;
111 memset(wbytes + state.chunkSize + 1, 0x00, 64 - 8 - (state.chunkSize + 1));
112 state.w[14] = htobe32((uint32_t)(state.length >> 32));
113 state.w[15] = htobe32((uint32_t)state.length);
116 wbytes[state.chunkSize] = 0x80;
117 memset(wbytes + state.chunkSize + 1, 0x00, 64 - (state.chunkSize + 1));
119 memset(wbytes, 0x00, 64 - 8);
120 state.w[14] = htobe32((uint32_t)(state.length >> 32));
121 state.w[15] = htobe32((uint32_t)state.length);
126 for (uint8_t posn = 0; posn < 5; ++posn)
127 state.w[posn] = htobe32(state.h[posn]);
128 state.finalized =
true;
134 memcpy(hash, state.w, len);
148 void SHA1::processChunk()
153 for (index = 0; index < 16; ++index)
154 state.w[index] = be32toh(state.w[index]);
157 uint32_t a = state.h[0];
158 uint32_t b = state.h[1];
159 uint32_t c = state.h[2];
160 uint32_t d = state.h[3];
161 uint32_t e = state.h[4];
165 for (index = 0; index < 16; ++index) {
166 temp = leftRotate5(a) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + state.w[index];
177 for (; index < 20; ++index) {
178 temp = state.w[index & 0x0F] = leftRotate1
179 (state.w[(index - 3) & 0x0F] ^ state.w[(index - 8) & 0x0F] ^
180 state.w[(index - 14) & 0x0F] ^ state.w[(index - 16) & 0x0F]);
181 temp = leftRotate5(a) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + temp;
188 for (; index < 40; ++index) {
189 temp = state.w[index & 0x0F] = leftRotate1
190 (state.w[(index - 3) & 0x0F] ^ state.w[(index - 8) & 0x0F] ^
191 state.w[(index - 14) & 0x0F] ^ state.w[(index - 16) & 0x0F]);
192 temp = leftRotate5(a) + (b ^ c ^ d) + e + 0x6ED9EBA1 + temp;
199 for (; index < 60; ++index) {
200 temp = state.w[index & 0x0F] = leftRotate1
201 (state.w[(index - 3) & 0x0F] ^ state.w[(index - 8) & 0x0F] ^
202 state.w[(index - 14) & 0x0F] ^ state.w[(index - 16) & 0x0F]);
203 temp = leftRotate5(a) + ((b & c) | (b & d) | (c & d)) + e + 0x8F1BBCDC + temp;
210 for (; index < 80; ++index) {
211 temp = state.w[index & 0x0F] = leftRotate1
212 (state.w[(index - 3) & 0x0F] ^ state.w[(index - 8) & 0x0F] ^
213 state.w[(index - 14) & 0x0F] ^ state.w[(index - 16) & 0x0F]);
214 temp = leftRotate5(a) + (b ^ c ^ d) + e + 0xCA62C1D6 + temp;
230 a = b = c = d = e = temp = 0;
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.
size_t hashSize() const
Size of the hash result from finalize().
SHA1()
Constructs a SHA-1 hash object.