30 using byte_t =
unsigned char;
31 using word_t = std::int32_t;
36 unsigned long long bitlen;
40 #define ROTLEFT(a, b) (((a) << (b)) | ((a) >> (32 - (b)))) 41 #define ROTRIGHT(a, b) (((a) >> (b)) | ((a) << (32 - (b)))) 43 #define CH(x, y, z) (((x) & (y)) ^ (~(x) & (z))) 44 #define MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) 45 #define EP0(x) (ROTRIGHT(x, 2) ^ ROTRIGHT(x, 13) ^ ROTRIGHT(x, 22)) 46 #define EP1(x) (ROTRIGHT(x, 6) ^ ROTRIGHT(x, 11) ^ ROTRIGHT(x, 25)) 47 #define SIG0(x) (ROTRIGHT(x, 7) ^ ROTRIGHT(x, 18) ^ ((x) >> 3)) 48 #define SIG1(x) (ROTRIGHT(x, 17) ^ ROTRIGHT(x, 19) ^ ((x) >> 10)) 50 static const word_t k[64] = {
static_cast<word_t
>(1116352408), static_cast<word_t>(1899447441),
51 static_cast<word_t
>(3049323471), static_cast<word_t>(3921009573),
52 static_cast<word_t
>(961987163), static_cast<word_t>(1508970993),
53 static_cast<word_t
>(2453635748), static_cast<word_t>(2870763221),
54 static_cast<word_t
>(3624381080), static_cast<word_t>(310598401),
55 static_cast<word_t
>(607225278), static_cast<word_t>(1426881987),
56 static_cast<word_t
>(1925078388), static_cast<word_t>(2162078206),
57 static_cast<word_t
>(2614888103), static_cast<word_t>(3248222580),
58 static_cast<word_t
>(3835390401), static_cast<word_t>(4022224774),
59 static_cast<word_t
>(264347078), static_cast<word_t>(604807628),
60 static_cast<word_t
>(770255983), static_cast<word_t>(1249150122),
61 static_cast<word_t
>(1555081692), static_cast<word_t>(1996064986),
62 static_cast<word_t
>(2554220882), static_cast<word_t>(2821834349),
63 static_cast<word_t
>(2952996808), static_cast<word_t>(3210313671),
64 static_cast<word_t
>(3336571891), static_cast<word_t>(3584528711),
65 static_cast<word_t
>(113926993), static_cast<word_t>(338241895),
66 static_cast<word_t
>(666307205), static_cast<word_t>(773529912),
67 static_cast<word_t
>(1294757372), static_cast<word_t>(1396182291),
68 static_cast<word_t
>(1695183700), static_cast<word_t>(1986661051),
69 static_cast<word_t
>(2177026350), static_cast<word_t>(2456956037),
70 static_cast<word_t
>(2730485921), static_cast<word_t>(2820302411),
71 static_cast<word_t
>(3259730800), static_cast<word_t>(3345764771),
72 static_cast<word_t
>(3516065817), static_cast<word_t>(3600352804),
73 static_cast<word_t
>(4094571909), static_cast<word_t>(275423344),
74 static_cast<word_t
>(430227734), static_cast<word_t>(506948616),
75 static_cast<word_t
>(659060556), static_cast<word_t>(883997877),
76 static_cast<word_t
>(958139571), static_cast<word_t>(1322822218),
77 static_cast<word_t
>(1537002063), static_cast<word_t>(1747873779),
78 static_cast<word_t
>(1955562222), static_cast<word_t>(2024104815),
79 static_cast<word_t
>(2227730452), static_cast<word_t>(2361852424),
80 static_cast<word_t
>(2428436474), static_cast<word_t>(2756734187),
81 static_cast<word_t
>(3204031479), static_cast<word_t>(3329325298)};
83 static inline void sha256_transform(ctx_t* ctx,
const byte_t data[]) {
84 word_t a, b, c, d, e, f, g, h, i, j, t1, t2, m[64];
86 for(i = 0, j = 0; i < 16; ++i, j += 4)
87 m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]);
89 m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16];
100 for(i = 0; i < 64; ++i) {
101 t1 = h + EP1(e) + CH(e, f, g) + k[i] + m[i];
102 t2 = EP0(a) + MAJ(a, b, c);
123 static void sha256_init(ctx_t* ctx) {
126 ctx->state[0] = 0x6a09e667;
127 ctx->state[1] = 0xbb67ae85;
128 ctx->state[2] = 0x3c6ef372;
129 ctx->state[3] = 0xa54ff53a;
130 ctx->state[4] = 0x510e527f;
131 ctx->state[5] = 0x9b05688c;
132 ctx->state[6] = 0x1f83d9ab;
133 ctx->state[7] = 0x5be0cd19;
136 static void sha256_update(ctx_t* ctx,
const byte_t data[],
size_t len) {
139 for(i = 0; i < len; ++i) {
140 ctx->data[ctx->datalen] = data[i];
142 if(ctx->datalen == 64) {
143 sha256_transform(ctx, ctx->data);
150 static void sha256_final(ctx_t* ctx, byte_t hash[32]) {
156 if(ctx->datalen < 56) {
157 ctx->data[i++] = 0x80;
159 ctx->data[i++] = 0x00;
161 ctx->data[i++] = 0x80;
163 ctx->data[i++] = 0x00;
164 sha256_transform(ctx, ctx->data);
165 std::memset(ctx->data, 0, 56);
169 ctx->bitlen += ctx->datalen * 8;
170 ctx->data[63] = ctx->bitlen;
171 ctx->data[62] = ctx->bitlen >> 8;
172 ctx->data[61] = ctx->bitlen >> 16;
173 ctx->data[60] = ctx->bitlen >> 24;
174 ctx->data[59] = ctx->bitlen >> 32;
175 ctx->data[58] = ctx->bitlen >> 40;
176 ctx->data[57] = ctx->bitlen >> 48;
177 ctx->data[56] = ctx->bitlen >> 56;
178 sha256_transform(ctx, ctx->data);
182 for(i = 0; i < 4; ++i) {
183 hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff;
184 hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff;
185 hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff;
186 hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff;
187 hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff;
188 hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0x000000ff;
189 hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0x000000ff;
190 hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0x000000ff;
194 static void sha256(
const void* data,
int size, byte_t hash[32]) {
197 sha256_init(&context);
198 sha256_update(&context, (
const byte_t*)data, size);
199 sha256_final(&context, hash);
207 sha256::byte_t hash[32];
208 sha256::sha256(data, length, hash);
210 std::ostringstream ss;
211 for(
int i = 0; i < 32; ++i)
212 ss << std::hex << std::uppercase << static_cast<int>(hash[i]);
virtual std::string hash(const void *data, int length) override
Compute 256 bit hash using SHA-1.
static const char * Name
Identifier of the hash.
Namespace of the serialbox library.