11 #include <machine/endian.h>
12#elif !defined(_MSC_VER)
35 m_hash[0] = 0x6a09e667;
36 m_hash[1] = 0xbb67ae85;
37 m_hash[2] = 0x3c6ef372;
38 m_hash[3] = 0xa54ff53a;
39 m_hash[4] = 0x510e527f;
40 m_hash[5] = 0x9b05688c;
41 m_hash[6] = 0x1f83d9ab;
42 m_hash[7] = 0x5be0cd19;
44#ifdef SHA2_224_SEED_VECTOR
47 m_hash[0] = 0xc1059ed8;
48 m_hash[1] = 0x367cd507;
49 m_hash[2] = 0x3070dd17;
50 m_hash[3] = 0xf70e5939;
51 m_hash[4] = 0xffc00b31;
52 m_hash[5] = 0x68581511;
53 m_hash[6] = 0x64f98fa7;
54 m_hash[7] = 0xbefa4fa4;
61 inline uint32_t rotate(uint32_t a, uint32_t c)
63 return (a >> c) | (a << (32 - c));
66 inline uint32_t swap(uint32_t x)
68#if defined(__GNUC__) || defined(__clang__)
69 return __builtin_bswap32(x);
72 return _byteswap_ulong(x);
76 ((x >> 8) & 0x0000FF00) |
77 ((x << 8) & 0x00FF0000) |
82 inline uint32_t f1(uint32_t e, uint32_t f, uint32_t g)
84 uint32_t term1 = rotate(e, 6) ^ rotate(e, 11) ^ rotate(e, 25);
85 uint32_t term2 = (e & f) ^ (~e & g);
89 inline uint32_t f2(uint32_t a, uint32_t b, uint32_t c)
91 uint32_t term1 = rotate(a, 2) ^ rotate(a, 13) ^ rotate(a, 22);
92 uint32_t term2 = ((a | b) & c) | (a & b);
99void SHA256::processBlock(
const void* data)
102 uint32_t a = m_hash[0];
103 uint32_t b = m_hash[1];
104 uint32_t c = m_hash[2];
105 uint32_t d = m_hash[3];
106 uint32_t e = m_hash[4];
107 uint32_t f = m_hash[5];
108 uint32_t g = m_hash[6];
109 uint32_t h = m_hash[7];
112 const uint32_t* input = (uint32_t*) data;
116 for (i = 0; i < 16; i++)
117#
if defined(__BYTE_ORDER) && (__BYTE_ORDER != 0) && (__BYTE_ORDER == __BIG_ENDIAN)
120 words[i] = swap(input[i]);
126 x = h + f1(e,f,g) + 0x428a2f98 + words[ 0]; y = f2(a,b,c); d += x; h = x + y;
127 x = g + f1(d,e,f) + 0x71374491 + words[ 1]; y = f2(h,a,b); c += x; g = x + y;
128 x = f + f1(c,d,e) + 0xb5c0fbcf + words[ 2]; y = f2(g,h,a); b += x; f = x + y;
129 x = e + f1(b,c,d) + 0xe9b5dba5 + words[ 3]; y = f2(f,g,h); a += x; e = x + y;
130 x = d + f1(a,b,c) + 0x3956c25b + words[ 4]; y = f2(e,f,g); h += x; d = x + y;
131 x = c + f1(h,a,b) + 0x59f111f1 + words[ 5]; y = f2(d,e,f); g += x; c = x + y;
132 x = b + f1(g,h,a) + 0x923f82a4 + words[ 6]; y = f2(c,d,e); f += x; b = x + y;
133 x = a + f1(f,g,h) + 0xab1c5ed5 + words[ 7]; y = f2(b,c,d); e += x; a = x + y;
136 x = h + f1(e,f,g) + 0xd807aa98 + words[ 8]; y = f2(a,b,c); d += x; h = x + y;
137 x = g + f1(d,e,f) + 0x12835b01 + words[ 9]; y = f2(h,a,b); c += x; g = x + y;
138 x = f + f1(c,d,e) + 0x243185be + words[10]; y = f2(g,h,a); b += x; f = x + y;
139 x = e + f1(b,c,d) + 0x550c7dc3 + words[11]; y = f2(f,g,h); a += x; e = x + y;
140 x = d + f1(a,b,c) + 0x72be5d74 + words[12]; y = f2(e,f,g); h += x; d = x + y;
141 x = c + f1(h,a,b) + 0x80deb1fe + words[13]; y = f2(d,e,f); g += x; c = x + y;
142 x = b + f1(g,h,a) + 0x9bdc06a7 + words[14]; y = f2(c,d,e); f += x; b = x + y;
143 x = a + f1(f,g,h) + 0xc19bf174 + words[15]; y = f2(b,c,d); e += x; a = x + y;
147 words[i] = words[i-16] +
148 (rotate(words[i-15], 7) ^ rotate(words[i-15], 18) ^ (words[i-15] >> 3)) +
150 (rotate(words[i- 2], 17) ^ rotate(words[i- 2], 19) ^ (words[i- 2] >> 10));
153 x = h + f1(e,f,g) + 0xe49b69c1 + words[16]; y = f2(a,b,c); d += x; h = x + y;
154 x = g + f1(d,e,f) + 0xefbe4786 + words[17]; y = f2(h,a,b); c += x; g = x + y;
155 x = f + f1(c,d,e) + 0x0fc19dc6 + words[18]; y = f2(g,h,a); b += x; f = x + y;
156 x = e + f1(b,c,d) + 0x240ca1cc + words[19]; y = f2(f,g,h); a += x; e = x + y;
157 x = d + f1(a,b,c) + 0x2de92c6f + words[20]; y = f2(e,f,g); h += x; d = x + y;
158 x = c + f1(h,a,b) + 0x4a7484aa + words[21]; y = f2(d,e,f); g += x; c = x + y;
159 x = b + f1(g,h,a) + 0x5cb0a9dc + words[22]; y = f2(c,d,e); f += x; b = x + y;
160 x = a + f1(f,g,h) + 0x76f988da + words[23]; y = f2(b,c,d); e += x; a = x + y;
164 words[i] = words[i-16] +
165 (rotate(words[i-15], 7) ^ rotate(words[i-15], 18) ^ (words[i-15] >> 3)) +
167 (rotate(words[i- 2], 17) ^ rotate(words[i- 2], 19) ^ (words[i- 2] >> 10));
170 x = h + f1(e,f,g) + 0x983e5152 + words[24]; y = f2(a,b,c); d += x; h = x + y;
171 x = g + f1(d,e,f) + 0xa831c66d + words[25]; y = f2(h,a,b); c += x; g = x + y;
172 x = f + f1(c,d,e) + 0xb00327c8 + words[26]; y = f2(g,h,a); b += x; f = x + y;
173 x = e + f1(b,c,d) + 0xbf597fc7 + words[27]; y = f2(f,g,h); a += x; e = x + y;
174 x = d + f1(a,b,c) + 0xc6e00bf3 + words[28]; y = f2(e,f,g); h += x; d = x + y;
175 x = c + f1(h,a,b) + 0xd5a79147 + words[29]; y = f2(d,e,f); g += x; c = x + y;
176 x = b + f1(g,h,a) + 0x06ca6351 + words[30]; y = f2(c,d,e); f += x; b = x + y;
177 x = a + f1(f,g,h) + 0x14292967 + words[31]; y = f2(b,c,d); e += x; a = x + y;
181 words[i] = words[i-16] +
182 (rotate(words[i-15], 7) ^ rotate(words[i-15], 18) ^ (words[i-15] >> 3)) +
184 (rotate(words[i- 2], 17) ^ rotate(words[i- 2], 19) ^ (words[i- 2] >> 10));
187 x = h + f1(e,f,g) + 0x27b70a85 + words[32]; y = f2(a,b,c); d += x; h = x + y;
188 x = g + f1(d,e,f) + 0x2e1b2138 + words[33]; y = f2(h,a,b); c += x; g = x + y;
189 x = f + f1(c,d,e) + 0x4d2c6dfc + words[34]; y = f2(g,h,a); b += x; f = x + y;
190 x = e + f1(b,c,d) + 0x53380d13 + words[35]; y = f2(f,g,h); a += x; e = x + y;
191 x = d + f1(a,b,c) + 0x650a7354 + words[36]; y = f2(e,f,g); h += x; d = x + y;
192 x = c + f1(h,a,b) + 0x766a0abb + words[37]; y = f2(d,e,f); g += x; c = x + y;
193 x = b + f1(g,h,a) + 0x81c2c92e + words[38]; y = f2(c,d,e); f += x; b = x + y;
194 x = a + f1(f,g,h) + 0x92722c85 + words[39]; y = f2(b,c,d); e += x; a = x + y;
198 words[i] = words[i-16] +
199 (rotate(words[i-15], 7) ^ rotate(words[i-15], 18) ^ (words[i-15] >> 3)) +
201 (rotate(words[i- 2], 17) ^ rotate(words[i- 2], 19) ^ (words[i- 2] >> 10));
204 x = h + f1(e,f,g) + 0xa2bfe8a1 + words[40]; y = f2(a,b,c); d += x; h = x + y;
205 x = g + f1(d,e,f) + 0xa81a664b + words[41]; y = f2(h,a,b); c += x; g = x + y;
206 x = f + f1(c,d,e) + 0xc24b8b70 + words[42]; y = f2(g,h,a); b += x; f = x + y;
207 x = e + f1(b,c,d) + 0xc76c51a3 + words[43]; y = f2(f,g,h); a += x; e = x + y;
208 x = d + f1(a,b,c) + 0xd192e819 + words[44]; y = f2(e,f,g); h += x; d = x + y;
209 x = c + f1(h,a,b) + 0xd6990624 + words[45]; y = f2(d,e,f); g += x; c = x + y;
210 x = b + f1(g,h,a) + 0xf40e3585 + words[46]; y = f2(c,d,e); f += x; b = x + y;
211 x = a + f1(f,g,h) + 0x106aa070 + words[47]; y = f2(b,c,d); e += x; a = x + y;
215 words[i] = words[i-16] +
216 (rotate(words[i-15], 7) ^ rotate(words[i-15], 18) ^ (words[i-15] >> 3)) +
218 (rotate(words[i- 2], 17) ^ rotate(words[i- 2], 19) ^ (words[i- 2] >> 10));
221 x = h + f1(e,f,g) + 0x19a4c116 + words[48]; y = f2(a,b,c); d += x; h = x + y;
222 x = g + f1(d,e,f) + 0x1e376c08 + words[49]; y = f2(h,a,b); c += x; g = x + y;
223 x = f + f1(c,d,e) + 0x2748774c + words[50]; y = f2(g,h,a); b += x; f = x + y;
224 x = e + f1(b,c,d) + 0x34b0bcb5 + words[51]; y = f2(f,g,h); a += x; e = x + y;
225 x = d + f1(a,b,c) + 0x391c0cb3 + words[52]; y = f2(e,f,g); h += x; d = x + y;
226 x = c + f1(h,a,b) + 0x4ed8aa4a + words[53]; y = f2(d,e,f); g += x; c = x + y;
227 x = b + f1(g,h,a) + 0x5b9cca4f + words[54]; y = f2(c,d,e); f += x; b = x + y;
228 x = a + f1(f,g,h) + 0x682e6ff3 + words[55]; y = f2(b,c,d); e += x; a = x + y;
232 words[i] = words[i-16] +
233 (rotate(words[i-15], 7) ^ rotate(words[i-15], 18) ^ (words[i-15] >> 3)) +
235 (rotate(words[i- 2], 17) ^ rotate(words[i- 2], 19) ^ (words[i- 2] >> 10));
238 x = h + f1(e,f,g) + 0x748f82ee + words[56]; y = f2(a,b,c); d += x; h = x + y;
239 x = g + f1(d,e,f) + 0x78a5636f + words[57]; y = f2(h,a,b); c += x; g = x + y;
240 x = f + f1(c,d,e) + 0x84c87814 + words[58]; y = f2(g,h,a); b += x; f = x + y;
241 x = e + f1(b,c,d) + 0x8cc70208 + words[59]; y = f2(f,g,h); a += x; e = x + y;
242 x = d + f1(a,b,c) + 0x90befffa + words[60]; y = f2(e,f,g); h += x; d = x + y;
243 x = c + f1(h,a,b) + 0xa4506ceb + words[61]; y = f2(d,e,f); g += x; c = x + y;
244 x = b + f1(g,h,a) + 0xbef9a3f7 + words[62]; y = f2(c,d,e); f += x; b = x + y;
245 x = a + f1(f,g,h) + 0xc67178f2 + words[63]; y = f2(b,c,d); e += x; a = x + y;
262 const uint8_t* current = (
const uint8_t*) data;
264 if (m_bufferSize > 0)
266 while (numBytes > 0 && m_bufferSize < BlockSize)
268 m_buffer[m_bufferSize++] = *current++;
274 if (m_bufferSize == BlockSize)
276 processBlock(m_buffer);
277 m_numBytes += BlockSize;
286 while (numBytes >= BlockSize)
288 processBlock(current);
289 current += BlockSize;
290 m_numBytes += BlockSize;
291 numBytes -= BlockSize;
297 m_buffer[m_bufferSize++] = *current++;
304void SHA256::processBuffer()
313 size_t paddedLength = m_bufferSize * 8;
319 size_t lower11Bits = paddedLength & 511;
320 if (lower11Bits <= 448)
321 paddedLength += 448 - lower11Bits;
323 paddedLength += 512 + 448 - lower11Bits;
328 unsigned char extra[BlockSize];
331 if (m_bufferSize < BlockSize)
332 m_buffer[m_bufferSize] = 128;
337 for (i = m_bufferSize + 1; i < BlockSize; i++)
339 for (; i < paddedLength; i++)
340 extra[i - BlockSize] = 0;
343 uint64_t msgBits = 8 * (m_numBytes + m_bufferSize);
345 unsigned char* addLength;
346 if (paddedLength < BlockSize)
347 addLength = m_buffer + paddedLength;
349 addLength = extra + paddedLength - BlockSize;
352 *addLength++ = (
unsigned char)((msgBits >> 56) & 0xFF);
353 *addLength++ = (
unsigned char)((msgBits >> 48) & 0xFF);
354 *addLength++ = (
unsigned char)((msgBits >> 40) & 0xFF);
355 *addLength++ = (
unsigned char)((msgBits >> 32) & 0xFF);
356 *addLength++ = (
unsigned char)((msgBits >> 24) & 0xFF);
357 *addLength++ = (
unsigned char)((msgBits >> 16) & 0xFF);
358 *addLength++ = (
unsigned char)((msgBits >> 8) & 0xFF);
359 *addLength = (
unsigned char)( msgBits & 0xFF);
362 processBlock(m_buffer);
364 if (paddedLength > BlockSize)
373 unsigned char rawHash[HashBytes];
378 result.reserve(2 * HashBytes);
379 for (
int i = 0; i < HashBytes; i++)
381 static const char dec2hex[16+1] =
"0123456789abcdef";
382 result += dec2hex[(rawHash[i] >> 4) & 15];
383 result += dec2hex[ rawHash[i] & 15];
394 uint32_t oldHash[HashValues];
395 for (
int i = 0; i < HashValues; i++)
396 oldHash[i] = m_hash[i];
401 unsigned char* current = buffer;
402 for (
int i = 0; i < HashValues; i++)
404 *current++ = (m_hash[i] >> 24) & 0xFF;
405 *current++ = (m_hash[i] >> 16) & 0xFF;
406 *current++ = (m_hash[i] >> 8) & 0xFF;
407 *current++ = m_hash[i] & 0xFF;
410 m_hash[i] = oldHash[i];
428 add(text.c_str(), text.size());
void add(const void *data, size_t numBytes)
add arbitrary number of bytes
std::string operator()(const void *data, size_t numBytes)
compute SHA256 of a memory block
std::string getHash()
return latest hash as 64 hex characters