20 #define ror(value, bits) (((value) >> (bits)) | ((value) << (32 - (bits))))
22 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
24 #define STORE32H(x, y) \
26 (y)[0] = static_cast<uint8_t>(((x) >> 24) & 255); \
27 (y)[1] = static_cast<uint8_t>(((x) >> 16) & 255); \
28 (y)[2] = static_cast<uint8_t>(((x) >> 8) & 255); \
29 (y)[3] = static_cast<uint8_t>((x)&255); \
32 #define LOAD32H(x, y) \
34 x = (static_cast<uint32_t>((y)[0] & 255) << 24) | \
35 (static_cast<uint32_t>((y)[1] & 255) << 16) | \
36 (static_cast<uint32_t>((y)[2] & 255) << 8) | \
37 (static_cast<uint32_t>((y)[3] & 255)); \
40 #define STORE64H(x, y) \
42 (y)[0] = static_cast<uint8_t>(((x) >> 56) & 255); \
43 (y)[1] = static_cast<uint8_t>(((x) >> 48) & 255); \
44 (y)[2] = static_cast<uint8_t>(((x) >> 40) & 255); \
45 (y)[3] = static_cast<uint8_t>(((x) >> 32) & 255); \
46 (y)[4] = static_cast<uint8_t>(((x) >> 24) & 255); \
47 (y)[5] = static_cast<uint8_t>(((x) >> 16) & 255); \
48 (y)[6] = static_cast<uint8_t>(((x) >> 8) & 255); \
49 (y)[7] = static_cast<uint8_t>((x)&255); \
52 #define Ch(x, y, z) (z ^ (x & (y ^ z)))
53 #define Maj(x, y, z) (((x | y) & z) | (x & y))
54 #define S(x, n) ror((x), (n))
55 #define R(x, n) (((x)&0xFFFFFFFFUL) >> (n))
56 #define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
57 #define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
58 #define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
59 #define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
61 #define Sha256Round(a, b, c, d, e, f, g, h, i) \
62 t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \
63 t1 = Sigma0(a) + Maj(a, b, c); \
70 static const uint32_t
K[64] = {
71 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL,
72 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL,
73 0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL,
74 0xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
75 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 0x983e5152UL,
76 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL,
77 0x06ca6351UL, 0x14292967UL, 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL,
78 0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
79 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL,
80 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL,
81 0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL,
82 0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
83 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL};
104 for (i = 0; i < 8; i++) {
109 for (i = 0; i < 16; i++) {
110 LOAD32H(W[i], buffer + (4 * i));
114 for (i = 16; i < 64; i++) {
115 W[i] =
Gamma1(W[i - 2]) + W[i - 7] +
Gamma0(W[i - 15]) + W[i - 16];
119 for (i = 0; i < 64; i++) {
133 for (i = 0; i < 8; i++) {
160 while (buffer_size > 0) {
182 update(
reinterpret_cast<const uint8_t*
>(buffer.c_str()), buffer.size());
218 for (
int i = 0; i < 8; i++) {
226 context.
update(buffer, buffer_size);
231 std::stringstream ss;
233 for (uint16_t i : hash) {
234 ss << std::setw(2) << std::setfill(
'0') << i;
void update(uint8_t const *buffer, size_t buffer_size)
Add data to the SHA256 context.
uint32_t state_[8]
State of the SHA256 hash.
size_t curlen_
Current length of the SHA256 hash.
void transform_function(uint8_t const *buffer)
Compress 512-bits.
void reset()
Reset the SHA256 context.
uint64_t length_
Length of the SHA256 hash.
uint8_t buf_[64]
Buffer of the SHA256 hash.
Hash finalize()
Performs the final calculation of the hash and returns the digest (32 byte buffer containing 256bit h...
Hash calculate(uint8_t const *buffer, size_t buffer_size)
Calculate the SHA256 hash of the buffer.
std::array< uint8_t, HASH_SIZE > Hash
A SHA256 hash.
constexpr size_t BLOCK_SIZE
std::string hash_to_string(Hash hash)
Convert a SHA256 hash to a hexadecimal string.
static const uint32_t K[64]
The K array.
#define Sha256Round(a, b, c, d, e, f, g, h, i)