37 inline static uint32_t rol32(uint32_t value,
int bits)
39 return (value << bits) | (value >> (32 - bits));
48 data[i] = Endian::readB32(&data[i]);
53 return data[i & 15] = rol32(
54 data[(i + 13) & 15] ^ data[(i + 8) & 15] ^
55 data[(i + 2) & 15] ^ data[ i & 15]
63 void r0(uint32_t v, uint32_t& w, uint32_t x, uint32_t y, uint32_t& z,
int i)
65 z += ((w & (x ^ y)) ^ y) + next0(i) + 0x5A827999 + rol32(v, 5);
68 void r1(uint32_t v, uint32_t& w, uint32_t x, uint32_t y, uint32_t& z,
int i)
70 z += ((w & (x ^ y)) ^ y) + next(i) + 0x5A827999 + rol32(v, 5);
73 void r2(uint32_t v, uint32_t& w, uint32_t x, uint32_t y, uint32_t& z,
int i)
75 z += (w ^ x ^ y) + next(i) + 0x6ED9EBA1 + rol32(v, 5);
78 void r3(uint32_t v, uint32_t& w, uint32_t x, uint32_t y, uint32_t& z,
int i)
80 z += (((w | x) & y) | (w & x)) + next(i) + 0x8F1BBCDC + rol32(v, 5);
83 void r4(uint32_t v, uint32_t& w, uint32_t x, uint32_t y, uint32_t& z,
int i)
85 z += (w ^ x ^ y) + next(i) + 0xCA62C1D6 + rol32(v, 5);
92 memcpy(data, buffer,
sizeof(data));
105 if (str.
size() != 40) {
106 throw MSXException(
"Invalid sha1, should be exactly 40 digits long: " + str);
111 static inline unsigned hex(
char x,
const char* str)
113 if ((
'0' <= x) && (x <=
'9'))
return x -
'0';
114 if ((
'a' <= x) && (x <=
'f'))
return x -
'a' + 10;
115 if ((
'A' <= x) && (x <=
'F'))
return x -
'A' + 10;
116 throw MSXException(
"Invalid sha1, digits should be 0-9, a-f: " +
122 for (
int i = 0; i < 5; ++i) {
124 for (
int j = 0; j < 8; ++j) {
132 static inline char digit(
unsigned x)
134 return (x < 10) ? (x +
'0') : (x - 10 +
'a');
140 for (
int i = 0; i < 5; ++i) {
141 for (
int j = 28; j >= 0; j -= 4) {
142 *p++ = digit((a[i] >> j) & 0xf);
145 return string(buf, 40);
150 for (
int i = 0; i < 5; ++i) {
151 if (a[i] != 0)
return false;
157 for (
int i = 0; i < 5; ++i) {
164 for (
int i = 0; i < 5; ++i) {
165 if (a[i] != other.a[i])
return false;
171 return !(*
this == other);
176 for (
int i = 0; i < 5-1; ++i) {
177 if (a[i] != other.a[i])
return a[i] < other.a[i];
179 return a[5-1] < other.a[5-1];
188 m_state.a[0] = 0x67452301;
189 m_state.a[1] = 0xEFCDAB89;
190 m_state.a[2] = 0x98BADCFE;
191 m_state.a[3] = 0x10325476;
192 m_state.a[4] = 0xC3D2E1F0;
198 void SHA1::transform(
const uint8_t buffer[64])
203 uint32_t a = m_state.a[0];
204 uint32_t b = m_state.a[1];
205 uint32_t c = m_state.a[2];
206 uint32_t d = m_state.a[3];
207 uint32_t e = m_state.a[4];
210 block.r0(a,b,c,d,e, 0); block.r0(e,a,b,c,d, 1); block.r0(d,e,a,b,c, 2);
211 block.r0(c,d,e,a,b, 3); block.r0(b,c,d,e,a, 4); block.r0(a,b,c,d,e, 5);
212 block.r0(e,a,b,c,d, 6); block.r0(d,e,a,b,c, 7); block.r0(c,d,e,a,b, 8);
213 block.r0(b,c,d,e,a, 9); block.r0(a,b,c,d,e,10); block.r0(e,a,b,c,d,11);
214 block.r0(d,e,a,b,c,12); block.r0(c,d,e,a,b,13); block.r0(b,c,d,e,a,14);
215 block.r0(a,b,c,d,e,15); block.r1(e,a,b,c,d,16); block.r1(d,e,a,b,c,17);
216 block.r1(c,d,e,a,b,18); block.r1(b,c,d,e,a,19); block.r2(a,b,c,d,e,20);
217 block.r2(e,a,b,c,d,21); block.r2(d,e,a,b,c,22); block.r2(c,d,e,a,b,23);
218 block.r2(b,c,d,e,a,24); block.r2(a,b,c,d,e,25); block.r2(e,a,b,c,d,26);
219 block.r2(d,e,a,b,c,27); block.r2(c,d,e,a,b,28); block.r2(b,c,d,e,a,29);
220 block.r2(a,b,c,d,e,30); block.r2(e,a,b,c,d,31); block.r2(d,e,a,b,c,32);
221 block.r2(c,d,e,a,b,33); block.r2(b,c,d,e,a,34); block.r2(a,b,c,d,e,35);
222 block.r2(e,a,b,c,d,36); block.r2(d,e,a,b,c,37); block.r2(c,d,e,a,b,38);
223 block.r2(b,c,d,e,a,39); block.r3(a,b,c,d,e,40); block.r3(e,a,b,c,d,41);
224 block.r3(d,e,a,b,c,42); block.r3(c,d,e,a,b,43); block.r3(b,c,d,e,a,44);
225 block.r3(a,b,c,d,e,45); block.r3(e,a,b,c,d,46); block.r3(d,e,a,b,c,47);
226 block.r3(c,d,e,a,b,48); block.r3(b,c,d,e,a,49); block.r3(a,b,c,d,e,50);
227 block.r3(e,a,b,c,d,51); block.r3(d,e,a,b,c,52); block.r3(c,d,e,a,b,53);
228 block.r3(b,c,d,e,a,54); block.r3(a,b,c,d,e,55); block.r3(e,a,b,c,d,56);
229 block.r3(d,e,a,b,c,57); block.r3(c,d,e,a,b,58); block.r3(b,c,d,e,a,59);
230 block.r4(a,b,c,d,e,60); block.r4(e,a,b,c,d,61); block.r4(d,e,a,b,c,62);
231 block.r4(c,d,e,a,b,63); block.r4(b,c,d,e,a,64); block.r4(a,b,c,d,e,65);
232 block.r4(e,a,b,c,d,66); block.r4(d,e,a,b,c,67); block.r4(c,d,e,a,b,68);
233 block.r4(b,c,d,e,a,69); block.r4(a,b,c,d,e,70); block.r4(e,a,b,c,d,71);
234 block.r4(d,e,a,b,c,72); block.r4(c,d,e,a,b,73); block.r4(b,c,d,e,a,74);
235 block.r4(a,b,c,d,e,75); block.r4(e,a,b,c,d,76); block.r4(d,e,a,b,c,77);
236 block.r4(c,d,e,a,b,78); block.r4(b,c,d,e,a,79);
249 assert(!m_finalized);
250 uint32_t j = (m_count >> 3) & 63;
252 m_count += uint64_t(len) << 3;
255 if ((j + len) > 63) {
256 memcpy(&m_buffer[j], data, (i = 64 - j));
258 for (; i + 63 < len; i += 64) {
265 memcpy(&m_buffer[j], &data[i], len - i);
268 void SHA1::finalize()
270 assert(!m_finalized);
271 uint8_t finalcount[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
272 for (
int i = 0; i < 8; i++) {
273 finalcount[i] = uint8_t(m_count >> ((7 - i) * 8));
276 update(reinterpret_cast<const uint8_t*>(
"\200"), 1);
277 while ((m_count & 504) != 448) {
278 update(reinterpret_cast<const uint8_t*>(
"\0"), 1);
286 if (!m_finalized) finalize();
297 static void reportProgress(
const string& filename,
size_t percentage,
CliComm& cliComm,
EventDistributor& distributor) {
304 if (len < 10*1024*1024) {
305 return calc(data, len);
308 static const size_t NUMBER_OF_STEPS = 100;
310 auto stepSize = len / NUMBER_OF_STEPS;
311 auto remainder = len % NUMBER_OF_STEPS;
313 reportProgress(filename, 0, cliComm, distributor);
314 for (
size_t i = 0; i < (NUMBER_OF_STEPS - 1); ++i) {
315 sha1.
update(&data[offset], stepSize);
317 reportProgress(filename, i + 1, cliComm, distributor);
319 sha1.
update(data + offset, stepSize + remainder);
320 reportProgress(filename, 100, cliComm, distributor);