81 void AbstractSHA256Impl::finish()
94 if ((lsb += count) < t)
104 ibuffer[count++] = 0x80;
106 ibuffer[count++] = 0;
110 ibuffer[count++] = 0x80;
112 ibuffer[count++] = 0;
114 memset(ibuffer, 0, 56);
117 ibuffer[56] = msb >> 24;
118 ibuffer[57] = msb >> 16;
119 ibuffer[58] = msb >> 8;
121 ibuffer[60] = lsb >> 24;
122 ibuffer[61] = lsb >> 16;
123 ibuffer[62] = lsb >> 8;
129 #if (LIBXCKS_BYTE_ORDER == LIBXCKS_BIG_ENDIAN)
130 #define X(a) do { *(uint32_t*)p = h##a ; p += 4; } while(0)
132 #define X(a) do { *p++ = h##a >> 24; *p++ = h##a >> 16; \
133 *p++ = h##a >> 8; *p++ = h##a; } while(0)
151 void AbstractSHA256Impl::update(
const uint8_t* buf,
size_t len)
165 for (; len && count < 64; len--)
166 ibuffer[count++] = *buf++;
174 uint8_t tmpBuf[
sizeof(uint32_t) * 16];
175 memcpy(tmpBuf, buf,
sizeof(uint32_t) * 16);
183 for (; len && count < 64; len--)
184 ibuffer[count++] = *buf++;
189 #define Cho(x,y,z) (z ^ (x & (y ^ z)))
190 #define Maj(x,y,z) ((x & y) | (z & (x|y)))
191 #define Sum0(x) (ror ((x), 2) ^ ror ((x), 13) ^ ror ((x), 22))
192 #define Sum1(x) (ror ((x), 6) ^ ror ((x), 11) ^ ror ((x), 25))
193 #define S0(x) (ror ((x), 7) ^ ror ((x), 18) ^ ((x) >> 3))
194 #define S1(x) (ror ((x), 17) ^ ror ((x), 19) ^ ((x) >> 10))
195 #define R(a,b,c,d,e,f,g,h,k,w) do \
197 t1 = (h) + Sum1((e)) + Cho((e),(f),(g)) + (k) + (w); \
198 t2 = Sum0((a)) + Maj((a),(b),(c)); \
211 void AbstractSHA256Impl::transform(uint8_t* data)
213 static const uint32_t K[64] = {
214 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
215 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
216 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
217 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
218 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
219 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
220 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
221 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
222 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
223 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
224 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
225 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
226 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
227 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
228 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
229 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
232 uint32_t a,b,c,d,e,f,g,h,t1,t2;
246 #if (LIBXCKS_BYTE_ORDER == LIBXCKS_BIG_ENDIAN)
252 for (i = 0, p2 = (uint8_t*)x; i < 16; i++, p2 += 4)
262 for (i = 0; i < 16; i++)
265 w[i] =
S1(w[i - 2]) + w[i - 7] +
S0(w[i - 15]) + w[i - 16];
267 for (i = 0; i < 64; i++)
268 R(a, b, c, d, e, f, g, h, K[i], w[i]);
326 uint8_t* SHA224::getValue(uint8_t* buffer)
const
331 memcpy(buffer, sha224.
ibuffer, getSize());
385 uint8_t* SHA256::getValue(uint8_t* buffer)
const
390 memcpy(buffer, sha256.
ibuffer, getSize());
void finish()
Process the remaining bytes in the internal buffer and the usual prolog according to the standard.
uint8_t ibuffer[64]
Input buffer.
Computes the SHA224 hash from a byte stream.
Computes the SHA256 hash from a byte stream.
#define S0(x)
Helper function for SHA256's computing.
#define S1(x)
Helper function for SHA256's computing.
#define R(a, b, c, d, e, f, g, h, k, w)
Helper function for SHA256's computing.
#define X(a)
Helper function for SHA256's computing.
Compute sha224 hash and sha256 hash.
std::vector< std::string > ArrayString
Array of strings.