82 std::ostringstream result;
83 for (
size_t i = 0; i < digest.size(); ++i) {
84 result << std::hex << std::setfill(
'0') << std::setw(8);
94 bool const all_hex = std::all_of( hex.begin(), hex.end(), [](
char c ){
95 return std::isxdigit( c );
97 if( hex.size() != 40 || ! all_hex ) {
98 throw std::runtime_error(
"Invalid SHA1 hex string." );
103 for (
size_t i = 0; i < result.size(); ++i) {
108 std::string
const sub = hex.substr( 8*i, 8 );
110 auto const res = strtoul( sub.c_str(), &endptr, 16 );
112 throw std::runtime_error(
"Invalid SHA1 hex string: \"" + hex +
"\"" );
114 result[i] =
static_cast<uint32_t
>(res);
138 buffer_.append( sbuf, count );
147 buffer_to_block_(buffer_, block);
155 std::istringstream is(s);
165 buffer_.append(sbuf, is.gcount());
170 buffer_to_block_(buffer_, block);
188 buffer_ +=
static_cast<char>( 0x80 );
189 size_t orig_size = buffer_.size();
191 buffer_ +=
static_cast<char>( 0x00 );
195 buffer_to_block_(buffer_, block);
209 auto result = digest_;
224 digest_[0] = 0x67452301;
225 digest_[1] = 0xefcdab89;
226 digest_[2] = 0x98badcfe;
227 digest_[3] = 0x10325476;
228 digest_[4] = 0xc3d2e1f0;
235 uint32_t SHA1::rol_(
const uint32_t value,
const size_t bits)
237 return (value << bits) | (value >> (32 - bits));
240 uint32_t SHA1::blk_(
const uint32_t block[
SHA1::BlockInts],
const size_t i)
242 return rol_(block[(i+13)&15] ^ block[(i+8)&15] ^ block[(i+2)&15] ^ block[i], 1);
246 const uint32_t block[
SHA1::BlockInts],
const uint32_t v, uint32_t& w,
const uint32_t x,
247 const uint32_t y, uint32_t& z,
const size_t i
249 z += ((w&(x^y))^y) + block[i] + 0x5a827999 + rol_(v, 5);
254 uint32_t block[
SHA1::BlockInts],
const uint32_t v, uint32_t& w,
const uint32_t x,
255 const uint32_t y, uint32_t& z,
const size_t i
257 block[i] = blk_(block, i);
258 z += ((w&(x^y))^y) + block[i] + 0x5a827999 + rol_(v, 5);
263 uint32_t block[
SHA1::BlockInts],
const uint32_t v, uint32_t& w,
const uint32_t x,
264 const uint32_t y, uint32_t& z,
const size_t i
266 block[i] = blk_(block, i);
267 z += (w^x^y) + block[i] + 0x6ed9eba1 + rol_(v, 5);
272 uint32_t block[
SHA1::BlockInts],
const uint32_t v, uint32_t& w,
const uint32_t x,
273 const uint32_t y, uint32_t& z,
const size_t i
275 block[i] = blk_(block, i);
276 z += (((w|x)&y)|(w&x)) + block[i] + 0x8f1bbcdc + rol_(v, 5);
282 uint32_t block[
SHA1::BlockInts],
const uint32_t v, uint32_t& w,
const uint32_t x,
283 const uint32_t y, uint32_t& z,
const size_t i
285 block[i] = blk_(block, i);
286 z += (w^x^y) + block[i] + 0xca62c1d6 + rol_(v, 5);
295 uint32_t a = digest_[0];
296 uint32_t b = digest_[1];
297 uint32_t c = digest_[2];
298 uint32_t d = digest_[3];
299 uint32_t e = digest_[4];
302 R0_(block, a, b, c, d, e, 0);
303 R0_(block, e, a, b, c, d, 1);
304 R0_(block, d, e, a, b, c, 2);
305 R0_(block, c, d, e, a, b, 3);
306 R0_(block, b, c, d, e, a, 4);
307 R0_(block, a, b, c, d, e, 5);
308 R0_(block, e, a, b, c, d, 6);
309 R0_(block, d, e, a, b, c, 7);
310 R0_(block, c, d, e, a, b, 8);
311 R0_(block, b, c, d, e, a, 9);
312 R0_(block, a, b, c, d, e, 10);
313 R0_(block, e, a, b, c, d, 11);
314 R0_(block, d, e, a, b, c, 12);
315 R0_(block, c, d, e, a, b, 13);
316 R0_(block, b, c, d, e, a, 14);
317 R0_(block, a, b, c, d, e, 15);
318 R1_(block, e, a, b, c, d, 0);
319 R1_(block, d, e, a, b, c, 1);
320 R1_(block, c, d, e, a, b, 2);
321 R1_(block, b, c, d, e, a, 3);
322 R2_(block, a, b, c, d, e, 4);
323 R2_(block, e, a, b, c, d, 5);
324 R2_(block, d, e, a, b, c, 6);
325 R2_(block, c, d, e, a, b, 7);
326 R2_(block, b, c, d, e, a, 8);
327 R2_(block, a, b, c, d, e, 9);
328 R2_(block, e, a, b, c, d, 10);
329 R2_(block, d, e, a, b, c, 11);
330 R2_(block, c, d, e, a, b, 12);
331 R2_(block, b, c, d, e, a, 13);
332 R2_(block, a, b, c, d, e, 14);
333 R2_(block, e, a, b, c, d, 15);
334 R2_(block, d, e, a, b, c, 0);
335 R2_(block, c, d, e, a, b, 1);
336 R2_(block, b, c, d, e, a, 2);
337 R2_(block, a, b, c, d, e, 3);
338 R2_(block, e, a, b, c, d, 4);
339 R2_(block, d, e, a, b, c, 5);
340 R2_(block, c, d, e, a, b, 6);
341 R2_(block, b, c, d, e, a, 7);
342 R3_(block, a, b, c, d, e, 8);
343 R3_(block, e, a, b, c, d, 9);
344 R3_(block, d, e, a, b, c, 10);
345 R3_(block, c, d, e, a, b, 11);
346 R3_(block, b, c, d, e, a, 12);
347 R3_(block, a, b, c, d, e, 13);
348 R3_(block, e, a, b, c, d, 14);
349 R3_(block, d, e, a, b, c, 15);
350 R3_(block, c, d, e, a, b, 0);
351 R3_(block, b, c, d, e, a, 1);
352 R3_(block, a, b, c, d, e, 2);
353 R3_(block, e, a, b, c, d, 3);
354 R3_(block, d, e, a, b, c, 4);
355 R3_(block, c, d, e, a, b, 5);
356 R3_(block, b, c, d, e, a, 6);
357 R3_(block, a, b, c, d, e, 7);
358 R3_(block, e, a, b, c, d, 8);
359 R3_(block, d, e, a, b, c, 9);
360 R3_(block, c, d, e, a, b, 10);
361 R3_(block, b, c, d, e, a, 11);
362 R4_(block, a, b, c, d, e, 12);
363 R4_(block, e, a, b, c, d, 13);
364 R4_(block, d, e, a, b, c, 14);
365 R4_(block, c, d, e, a, b, 15);
366 R4_(block, b, c, d, e, a, 0);
367 R4_(block, a, b, c, d, e, 1);
368 R4_(block, e, a, b, c, d, 2);
369 R4_(block, d, e, a, b, c, 3);
370 R4_(block, c, d, e, a, b, 4);
371 R4_(block, b, c, d, e, a, 5);
372 R4_(block, a, b, c, d, e, 6);
373 R4_(block, e, a, b, c, d, 7);
374 R4_(block, d, e, a, b, c, 8);
375 R4_(block, c, d, e, a, b, 9);
376 R4_(block, b, c, d, e, a, 10);
377 R4_(block, a, b, c, d, e, 11);
378 R4_(block, e, a, b, c, d, 12);
379 R4_(block, d, e, a, b, c, 13);
380 R4_(block, c, d, e, a, b, 14);
381 R4_(block, b, c, d, e, a, 15);
394 void SHA1::buffer_to_block_(
const std::string& buffer, uint32_t block[
SHA1::BlockInts])
399 block[i] = ( buffer[4*i+3] & 0xff )
400 | ( buffer[4*i+2] & 0xff ) << 8
401 | ( buffer[4*i+1] & 0xff ) << 16
402 | ( buffer[4*i+0] & 0xff ) << 24;