1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
// Copyright (c) 2026 Progmasoft.
// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0
#include "Hash.hpp"
#include <cstring>
#include <fstream>
namespace Progmasoft::Catch3::Runner
{
namespace
{
constexpr std::array<std::uint32_t, 64> kRoundConstants = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b,
0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01,
0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7,
0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152,
0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc,
0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819,
0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08,
0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f,
0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
};
constexpr std::uint32_t
RotateRight(std::uint32_t value, int count) noexcept
{
return (value >> count) | (value << (32 - count));
}
} // namespace
Sha256::Sha256()
: state_{0x6a09e667,
0xbb67ae85,
0x3c6ef372,
0xa54ff53a,
0x510e527f,
0x9b05688c,
0x1f83d9ab,
0x5be0cd19}
{
}
void
Sha256::Update(const void *data, std::size_t size)
{
const auto *bytes = static_cast<const std::uint8_t *>(data);
totalSize_ += size;
while (size > 0)
{
const std::size_t room = buffer_.size() - bufferSize_;
const std::size_t take = size < room ? size : room;
std::memcpy(buffer_.data() + bufferSize_, bytes, take);
bufferSize_ += take;
bytes += take;
size -= take;
if (bufferSize_ == buffer_.size())
{
Compress(buffer_.data());
bufferSize_ = 0;
}
}
}
void
Sha256::Compress(const std::uint8_t *block)
{
std::array<std::uint32_t, 64> schedule{};
for (int index = 0; index < 16; ++index)
{
schedule[index] =
(static_cast<std::uint32_t>(block[index * 4]) << 24) |
(static_cast<std::uint32_t>(block[index * 4 + 1]) << 16) |
(static_cast<std::uint32_t>(block[index * 4 + 2]) << 8) |
static_cast<std::uint32_t>(block[index * 4 + 3]);
}
for (int index = 16; index < 64; ++index)
{
const std::uint32_t s0 = RotateRight(schedule[index - 15], 7) ^
RotateRight(schedule[index - 15], 18) ^
(schedule[index - 15] >> 3);
const std::uint32_t s1 = RotateRight(schedule[index - 2], 17) ^
RotateRight(schedule[index - 2], 19) ^
(schedule[index - 2] >> 10);
schedule[index] =
schedule[index - 16] + s0 + schedule[index - 7] + s1;
}
std::uint32_t a = state_[0];
std::uint32_t b = state_[1];
std::uint32_t c = state_[2];
std::uint32_t d = state_[3];
std::uint32_t e = state_[4];
std::uint32_t f = state_[5];
std::uint32_t g = state_[6];
std::uint32_t h = state_[7];
for (int index = 0; index < 64; ++index)
{
const std::uint32_t s1 =
RotateRight(e, 6) ^ RotateRight(e, 11) ^ RotateRight(e, 25);
const std::uint32_t choice = (e & f) ^ (~e & g);
const std::uint32_t first =
h + s1 + choice + kRoundConstants[index] + schedule[index];
const std::uint32_t s0 =
RotateRight(a, 2) ^ RotateRight(a, 13) ^ RotateRight(a, 22);
const std::uint32_t majority = (a & b) ^ (a & c) ^ (b & c);
const std::uint32_t second = s0 + majority;
h = g;
g = f;
f = e;
e = d + first;
d = c;
c = b;
b = a;
a = first + second;
}
state_[0] += a;
state_[1] += b;
state_[2] += c;
state_[3] += d;
state_[4] += e;
state_[5] += f;
state_[6] += g;
state_[7] += h;
}
std::string
Sha256::HexDigest()
{
const std::uint64_t bitCount = totalSize_ * 8;
const std::uint8_t marker = 0x80;
Update(&marker, 1);
const std::uint8_t zero = 0;
while (bufferSize_ != 56)
{
Update(&zero, 1);
}
std::uint8_t length[8];
for (int index = 0; index < 8; ++index)
{
length[index] =
static_cast<std::uint8_t>(bitCount >> (56 - index * 8));
}
Update(length, sizeof(length));
static constexpr char kDigits[] = "0123456789abcdef";
std::string text;
text.reserve(64);
for (const std::uint32_t word : state_)
{
for (int shift = 28; shift >= 0; shift -= 4)
{
text.push_back(kDigits[(word >> shift) & 0xF]);
}
}
return text;
}
std::string
Sha256Hex(std::string_view text)
{
Sha256 hash;
hash.Update(text);
return hash.HexDigest();
}
Expected<std::string>
Sha256File(const std::filesystem::path &path)
{
std::ifstream file(path, std::ios::binary);
if (!file)
{
return Error{"cannot read '" + path.string() + "'"};
}
Sha256 hash;
char buffer[1 << 16];
while (file)
{
file.read(buffer, sizeof(buffer));
const auto count = file.gcount();
if (count > 0)
{
hash.Update(buffer, static_cast<std::size_t>(count));
}
}
if (file.bad())
{
return Error{"cannot read '" + path.string() + "'"};
}
return hash.HexDigest();
}
std::uint64_t
Fnv1a64(std::string_view text) noexcept
{
std::uint64_t hash = 0xcbf29ce484222325ULL;
for (const char character : text)
{
hash ^= static_cast<unsigned char>(character);
hash *= 0x100000001b3ULL;
}
return hash;
}
std::string
ToHex(std::uint64_t value)
{
static constexpr char kDigits[] = "0123456789abcdef";
std::string text(16, '0');
for (int index = 15; index >= 0; --index)
{
text[static_cast<std::size_t>(index)] = kDigits[value & 0xF];
value >>= 4;
}
return text;
}
} // namespace Progmasoft::Catch3::Runner
|