39 m_hash[0] = 0x6a09e667;
40 m_hash[1] = 0xbb67ae85;
41 m_hash[2] = 0x3c6ef372;
42 m_hash[3] = 0xa54ff53a;
43 m_hash[4] = 0x510e527f;
44 m_hash[5] = 0x9b05688c;
45 m_hash[6] = 0x1f83d9ab;
46 m_hash[7] = 0x5be0cd19;
52 inline uint32_t rotate(uint32_t a, uint32_t c)
54 return (a >> c) | (a << (32 - c));
57 inline uint32_t swap(uint32_t x)
59#if defined(__GNUC__) || defined(__clang__)
60 return __builtin_bswap32(x);
63 return _byteswap_ulong(x);
67 ((x >> 8) & 0x0000FF00) |
68 ((x << 8) & 0x00FF0000) |
73 inline uint32_t f1(uint32_t e, uint32_t f, uint32_t g)
75 uint32_t term1 = rotate(e, 6) ^ rotate(e, 11) ^ rotate(e, 25);
76 uint32_t term2 = (e & f) ^ (~e & g);
80 inline uint32_t f2(uint32_t a, uint32_t b, uint32_t c)
82 uint32_t term1 = rotate(a, 2) ^ rotate(a, 13) ^ rotate(a, 22);
83 uint32_t term2 = ((a | b) & c) | (a & b);
90void SHA256::processBlock(
const void* data)
93 uint32_t a = m_hash[0];
94 uint32_t b = m_hash[1];
95 uint32_t c = m_hash[2];
96 uint32_t d = m_hash[3];
97 uint32_t e = m_hash[4];
98 uint32_t f = m_hash[5];
99 uint32_t g = m_hash[6];
100 uint32_t h = m_hash[7];
103 const uint32_t* input = (uint32_t*) data;
107 for (i = 0; i < 16; i++)
108#
if defined(__BYTE_ORDER) && (__BYTE_ORDER != 0) && (__BYTE_ORDER == __BIG_ENDIAN)
111 words[i] = swap(input[i]);
117 x = h + f1(e,f,g) + 0x428a2f98 + words[ 0]; y = f2(a,b,c); d += x; h = x + y;
118 x = g + f1(d,e,f) + 0x71374491 + words[ 1]; y = f2(h,a,b); c += x; g = x + y;
119 x = f + f1(c,d,e) + 0xb5c0fbcf + words[ 2]; y = f2(g,h,a); b += x; f = x + y;
120 x = e + f1(b,c,d) + 0xe9b5dba5 + words[ 3]; y = f2(f,g,h); a += x; e = x + y;
121 x = d + f1(a,b,c) + 0x3956c25b + words[ 4]; y = f2(e,f,g); h += x; d = x + y;
122 x = c + f1(h,a,b) + 0x59f111f1 + words[ 5]; y = f2(d,e,f); g += x; c = x + y;
123 x = b + f1(g,h,a) + 0x923f82a4 + words[ 6]; y = f2(c,d,e); f += x; b = x + y;
124 x = a + f1(f,g,h) + 0xab1c5ed5 + words[ 7]; y = f2(b,c,d); e += x; a = x + y;
127 x = h + f1(e,f,g) + 0xd807aa98 + words[ 8]; y = f2(a,b,c); d += x; h = x + y;
128 x = g + f1(d,e,f) + 0x12835b01 + words[ 9]; y = f2(h,a,b); c += x; g = x + y;
129 x = f + f1(c,d,e) + 0x243185be + words[10]; y = f2(g,h,a); b += x; f = x + y;
130 x = e + f1(b,c,d) + 0x550c7dc3 + words[11]; y = f2(f,g,h); a += x; e = x + y;
131 x = d + f1(a,b,c) + 0x72be5d74 + words[12]; y = f2(e,f,g); h += x; d = x + y;
132 x = c + f1(h,a,b) + 0x80deb1fe + words[13]; y = f2(d,e,f); g += x; c = x + y;
133 x = b + f1(g,h,a) + 0x9bdc06a7 + words[14]; y = f2(c,d,e); f += x; b = x + y;
134 x = a + f1(f,g,h) + 0xc19bf174 + words[15]; y = f2(b,c,d); e += x; a = x + y;
138 words[i] = words[i-16] +
139 (rotate(words[i-15], 7) ^ rotate(words[i-15], 18) ^ (words[i-15] >> 3)) +
141 (rotate(words[i- 2], 17) ^ rotate(words[i- 2], 19) ^ (words[i- 2] >> 10));
144 x = h + f1(e,f,g) + 0xe49b69c1 + words[16]; y = f2(a,b,c); d += x; h = x + y;
145 x = g + f1(d,e,f) + 0xefbe4786 + words[17]; y = f2(h,a,b); c += x; g = x + y;
146 x = f + f1(c,d,e) + 0x0fc19dc6 + words[18]; y = f2(g,h,a); b += x; f = x + y;
147 x = e + f1(b,c,d) + 0x240ca1cc + words[19]; y = f2(f,g,h); a += x; e = x + y;
148 x = d + f1(a,b,c) + 0x2de92c6f + words[20]; y = f2(e,f,g); h += x; d = x + y;
149 x = c + f1(h,a,b) + 0x4a7484aa + words[21]; y = f2(d,e,f); g += x; c = x + y;
150 x = b + f1(g,h,a) + 0x5cb0a9dc + words[22]; y = f2(c,d,e); f += x; b = x + y;
151 x = a + f1(f,g,h) + 0x76f988da + words[23]; y = f2(b,c,d); e += x; a = x + y;
155 words[i] = words[i-16] +
156 (rotate(words[i-15], 7) ^ rotate(words[i-15], 18) ^ (words[i-15] >> 3)) +
158 (rotate(words[i- 2], 17) ^ rotate(words[i- 2], 19) ^ (words[i- 2] >> 10));
161 x = h + f1(e,f,g) + 0x983e5152 + words[24]; y = f2(a,b,c); d += x; h = x + y;
162 x = g + f1(d,e,f) + 0xa831c66d + words[25]; y = f2(h,a,b); c += x; g = x + y;
163 x = f + f1(c,d,e) + 0xb00327c8 + words[26]; y = f2(g,h,a); b += x; f = x + y;
164 x = e + f1(b,c,d) + 0xbf597fc7 + words[27]; y = f2(f,g,h); a += x; e = x + y;
165 x = d + f1(a,b,c) + 0xc6e00bf3 + words[28]; y = f2(e,f,g); h += x; d = x + y;
166 x = c + f1(h,a,b) + 0xd5a79147 + words[29]; y = f2(d,e,f); g += x; c = x + y;
167 x = b + f1(g,h,a) + 0x06ca6351 + words[30]; y = f2(c,d,e); f += x; b = x + y;
168 x = a + f1(f,g,h) + 0x14292967 + words[31]; y = f2(b,c,d); e += x; a = x + y;
172 words[i] = words[i-16] +
173 (rotate(words[i-15], 7) ^ rotate(words[i-15], 18) ^ (words[i-15] >> 3)) +
175 (rotate(words[i- 2], 17) ^ rotate(words[i- 2], 19) ^ (words[i- 2] >> 10));
178 x = h + f1(e,f,g) + 0x27b70a85 + words[32]; y = f2(a,b,c); d += x; h = x + y;
179 x = g + f1(d,e,f) + 0x2e1b2138 + words[33]; y = f2(h,a,b); c += x; g = x + y;
180 x = f + f1(c,d,e) + 0x4d2c6dfc + words[34]; y = f2(g,h,a); b += x; f = x + y;
181 x = e + f1(b,c,d) + 0x53380d13 + words[35]; y = f2(f,g,h); a += x; e = x + y;
182 x = d + f1(a,b,c) + 0x650a7354 + words[36]; y = f2(e,f,g); h += x; d = x + y;
183 x = c + f1(h,a,b) + 0x766a0abb + words[37]; y = f2(d,e,f); g += x; c = x + y;
184 x = b + f1(g,h,a) + 0x81c2c92e + words[38]; y = f2(c,d,e); f += x; b = x + y;
185 x = a + f1(f,g,h) + 0x92722c85 + words[39]; y = f2(b,c,d); e += x; a = x + y;
189 words[i] = words[i-16] +
190 (rotate(words[i-15], 7) ^ rotate(words[i-15], 18) ^ (words[i-15] >> 3)) +
192 (rotate(words[i- 2], 17) ^ rotate(words[i- 2], 19) ^ (words[i- 2] >> 10));
195 x = h + f1(e,f,g) + 0xa2bfe8a1 + words[40]; y = f2(a,b,c); d += x; h = x + y;
196 x = g + f1(d,e,f) + 0xa81a664b + words[41]; y = f2(h,a,b); c += x; g = x + y;
197 x = f + f1(c,d,e) + 0xc24b8b70 + words[42]; y = f2(g,h,a); b += x; f = x + y;
198 x = e + f1(b,c,d) + 0xc76c51a3 + words[43]; y = f2(f,g,h); a += x; e = x + y;
199 x = d + f1(a,b,c) + 0xd192e819 + words[44]; y = f2(e,f,g); h += x; d = x + y;
200 x = c + f1(h,a,b) + 0xd6990624 + words[45]; y = f2(d,e,f); g += x; c = x + y;
201 x = b + f1(g,h,a) + 0xf40e3585 + words[46]; y = f2(c,d,e); f += x; b = x + y;
202 x = a + f1(f,g,h) + 0x106aa070 + words[47]; y = f2(b,c,d); e += x; a = x + y;
206 words[i] = words[i-16] +
207 (rotate(words[i-15], 7) ^ rotate(words[i-15], 18) ^ (words[i-15] >> 3)) +
209 (rotate(words[i- 2], 17) ^ rotate(words[i- 2], 19) ^ (words[i- 2] >> 10));
212 x = h + f1(e,f,g) + 0x19a4c116 + words[48]; y = f2(a,b,c); d += x; h = x + y;
213 x = g + f1(d,e,f) + 0x1e376c08 + words[49]; y = f2(h,a,b); c += x; g = x + y;
214 x = f + f1(c,d,e) + 0x2748774c + words[50]; y = f2(g,h,a); b += x; f = x + y;
215 x = e + f1(b,c,d) + 0x34b0bcb5 + words[51]; y = f2(f,g,h); a += x; e = x + y;
216 x = d + f1(a,b,c) + 0x391c0cb3 + words[52]; y = f2(e,f,g); h += x; d = x + y;
217 x = c + f1(h,a,b) + 0x4ed8aa4a + words[53]; y = f2(d,e,f); g += x; c = x + y;
218 x = b + f1(g,h,a) + 0x5b9cca4f + words[54]; y = f2(c,d,e); f += x; b = x + y;
219 x = a + f1(f,g,h) + 0x682e6ff3 + words[55]; y = f2(b,c,d); e += x; a = x + y;
223 words[i] = words[i-16] +
224 (rotate(words[i-15], 7) ^ rotate(words[i-15], 18) ^ (words[i-15] >> 3)) +
226 (rotate(words[i- 2], 17) ^ rotate(words[i- 2], 19) ^ (words[i- 2] >> 10));
229 x = h + f1(e,f,g) + 0x748f82ee + words[56]; y = f2(a,b,c); d += x; h = x + y;
230 x = g + f1(d,e,f) + 0x78a5636f + words[57]; y = f2(h,a,b); c += x; g = x + y;
231 x = f + f1(c,d,e) + 0x84c87814 + words[58]; y = f2(g,h,a); b += x; f = x + y;
232 x = e + f1(b,c,d) + 0x8cc70208 + words[59]; y = f2(f,g,h); a += x; e = x + y;
233 x = d + f1(a,b,c) + 0x90befffa + words[60]; y = f2(e,f,g); h += x; d = x + y;
234 x = c + f1(h,a,b) + 0xa4506ceb + words[61]; y = f2(d,e,f); g += x; c = x + y;
235 x = b + f1(g,h,a) + 0xbef9a3f7 + words[62]; y = f2(c,d,e); f += x; b = x + y;
236 x = a + f1(f,g,h) + 0xc67178f2 + words[63]; y = f2(b,c,d); e += x; a = x + y;
253 const uint8_t* current = (
const uint8_t*) data;
255 if (m_bufferSize > 0)
257 while (numBytes > 0 && m_bufferSize <
BlockSize)
259 m_buffer[m_bufferSize++] = *current++;
267 processBlock(m_buffer);
279 processBlock(current);
288 m_buffer[m_bufferSize++] = *current++;
295void SHA256::processBuffer()
304 size_t paddedLength = m_bufferSize * 8;
310 size_t lower11Bits = paddedLength & 511;
311 if (lower11Bits <= 448)
312 paddedLength += 448 - lower11Bits;
314 paddedLength += 512 + 448 - lower11Bits;
323 m_buffer[m_bufferSize] = 128;
328 for (i = m_bufferSize + 1; i <
BlockSize; i++)
330 for (; i < paddedLength; i++)
334 uint64_t msgBits = 8 * (m_numBytes + m_bufferSize);
336 unsigned char* addLength;
338 addLength = m_buffer + paddedLength;
340 addLength = extra + paddedLength -
BlockSize;
343 *addLength++ = (
unsigned char)((msgBits >> 56) & 0xFF);
344 *addLength++ = (
unsigned char)((msgBits >> 48) & 0xFF);
345 *addLength++ = (
unsigned char)((msgBits >> 40) & 0xFF);
346 *addLength++ = (
unsigned char)((msgBits >> 32) & 0xFF);
347 *addLength++ = (
unsigned char)((msgBits >> 24) & 0xFF);
348 *addLength++ = (
unsigned char)((msgBits >> 16) & 0xFF);
349 *addLength++ = (
unsigned char)((msgBits >> 8) & 0xFF);
350 *addLength = (
unsigned char)( msgBits & 0xFF);
353 processBlock(m_buffer);
372 static const char dec2hex[16+1] =
"0123456789abcdef";
373 result += dec2hex[(rawHash[i] >> 4) & 15];
374 result += dec2hex[ rawHash[i] & 15];
385 uint32_t oldHash[HashValues];
386 for (
int i = 0; i < HashValues; i++)
387 oldHash[i] = m_hash[i];
392 unsigned char* current = buffer;
393 for (
int i = 0; i < HashValues; i++)
395 *current++ = (m_hash[i] >> 24) & 0xFF;
396 *current++ = (m_hash[i] >> 16) & 0xFF;
397 *current++ = (m_hash[i] >> 8) & 0xFF;
398 *current++ = m_hash[i] & 0xFF;
401 m_hash[i] = oldHash[i];
419 add(text.c_str(), text.size());
std::string getHash()
return latest hash as 64 hex characters
std::string operator()(const void *data, size_t numBytes)
compute SHA256 of a memory block
void add(const void *data, size_t numBytes)
add arbitrary number of bytes
Usage: std::string msg = "The quick brown fox jumps over the lazy dog"; std::string key = "key"; std:...
Third-party (vendored): SHA-256 hash from Stephan Brumme's portable hashing library (create....