39 m_hash[0] = 0x67452301;
40 m_hash[1] = 0xefcdab89;
41 m_hash[2] = 0x98badcfe;
42 m_hash[3] = 0x10325476;
43 m_hash[4] = 0xc3d2e1f0;
50 inline uint32_t f1(uint32_t b, uint32_t c, uint32_t d)
52 return d ^ (b & (c ^ d));
55 inline uint32_t f2(uint32_t b, uint32_t c, uint32_t d)
60 inline uint32_t f3(uint32_t b, uint32_t c, uint32_t d)
62 return (b & c) | (b & d) | (c & d);
65 inline uint32_t rotate(uint32_t a, uint32_t c)
67 return (a << c) | (a >> (32 - c));
70 inline uint32_t swap(uint32_t x)
72#if defined(__GNUC__) || defined(__clang__)
73 return __builtin_bswap32(x);
76 return _byteswap_ulong(x);
80 ((x >> 8) & 0x0000FF00) |
81 ((x << 8) & 0x00FF0000) |
88void SHA1::processBlock(
const void* data)
91 uint32_t a = m_hash[0];
92 uint32_t b = m_hash[1];
93 uint32_t c = m_hash[2];
94 uint32_t d = m_hash[3];
95 uint32_t e = m_hash[4];
98 const uint32_t* input = (uint32_t*) data;
101 for (
int i = 0; i < 16; i++)
102#
if defined(__BYTE_ORDER) && (__BYTE_ORDER != 0) && (__BYTE_ORDER == __BIG_ENDIAN)
105 words[i] = swap(input[i]);
109 for (
int i = 16; i < 80; i++)
110 words[i] = rotate(words[i-3] ^ words[i-8] ^ words[i-14] ^ words[i-16], 1);
113 for (
int i = 0; i < 4; i++)
116 e += rotate(a,5) + f1(b,c,d) + words[offset ] + 0x5a827999; b = rotate(b,30);
117 d += rotate(e,5) + f1(a,b,c) + words[offset+1] + 0x5a827999; a = rotate(a,30);
118 c += rotate(d,5) + f1(e,a,b) + words[offset+2] + 0x5a827999; e = rotate(e,30);
119 b += rotate(c,5) + f1(d,e,a) + words[offset+3] + 0x5a827999; d = rotate(d,30);
120 a += rotate(b,5) + f1(c,d,e) + words[offset+4] + 0x5a827999; c = rotate(c,30);
124 for (
int i = 4; i < 8; i++)
127 e += rotate(a,5) + f2(b,c,d) + words[offset ] + 0x6ed9eba1; b = rotate(b,30);
128 d += rotate(e,5) + f2(a,b,c) + words[offset+1] + 0x6ed9eba1; a = rotate(a,30);
129 c += rotate(d,5) + f2(e,a,b) + words[offset+2] + 0x6ed9eba1; e = rotate(e,30);
130 b += rotate(c,5) + f2(d,e,a) + words[offset+3] + 0x6ed9eba1; d = rotate(d,30);
131 a += rotate(b,5) + f2(c,d,e) + words[offset+4] + 0x6ed9eba1; c = rotate(c,30);
135 for (
int i = 8; i < 12; i++)
138 e += rotate(a,5) + f3(b,c,d) + words[offset ] + 0x8f1bbcdc; b = rotate(b,30);
139 d += rotate(e,5) + f3(a,b,c) + words[offset+1] + 0x8f1bbcdc; a = rotate(a,30);
140 c += rotate(d,5) + f3(e,a,b) + words[offset+2] + 0x8f1bbcdc; e = rotate(e,30);
141 b += rotate(c,5) + f3(d,e,a) + words[offset+3] + 0x8f1bbcdc; d = rotate(d,30);
142 a += rotate(b,5) + f3(c,d,e) + words[offset+4] + 0x8f1bbcdc; c = rotate(c,30);
146 for (
int i = 12; i < 16; i++)
149 e += rotate(a,5) + f2(b,c,d) + words[offset ] + 0xca62c1d6; b = rotate(b,30);
150 d += rotate(e,5) + f2(a,b,c) + words[offset+1] + 0xca62c1d6; a = rotate(a,30);
151 c += rotate(d,5) + f2(e,a,b) + words[offset+2] + 0xca62c1d6; e = rotate(e,30);
152 b += rotate(c,5) + f2(d,e,a) + words[offset+3] + 0xca62c1d6; d = rotate(d,30);
153 a += rotate(b,5) + f2(c,d,e) + words[offset+4] + 0xca62c1d6; c = rotate(c,30);
166void SHA1::add(
const void* data,
size_t numBytes)
168 const uint8_t* current = (
const uint8_t*) data;
170 if (m_bufferSize > 0)
172 while (numBytes > 0 && m_bufferSize <
BlockSize)
174 m_buffer[m_bufferSize++] = *current++;
182 processBlock((
void*)m_buffer);
194 processBlock(current);
203 m_buffer[m_bufferSize++] = *current++;
210void SHA1::processBuffer()
219 size_t paddedLength = m_bufferSize * 8;
225 size_t lower11Bits = paddedLength & 511;
226 if (lower11Bits <= 448)
227 paddedLength += 448 - lower11Bits;
229 paddedLength += 512 + 448 - lower11Bits;
238 m_buffer[m_bufferSize] = 128;
243 for (i = m_bufferSize + 1; i <
BlockSize; i++)
245 for (; i < paddedLength; i++)
249 uint64_t msgBits = 8 * (m_numBytes + m_bufferSize);
251 unsigned char* addLength;
253 addLength = m_buffer + paddedLength;
255 addLength = extra + paddedLength -
BlockSize;
258 *addLength++ = (
unsigned char)((msgBits >> 56) & 0xFF);
259 *addLength++ = (
unsigned char)((msgBits >> 48) & 0xFF);
260 *addLength++ = (
unsigned char)((msgBits >> 40) & 0xFF);
261 *addLength++ = (
unsigned char)((msgBits >> 32) & 0xFF);
262 *addLength++ = (
unsigned char)((msgBits >> 24) & 0xFF);
263 *addLength++ = (
unsigned char)((msgBits >> 16) & 0xFF);
264 *addLength++ = (
unsigned char)((msgBits >> 8) & 0xFF);
265 *addLength = (
unsigned char)( msgBits & 0xFF);
268 processBlock(m_buffer);
287 static const char dec2hex[16+1] =
"0123456789abcdef";
288 result += dec2hex[(rawHash[i] >> 4) & 15];
289 result += dec2hex[ rawHash[i] & 15];
307 result += rawHash[i];
318 uint32_t oldHash[HashValues];
319 for (
int i = 0; i < HashValues; i++)
320 oldHash[i] = m_hash[i];
325 unsigned char* current = buffer;
326 for (
int i = 0; i < HashValues; i++)
328 *current++ = (m_hash[i] >> 24) & 0xFF;
329 *current++ = (m_hash[i] >> 16) & 0xFF;
330 *current++ = (m_hash[i] >> 8) & 0xFF;
331 *current++ = m_hash[i] & 0xFF;
334 m_hash[i] = oldHash[i];
352 add(text.c_str(), text.size());
std::string getHashRaw()
return latest hash as raw characters
void add(const void *data, size_t numBytes)
add arbitrary number of bytes
std::string operator()(const void *data, size_t numBytes)
compute SHA1 of a memory block
std::string getHash()
return latest hash as 40 hex characters
Usage: std::string msg = "The quick brown fox jumps over the lazy dog"; std::string key = "key"; std:...
Third-party (vendored): SHA-1 hash from Stephan Brumme's portable hashing library (create....