Joedb 9.1.4
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
SHA_256.h
Go to the documentation of this file.
1#ifndef joedb_SHA_256_declared
2#define joedb_SHA_256_declared
3
4#include <array>
5#include <stdint.h>
6#include <algorithm>
7
8// https://en.wikipedia.org/wiki/SHA-2
9
10namespace joedb
11{
12 ////////////////////////////////////////////////////////////////////////////
13 constexpr uint32_t rotr(uint32_t x, uint8_t n)
14 ////////////////////////////////////////////////////////////////////////////
15 {
16 return (x >> n) | (x << ((-n) & 31));
17 }
18
19 /// @ingroup journal
20 class SHA_256
21 {
22 private:
23 static constexpr std::array<uint32_t, 8> hash_init
24 {
25 {
26 0x6a09e667,
27 0xbb67ae85,
28 0x3c6ef372,
29 0xa54ff53a,
30 0x510e527f,
31 0x9b05688c,
32 0x1f83d9ab,
33 0x5be0cd19
34 }
35 };
36
37 static constexpr std::array<uint32_t, 64> k
38 {
39 {
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
56 }
57 };
58
59 public:
60 typedef std::array<uint32_t, 8> Hash;
61
62 private:
63 Hash hash;
64
65 public:
66 SHA_256(): hash(hash_init) {}
67 const Hash &get_hash() const {return hash;}
68 static constexpr size_t chunk_size = 64;
69
70 /// process 512 bits (32 * 16, 8 * 64) of data, SHA_256::chunk_size bytes
71 void process_chunk(const char *data)
72 {
73 std::array<uint32_t, 64> w;
74
75 {
76 const uint8_t *u8_data = reinterpret_cast<const uint8_t *>(data);
77
78 for (uint32_t i = 0; i < 16; i++)
79 w[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]) );
84 }
85
86 for (uint32_t i = 16; i < 64; i++)
87 {
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);
92
93 w[i] = w[i - 16] + s0 + w[i - 7] + s1;
94 }
95
96 Hash x(hash);
97
98 for (uint32_t i = 0; i < 64; i++)
99 {
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;
106
107 x[7] = x[6];
108 x[6] = x[5];
109 x[5] = x[4];
110 x[4] = x[3] + temp1;
111 x[3] = x[2];
112 x[2] = x[1];
113 x[1] = x[0];
114 x[0] = temp1 + temp2;
115 };
116
117 for (uint32_t i = 0; i < 8; i++)
118 hash[i] += x[i];
119 }
120
121 /// process last bytes of the sequence
122 ///
123 /// @param data points to the final n bytes, 0 <= n < 64
124 /// @param total_length_in_bytes is the length of the whole sequence
126 (
127 const char * const data,
128 const uint64_t total_length_in_bytes
129 )
130 {
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;
136
137 const int chunk_count = n + 9 <= 64 ? 1 : 2;
138
139 {
140 uint64_t length_in_bits = total_length_in_bytes * 8;
141 for (int index = chunk_count * 64, i = 8; --index, --i >= 0;)
142 {
143 byte_buffer[index] = uint8_t(length_in_bits);
144 length_in_bits >>= 8;
145 }
146 }
147
148 for (uint32_t i = 0; i < uint32_t(chunk_count); i++)
149 process_chunk(reinterpret_cast<char *>(&final_chunks[16 * i]));
150 }
151 };
152}
153
154#endif
const Hash & get_hash() const
Definition SHA_256.h:67
void process_chunk(const char *data)
process 512 bits (32 * 16, 8 * 64) of data, SHA_256::chunk_size bytes
Definition SHA_256.h:71
std::array< uint32_t, 8 > Hash
Definition SHA_256.h:60
static constexpr size_t chunk_size
Definition SHA_256.h:68
void process_final_chunk(const char *const data, const uint64_t total_length_in_bytes)
process last bytes of the sequence
Definition SHA_256.h:126
Definition Blob.h:7
constexpr uint32_t rotr(uint32_t x, uint8_t n)
Definition SHA_256.h:13