23 static constexpr std::array<uint32_t, 8> hash_init
37 static constexpr std::array<uint32_t, 64> k
40 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
41 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
42 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
43 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
44 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
45 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
46 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
47 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
48 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
49 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
50 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
51 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
52 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
53 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
54 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
55 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
60 typedef std::array<uint32_t, 8>
Hash;
73 std::array<uint32_t, 64> w;
76 const uint8_t *u8_data =
reinterpret_cast<const uint8_t *
>(data);
78 for (uint32_t i = 0; i < 16; i++)
80 (uint32_t(u8_data[4 * i + 0]) << 24) |
81 (uint32_t(u8_data[4 * i + 1]) << 16) |
82 (uint32_t(u8_data[4 * i + 2]) << 8) |
83 (uint32_t(u8_data[4 * i + 3]) );
86 for (uint32_t i = 16; i < 64; i++)
88 const uint32_t w0 = w[i - 15];
89 const uint32_t s0 =
rotr(w0, 7) ^
rotr(w0, 18) ^ (w0 >> 3);
90 const uint32_t w1 = w[i - 2];
91 const uint32_t s1 =
rotr(w1, 17) ^
rotr(w1, 19) ^ (w1 >> 10);
93 w[i] = w[i - 16] + s0 + w[i - 7] + s1;
98 for (uint32_t i = 0; i < 64; i++)
100 const uint32_t S1 =
rotr(x[4], 6) ^
rotr(x[4], 11) ^
rotr(x[4], 25);
101 const uint32_t ch = (x[4] & x[5]) ^ ((~x[4]) & x[6]);
102 const uint32_t temp1 = x[7] + S1 + ch + k[i] + w[i];
103 const uint32_t S0 =
rotr(x[0], 2) ^
rotr(x[0], 13) ^
rotr(x[0], 22);
104 const uint32_t maj = (x[0] & x[1]) ^ (x[0] & x[2]) ^ (x[1] & x[2]);
105 const uint32_t temp2 = S0 + maj;
114 x[0] = temp1 + temp2;
117 for (uint32_t i = 0; i < 8; i++)
127 const char *
const data,
128 const uint64_t total_length_in_bytes
131 std::array<uint32_t, 32> final_chunks{};
132 uint8_t *byte_buffer =
reinterpret_cast<uint8_t *
>(&final_chunks[0]);
133 uint32_t n = uint32_t(total_length_in_bytes & 0x3fULL);
134 std::copy_n(data, n, byte_buffer);
135 byte_buffer[n] = 0x80;
137 const int chunk_count = n + 9 <= 64 ? 1 : 2;
140 uint64_t length_in_bits = total_length_in_bytes * 8;
141 for (
int index = chunk_count * 64, i = 8; --index, --i >= 0;)
143 byte_buffer[index] = uint8_t(length_in_bits);
144 length_in_bits >>= 8;
148 for (uint32_t i = 0; i < uint32_t(chunk_count); i++)
149 process_chunk(
reinterpret_cast<char *
>(&final_chunks[16 * i]));