Joedb 10.2.1
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 <cstring>
7
8namespace joedb
9{
10 /// Rotate x n bits to the right
11 constexpr uint32_t rotr(uint32_t x, uint8_t n)
12 {
13 return (x >> n) | (x << ((-n) & 31));
14 }
15
16 /// Compute SHA 256 hash code: https://en.wikipedia.org/wiki/SHA-2
17 ///
18 /// @ingroup journal
19 class SHA_256
20 {
21 private:
22 static constexpr std::array<uint32_t, 8> hash_init
23 {
24 {
25 0x6a09e667,
26 0xbb67ae85,
27 0x3c6ef372,
28 0xa54ff53a,
29 0x510e527f,
30 0x9b05688c,
31 0x1f83d9ab,
32 0x5be0cd19
33 }
34 };
35
36 static constexpr std::array<uint32_t, 64> k
37 {
38 {
39 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
40 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
41 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
42 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
43 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
44 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
45 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
46 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
47 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
48 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
49 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
50 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
51 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
52 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
53 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
54 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
55 }
56 };
57
58 public:
59 using Hash = std::array<uint32_t, 8>;
60
61 private:
62 Hash hash;
63
64 public:
65 SHA_256(): hash(hash_init) {}
66 const Hash &get_hash() const {return hash;}
67 static constexpr size_t chunk_size = 64;
68
69 /// process 512 bits (32 * 16, 8 * 64) of data, SHA_256::chunk_size bytes
70 void process_chunk(const char *data)
71 {
72 std::array<uint32_t, 64> w;
73
74 {
75 const uint8_t *u8_data = reinterpret_cast<const uint8_t *>(data);
76
77 for (uint32_t i = 0; i < 16; i++)
78 w[i] =
79 (uint32_t(u8_data[4 * i + 0]) << 24) |
80 (uint32_t(u8_data[4 * i + 1]) << 16) |
81 (uint32_t(u8_data[4 * i + 2]) << 8) |
82 (uint32_t(u8_data[4 * i + 3]) );
83 }
84
85 for (uint32_t i = 16; i < 64; i++)
86 {
87 const uint32_t w0 = w[i - 15];
88 const uint32_t s0 = rotr(w0, 7) ^ rotr(w0, 18) ^ (w0 >> 3);
89 const uint32_t w1 = w[i - 2];
90 const uint32_t s1 = rotr(w1, 17) ^ rotr(w1, 19) ^ (w1 >> 10);
91
92 w[i] = w[i - 16] + s0 + w[i - 7] + s1;
93 }
94
95 Hash x(hash);
96
97 for (uint32_t i = 0; i < 64; i++)
98 {
99 const uint32_t S1 = rotr(x[4], 6) ^ rotr(x[4], 11) ^ rotr(x[4], 25);
100 const uint32_t ch = (x[4] & x[5]) ^ ((~x[4]) & x[6]);
101 const uint32_t temp1 = x[7] + S1 + ch + k[i] + w[i];
102 const uint32_t S0 = rotr(x[0], 2) ^ rotr(x[0], 13) ^ rotr(x[0], 22);
103 const uint32_t maj = (x[0] & x[1]) ^ (x[0] & x[2]) ^ (x[1] & x[2]);
104 const uint32_t temp2 = S0 + maj;
105
106 x[7] = x[6];
107 x[6] = x[5];
108 x[5] = x[4];
109 x[4] = x[3] + temp1;
110 x[3] = x[2];
111 x[2] = x[1];
112 x[1] = x[0];
113 x[0] = temp1 + temp2;
114 }
115
116 for (uint32_t i = 0; i < 8; i++)
117 hash[i] += x[i];
118 }
119
120 /// process last bytes of the sequence
121 ///
122 /// @param data points to the final n bytes, 0 <= n < 64
123 /// @param total_length_in_bytes is the length of the whole sequence
125 (
126 const char * const data,
127 const uint64_t total_length_in_bytes
128 )
129 {
130 std::array<uint32_t, 32> final_chunks{};
131 uint8_t *byte_buffer = reinterpret_cast<uint8_t *>(&final_chunks[0]);
132 uint32_t n = uint32_t(total_length_in_bytes & 0x3fULL);
133 std::memcpy(byte_buffer, data, n);
134 byte_buffer[n] = 0x80;
135
136 const int chunk_count = n + 9 <= 64 ? 1 : 2;
137
138 {
139 uint64_t length_in_bits = total_length_in_bytes * 8;
140 for (int index = chunk_count * 64, i = 8; --index, --i >= 0;)
141 {
142 byte_buffer[index] = uint8_t(length_in_bits);
143 length_in_bits >>= 8;
144 }
145 }
146
147 for (uint32_t i = 0; i < uint32_t(chunk_count); i++)
148 process_chunk(reinterpret_cast<char *>(&final_chunks[16 * i]));
149 }
150 };
151}
152
153#endif
Compute SHA 256 hash code: https://en.wikipedia.org/wiki/SHA-2.
Definition SHA_256.h:20
const Hash & get_hash() const
Definition SHA_256.h:66
void process_chunk(const char *data)
process 512 bits (32 * 16, 8 * 64) of data, SHA_256::chunk_size bytes
Definition SHA_256.h:70
static constexpr size_t chunk_size
Definition SHA_256.h:67
std::array< uint32_t, 8 > Hash
Definition SHA_256.h:59
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:125
constexpr uint32_t rotr(uint32_t x, uint8_t n)
Rotate x n bits to the right.
Definition SHA_256.h:11