39[[nodiscard]]
static constexpr uint32_t rol32(uint32_t value,
int bits)
41 return (value << bits) | (value >> (32 - bits));
46 std::array<uint32_t, 16> data;
48 [[nodiscard]] uint32_t next0(
int i)
53 [[nodiscard]] uint32_t next(
int i)
55 return data[i & 15] = rol32(
56 data[(i + 13) & 15] ^ data[(i + 8) & 15] ^
57 data[(i + 2) & 15] ^ data[ i & 15]
65 void r0(uint32_t v, uint32_t& w, uint32_t x, uint32_t y, uint32_t& z,
int i)
67 z += ((w & (x ^ y)) ^ y) + next0(i) + 0x5A827999 + rol32(v, 5);
70 void r1(uint32_t v, uint32_t& w, uint32_t x, uint32_t y, uint32_t& z,
int i)
72 z += ((w & (x ^ y)) ^ y) + next(i) + 0x5A827999 + rol32(v, 5);
75 void r2(uint32_t v, uint32_t& w, uint32_t x, uint32_t y, uint32_t& z,
int i)
77 z += (w ^ x ^ y) + next(i) + 0x6ED9EBA1 + rol32(v, 5);
80 void r3(uint32_t v, uint32_t& w, uint32_t x, uint32_t y, uint32_t& z,
int i)
82 z += (((w | x) & y) | (w & x)) + next(i) + 0x8F1BBCDC + rol32(v, 5);
85 void r4(uint32_t v, uint32_t& w, uint32_t x, uint32_t y, uint32_t& z,
int i)
87 z += (w ^ x ^ y) + next(i) + 0xCA62C1D6 + rol32(v, 5);
94 memcpy(data.data(), buffer.data(),
sizeof(data));
107 if (hex.size() != 40) {
108 throw MSXException(
"Invalid sha1, should be exactly 40 digits long: ", hex);
115[[nodiscard]]
static inline __m128i _mm_cmpge_epu8(__m128i a, __m128i b)
117 return _mm_cmpeq_epi8(_mm_max_epu8(a, b), a);
120[[nodiscard]]
static inline __m128i _mm_cmple_epu8(__m128i a, __m128i b)
122 return _mm_cmpge_epu8(b, a);
126[[nodiscard]]
static inline int64_t loadSwap64(
const char* s)
133[[nodiscard]]
static unsigned hex(
char x,
const char* str)
135 if ((
'0' <= x) && (x <=
'9'))
return x -
'0';
136 if ((
'a' <= x) && (x <=
'f'))
return x -
'a' + 10;
137 if ((
'A' <= x) && (x <=
'F'))
return x -
'A' + 10;
138 throw MSXException(
"Invalid sha1, digits should be 0-9, a-f: ",
139 std::string_view(str, 40));
149 __m128i s0 = _mm_set_epi64x(loadSwap64(&str[ 8]), loadSwap64(&str[ 0]));
150 __m128i s1 = _mm_set_epi64x(loadSwap64(&str[24]), loadSwap64(&str[16]));
151 __m128i s2 = _mm_set_epi64x(
'0' * 0x0101010101010101, loadSwap64(&str[32]));
154 __m128i cc0 = _mm_set1_epi8(
char(-
'0'));
155 __m128i s0_0 = _mm_add_epi8(s0, cc0);
156 __m128i s1_0 = _mm_add_epi8(s1, cc0);
157 __m128i s2_0 = _mm_add_epi8(s2, cc0);
160 __m128i c32 = _mm_set1_epi8(32);
161 __m128i cca = _mm_set1_epi8(
char(-
'a'));
162 __m128i s0_a = _mm_add_epi8(_mm_or_si128(s0, c32), cca);
163 __m128i s1_a = _mm_add_epi8(_mm_or_si128(s1, c32), cca);
164 __m128i s2_a = _mm_add_epi8(_mm_or_si128(s2, c32), cca);
167 __m128i c9 = _mm_set1_epi8(9);
168 __m128i c0_0 = _mm_cmple_epu8(s0_0, c9);
169 __m128i c1_0 = _mm_cmple_epu8(s1_0, c9);
170 __m128i c2_0 = _mm_cmple_epu8(s2_0, c9);
173 __m128i c5 = _mm_set1_epi8(5);
174 __m128i c0_a = _mm_cmple_epu8(s0_a, c5);
175 __m128i c1_a = _mm_cmple_epu8(s1_a, c5);
176 __m128i c2_a = _mm_cmple_epu8(s2_a, c5);
179 __m128i ok0 = _mm_or_si128(c0_0, c0_a);
180 __m128i ok1 = _mm_or_si128(c1_0, c1_a);
181 __m128i ok2 = _mm_or_si128(c2_0, c2_a);
182 __m128i ok = _mm_and_si128(_mm_and_si128(ok0, ok1), ok2);
183 if (_mm_movemask_epi8(ok) != 0xffff) [[unlikely]] {
184 throw MSXException(
"Invalid sha1, digits should be 0-9, a-f: ",
185 std::string_view(str.data(), 40));
189 __m128i d0_0 = _mm_and_si128(s0_0, c0_0);
190 __m128i d1_0 = _mm_and_si128(s1_0, c1_0);
191 __m128i d2_0 = _mm_and_si128(s2_0, c2_0);
194 __m128i c10 = _mm_set1_epi8(10);
195 __m128i d0_a = _mm_and_si128(_mm_add_epi8(s0_a, c10), c0_a);
196 __m128i d1_a = _mm_and_si128(_mm_add_epi8(s1_a, c10), c1_a);
197 __m128i d2_a = _mm_and_si128(_mm_add_epi8(s2_a, c10), c2_a);
200 __m128i d0 = _mm_or_si128(d0_0, d0_a);
201 __m128i d1 = _mm_or_si128(d1_0, d1_a);
202 __m128i d2 = _mm_or_si128(d2_0, d2_a);
205 __m128i c00ff = _mm_set1_epi16(0x00ff);
206 __m128i e0 = _mm_and_si128(_mm_or_si128(d0, _mm_srli_epi16(d0, 4)), c00ff);
207 __m128i e1 = _mm_and_si128(_mm_or_si128(d1, _mm_srli_epi16(d1, 4)), c00ff);
208 __m128i e2 = _mm_and_si128(_mm_or_si128(d2, _mm_srli_epi16(d2, 4)), c00ff);
209 __m128i f0 = _mm_packus_epi16(e0, e0);
210 __m128i f1 = _mm_packus_epi16(e1, e1);
211 __m128i f2 = _mm_packus_epi16(e2, e2);
214 _mm_storeu_si128(std::bit_cast<__m128i*>(a.data()), _mm_unpacklo_epi64(f0, f1));
215 a[4] = _mm_cvtsi128_si32(f2);
218 const char* p = str.data();
223 t |= hex(*p++, str.data());
230[[nodiscard]]
static constexpr char digit(
unsigned x)
232 return narrow<char>((x < 10) ? (x +
'0') : (x - 10 +
'a'));
236 std::array<char, 40> buf;
238 for (
const auto& ai : a) {
239 for (
int j = 28; j >= 0; j -= 4) {
240 buf[i++] = digit((ai >> j) & 0xf);
243 return {buf.data(), buf.size()};
261 m_state.a[0] = 0x67452301;
262 m_state.a[1] = 0xEFCDAB89;
263 m_state.a[2] = 0x98BADCFE;
264 m_state.a[3] = 0x10325476;
265 m_state.a[4] = 0xC3D2E1F0;
268void SHA1::transform(std::span<const uint8_t, 64> buffer)
273 uint32_t a = m_state.a[0];
274 uint32_t b = m_state.a[1];
275 uint32_t c = m_state.a[2];
276 uint32_t d = m_state.a[3];
277 uint32_t e = m_state.a[4];
280 block.r0(a,b,c,d,e, 0); block.r0(e,a,b,c,d, 1); block.r0(d,e,a,b,c, 2);
281 block.r0(c,d,e,a,b, 3); block.r0(b,c,d,e,a, 4); block.r0(a,b,c,d,e, 5);
282 block.r0(e,a,b,c,d, 6); block.r0(d,e,a,b,c, 7); block.r0(c,d,e,a,b, 8);
283 block.r0(b,c,d,e,a, 9); block.r0(a,b,c,d,e,10); block.r0(e,a,b,c,d,11);
284 block.r0(d,e,a,b,c,12); block.r0(c,d,e,a,b,13); block.r0(b,c,d,e,a,14);
285 block.r0(a,b,c,d,e,15); block.r1(e,a,b,c,d,16); block.r1(d,e,a,b,c,17);
286 block.r1(c,d,e,a,b,18); block.r1(b,c,d,e,a,19); block.r2(a,b,c,d,e,20);
287 block.r2(e,a,b,c,d,21); block.r2(d,e,a,b,c,22); block.r2(c,d,e,a,b,23);
288 block.r2(b,c,d,e,a,24); block.r2(a,b,c,d,e,25); block.r2(e,a,b,c,d,26);
289 block.r2(d,e,a,b,c,27); block.r2(c,d,e,a,b,28); block.r2(b,c,d,e,a,29);
290 block.r2(a,b,c,d,e,30); block.r2(e,a,b,c,d,31); block.r2(d,e,a,b,c,32);
291 block.r2(c,d,e,a,b,33); block.r2(b,c,d,e,a,34); block.r2(a,b,c,d,e,35);
292 block.r2(e,a,b,c,d,36); block.r2(d,e,a,b,c,37); block.r2(c,d,e,a,b,38);
293 block.r2(b,c,d,e,a,39); block.r3(a,b,c,d,e,40); block.r3(e,a,b,c,d,41);
294 block.r3(d,e,a,b,c,42); block.r3(c,d,e,a,b,43); block.r3(b,c,d,e,a,44);
295 block.r3(a,b,c,d,e,45); block.r3(e,a,b,c,d,46); block.r3(d,e,a,b,c,47);
296 block.r3(c,d,e,a,b,48); block.r3(b,c,d,e,a,49); block.r3(a,b,c,d,e,50);
297 block.r3(e,a,b,c,d,51); block.r3(d,e,a,b,c,52); block.r3(c,d,e,a,b,53);
298 block.r3(b,c,d,e,a,54); block.r3(a,b,c,d,e,55); block.r3(e,a,b,c,d,56);
299 block.r3(d,e,a,b,c,57); block.r3(c,d,e,a,b,58); block.r3(b,c,d,e,a,59);
300 block.r4(a,b,c,d,e,60); block.r4(e,a,b,c,d,61); block.r4(d,e,a,b,c,62);
301 block.r4(c,d,e,a,b,63); block.r4(b,c,d,e,a,64); block.r4(a,b,c,d,e,65);
302 block.r4(e,a,b,c,d,66); block.r4(d,e,a,b,c,67); block.r4(c,d,e,a,b,68);
303 block.r4(b,c,d,e,a,69); block.r4(a,b,c,d,e,70); block.r4(e,a,b,c,d,71);
304 block.r4(d,e,a,b,c,72); block.r4(c,d,e,a,b,73); block.r4(b,c,d,e,a,74);
305 block.r4(a,b,c,d,e,75); block.r4(e,a,b,c,d,76); block.r4(d,e,a,b,c,77);
306 block.r4(c,d,e,a,b,78); block.r4(b,c,d,e,a,79);
319 assert(!m_finalized);
320 uint32_t j = m_count & 63;
322 size_t len = data.size();
326 if ((j + len) > 63) {
330 for (; i + 63 < len; i += 64) {
331 transform(subspan<64>(data, i));
342 assert(!m_finalized);
344 uint32_t j = m_count & 63;
345 m_buffer[j++] = 0x80;
353 memcpy(&m_buffer[56], &finalCount, 8);
361 if (!m_finalized) finalize();
Helper class to perform a sha1 calculation.
Sha1Sum digest()
Get the final hash.
void update(std::span< const uint8_t > data)
Incrementally calculate the hash value.
static Sha1Sum calc(std::span< const uint8_t > data)
Easier to use interface, if you can pass all data in one go.
This class represents the result of a sha1 calculation (a 160-bit value).
void parse40(std::span< const char, 40 > str)
Parse from a 40-character long buffer.
std::string toString() const
void r1(uint32_t v, uint32_t &w, uint32_t x, uint32_t y, uint32_t &z, int i)
void r2(uint32_t v, uint32_t &w, uint32_t x, uint32_t y, uint32_t &z, int i)
void r4(uint32_t v, uint32_t &w, uint32_t x, uint32_t y, uint32_t &z, int i)
WorkspaceBlock(std::span< const uint8_t, 64 > buffer)
void r0(uint32_t v, uint32_t &w, uint32_t x, uint32_t y, uint32_t &z, int i)
void r3(uint32_t v, uint32_t &w, uint32_t x, uint32_t y, uint32_t &z, int i)
ALWAYS_INLINE uint64_t read_UA_B64(const void *p)
uint32_t readB32(const void *p)
This file implemented 3 utility functions:
constexpr bool all_of(InputRange &&range, UnaryPredicate pred)
constexpr void fill(ForwardRange &&range, const T &value)
constexpr auto copy(InputRange &&range, OutputIter out)
constexpr auto subspan(Range &&range, size_t offset, size_t count=std::dynamic_extent)
constexpr void repeat(T n, Op op)
Repeat the given operation 'op' 'n' times.