37 static const u32
K[64] = {
38 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL,
39 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL,
40 0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL,
41 0xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
42 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 0x983e5152UL,
43 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL,
44 0x06ca6351UL, 0x14292967UL, 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL,
45 0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
46 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL,
47 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL,
48 0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL,
49 0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
50 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
53 static inline u32
min(u32
x, u32 y) {
57 static inline u32 load32(
const uint8_t* y) {
58 return (
u32(y[0]) << 24) | (
u32(y[1]) << 16) |
59 (
u32(y[2]) << 8) | (
u32(y[3]) << 0);
61 static inline void store64(u64 x, uint8_t* y) {
62 for (
int i = 0; i != 8; ++i)
63 y[i] = (x >> ((7 - i) * 8)) & 255;
65 static inline void store32(u32 x, uint8_t* y) {
66 for (
int i = 0; i != 4; ++i)
67 y[i] = (x >> ((3 - i) * 8)) & 255;
70 static inline u32
Ch(u32 x, u32 y, u32 z) {
71 return z ^ (x & (y ^ z));
73 static inline u32
Maj(u32 x, u32 y, u32 z) {
74 return ((x | y) & z) | (x & y);
76 static inline u32
Sh(u32 x, u32 n) {
79 static inline u32
Sigma0(u32 x) {
82 static inline u32
Sigma1(u32 x) {
85 static inline u32
Gamma0(u32 x) {
88 static inline u32
Gamma1(u32 x) {
92 static void sha256_compress(uint32_t state[8],
const uint8_t* buf) {
93 u32 S[8], W[64], t0, t1, t;
96 for (
size_t i = 0; i < 8; i++)
100 for (
size_t i = 0; i < 16; i++)
101 W[i] = load32(buf + (4 * i));
104 for (
size_t i = 16; i < 64; i++)
105 W[i] =
Gamma1(W[i - 2]) + W[i - 7] +
Gamma0(W[i - 15]) + W[i - 16];
109 [&](u32 a, u32 b, u32 c, u32& d, u32 e, u32 f, u32 g, u32& h, u32 i)
111 t0 = h +
Sigma1(e) +
Ch(e, f, g) + K[i] + W[i];
117 for (
size_t i = 0; i < 64; ++i)
119 RND(S[0], S[1], S[2], S[3], S[4], S[5], S[6], S[7], i);
120 t = S[7], S[7] = S[6], S[6] = S[5], S[5] = S[4],
121 S[4] = S[3], S[3] = S[2], S[2] = S[1], S[1] = S[0], S[0] = t;
125 for (
size_t i = 0; i < 8; i++)
126 state[i] = state[i] + S[i];
156 auto in =
static_cast<const uint8_t*
>(data);
160 if (
curlen_ == 0 && size >= block_size)
162 sha256_compress(
state_, in);
186 return process(str.data(), str.size());
216 for (
size_t i = 0; i < 8; i++)
217 store32(
state_[i], static_cast<uint8_t*>(digest) + (4 * i));
222 finalize(const_cast<char*>(out.data()));
239 return SHA256(data, size).digest_hex();
243 return SHA256(str).digest_hex();
247 return SHA256(data, size).digest_hex_uc();
251 return SHA256(str).digest_hex_uc();
static uint64_t Gamma0(uint64_t x)
std::string digest_hex_uc()
finalize computation and return 32 byte (256 bit) digest upper-case hex
static uint64_t Gamma1(uint64_t x)
static uint64_t Sigma0(uint64_t x)
std::string sha256_hex(const void *data, uint32_t size)
process data and return 32 byte (256 bit) digest hex encoded
SHA256()
construct empty object.
std::string hexdump_lc(const void *const data, size_t size)
Dump a (binary) string as a sequence of lowercase hexadecimal pairs.
static uint64_t Sh(uint64_t x, uint64_t n)
void finalize(void *digest)
finalize computation and output 32 byte (256 bit) digest
void process(const void *data, uint32_t size)
process more data
static uint64_t Ch(const uint64_t &x, const uint64_t &y, const uint64_t &z)
std::string digest_hex()
finalize computation and return 32 byte (256 bit) digest hex encoded
SHA-256 processor without external dependencies.
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
static constexpr size_t kDigestLength
digest length in bytes
static const uint64_t K[80]
static uint64_t Sigma1(uint64_t x)
std::string sha256_hex_uc(const void *data, uint32_t size)
process data and return 32 byte (256 bit) digest upper-case hex encoded
static uint_pair min()
return an uint_pair instance containing the smallest value possible
std::string digest()
finalize computation and return 32 byte (256 bit) digest
static void store64(uint64_t x, unsigned char *y)
static uint64_t Maj(const uint64_t &x, const uint64_t &y, const uint64_t &z)
std::string hexdump(const void *const data, size_t size)
Dump a (binary) string as a sequence of uppercase hexadecimal pairs.
static uint32_t ror32(const uint32_t &x, int i)
ror32 - generic