33typedef std::uint32_t
u32;
34typedef std::uint64_t
u64;
38static const u32 K[64] = {
39 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL,
40 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL,
41 0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL,
42 0xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
43 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 0x983e5152UL,
44 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL,
45 0x06ca6351UL, 0x14292967UL, 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL,
46 0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
47 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL,
48 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL,
49 0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL,
50 0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
51 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
58static inline u32 load32(
const std::uint8_t* y) {
59 return (
u32(y[0]) << 24) | (
u32(y[1]) << 16) |
60 (
u32(y[2]) << 8) | (
u32(y[3]) << 0);
62static inline void store64(
u64 x, std::uint8_t* y) {
63 for (
int i = 0; i != 8; ++i)
64 y[i] = (x >> ((7 - i) * 8)) & 255;
66static inline void store32(
u32 x, std::uint8_t* y) {
67 for (
int i = 0; i != 4; ++i)
68 y[i] = (x >> ((3 - i) * 8)) & 255;
72 return z ^ (x & (y ^ z));
75 return ((x | y) & z) | (x & y);
80static inline u32 Sigma0(
u32 x) {
83static inline u32 Sigma1(
u32 x) {
86static inline u32 Gamma0(
u32 x) {
89static inline u32 Gamma1(
u32 x) {
90 return ror32(x, 17) ^
ror32(x, 19) ^ Sh(x, 10);
93static void sha256_compress(std::uint32_t state[8],
const std::uint8_t* buf) {
94 u32 S[8], W[64], t0, t1, t;
97 for (
size_t i = 0; i < 8; i++)
101 for (
size_t i = 0; i < 16; i++)
102 W[i] = load32(buf + (4 * i));
105 for (
size_t i = 16; i < 64; i++)
106 W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
112 t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i];
113 t1 = Sigma0(a) + Maj(a, b, c);
118 for (
size_t i = 0; i < 64; ++i)
120 RND(S[0], S[1], S[2], S[3], S[4], S[5], S[6], S[7], i);
121 t = S[7], S[7] = S[6], S[6] = S[5], S[5] = S[4],
122 S[4] = S[3], S[3] = S[2], S[2] = S[1], S[1] = S[0], S[0] = t;
126 for (
size_t i = 0; i < 8; i++)
127 state[i] = state[i] + S[i];
157 auto in =
static_cast<const std::uint8_t*
>(data);
161 if (
curlen_ == 0 && size >= block_size)
163 sha256_compress(
state_, in);
187 return process(str.data(), str.size());
217 for (
size_t i = 0; i < 8; i++)
218 store32(
state_[i],
static_cast<std::uint8_t*
>(
digest) + (4 * i));
223 finalize(
const_cast<char*
>(out.data()));
239std::string
sha256_hex(
const void* data, std::uint32_t size) {
SHA-256 processor without external dependencies.
void finalize(void *digest)
finalize computation and output 32 byte (256 bit) digest
std::string digest_hex()
finalize computation and return 32 byte (256 bit) digest hex encoded
std::string digest()
finalize computation and return 32 byte (256 bit) digest
std::string digest_hex_uc()
finalize computation and return 32 byte (256 bit) digest upper-case hex
static constexpr size_t kDigestLength
digest length in bytes
void process(const void *data, std::uint32_t size)
process more data
SHA256()
construct empty object.
std::string sha256_hex_uc(const void *data, std::uint32_t size)
process data and return 32 byte (256 bit) digest upper-case hex encoded
std::string sha256_hex(const void *data, std::uint32_t size)
process data and return 32 byte (256 bit) digest hex encoded
static std::uint32_t ror32(const std::uint32_t &x, int i)
ror32 - generic
std::string hexdump_lc(const void *const data, size_t size)
Dump a (binary) string as a sequence of lowercase hexadecimal pairs.
std::string hexdump(const void *const data, size_t size)
Dump a (binary) string as a sequence of uppercase hexadecimal pairs.