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);
139 void SHA1::processChunk()
144 for (index = 0; index < 16; ++index)
145 state.w[index] = be32toh(state.w[index]);
148 uint32_t a = state.h[0];
149 uint32_t b = state.h[1];
150 uint32_t c = state.h[2];
151 uint32_t d = state.h[3];
152 uint32_t e = state.h[4];
156 for (index = 0; index < 16; ++index) {
157 temp = leftRotate5(a) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + state.w[index];
168 for (; index < 20; ++index) {
169 temp = state.w[index & 0x0F] = leftRotate1
170 (state.w[(index - 3) & 0x0F] ^ state.w[(index - 8) & 0x0F] ^
171 state.w[(index - 14) & 0x0F] ^ state.w[(index - 16) & 0x0F]);
172 temp = leftRotate5(a) + ((b & c) | ((~b) & d)) + e + 0x5A827999 + temp;
179 for (; index < 40; ++index) {
180 temp = state.w[index & 0x0F] = leftRotate1
181 (state.w[(index - 3) & 0x0F] ^ state.w[(index - 8) & 0x0F] ^
182 state.w[(index - 14) & 0x0F] ^ state.w[(index - 16) & 0x0F]);
183 temp = leftRotate5(a) + (b ^ c ^ d) + e + 0x6ED9EBA1 + temp;
190 for (; index < 60; ++index) {
191 temp = state.w[index & 0x0F] = leftRotate1
192 (state.w[(index - 3) & 0x0F] ^ state.w[(index - 8) & 0x0F] ^
193 state.w[(index - 14) & 0x0F] ^ state.w[(index - 16) & 0x0F]);
194 temp = leftRotate5(a) + ((b & c) | (b & d) | (c & d)) + e + 0x8F1BBCDC + temp;
201 for (; index < 80; ++index) {
202 temp = state.w[index & 0x0F] = leftRotate1
203 (state.w[(index - 3) & 0x0F] ^ state.w[(index - 8) & 0x0F] ^
204 state.w[(index - 14) & 0x0F] ^ state.w[(index - 16) & 0x0F]);
205 temp = leftRotate5(a) + (b ^ c ^ d) + e + 0xCA62C1D6 + temp;
221 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.