@@ -573,27 +573,11 @@ namespace http
573573
574574// ----------------------------------------------------------------------------------------------------------------
575575
576-
577576 static constexpr uint32_t rotl (uint32_t x, size_t s)
578577 {
579578 return (x << s) | (x >> (32 - s));
580579 }
581580
582- static constexpr uint32_t f1 (uint32_t b, uint32_t c, uint32_t d)
583- {
584- return d ^ (b & (c ^ d)); // original: f = (b & c) | ((~b) & d);
585- }
586-
587- static constexpr uint32_t f2 (uint32_t b, uint32_t c, uint32_t d)
588- {
589- return b ^ c ^ d;
590- }
591-
592- static constexpr uint32_t f3 (uint32_t b, uint32_t c, uint32_t d)
593- {
594- return (b & c) | (b & d) | (c & d);
595- }
596-
597581 const auto process_sha1_block = [](auto & hash, const auto & block, auto & words)
598582 {
599583 // Initialise buffer
@@ -612,50 +596,29 @@ namespace http
612596 uint32_t c = hash[2 ];
613597 uint32_t d = hash[3 ];
614598 uint32_t e = hash[4 ];
599+ size_t i{0 };
615600
616- // first round
617- for (size_t i = 0 ; i < 4 ; ++i)
601+ const auto fin = [&](const size_t i, const uint32_t k, const uint32_t f)
618602 {
619- const size_t offset = 5 *i;
620- e += rotl (a,5 ) + f1 (b,c,d) + words[offset ] + 0x5a827999 ; b = rotl (b,30 );
621- d += rotl (e,5 ) + f1 (a,b,c) + words[offset+1 ] + 0x5a827999 ; a = rotl (a,30 );
622- c += rotl (d,5 ) + f1 (e,a,b) + words[offset+2 ] + 0x5a827999 ; e = rotl (e,30 );
623- b += rotl (c,5 ) + f1 (d,e,a) + words[offset+3 ] + 0x5a827999 ; d = rotl (d,30 );
624- a += rotl (b,5 ) + f1 (c,d,e) + words[offset+4 ] + 0x5a827999 ; c = rotl (c,30 );
625- }
626-
627- // second round
628- for (size_t i = 4 ; i < 8 ; ++i)
629- {
630- const size_t offset = 5 *i;
631- e += rotl (a,5 ) + f2 (b,c,d) + words[offset ] + 0x6ed9eba1 ; b = rotl (b,30 );
632- d += rotl (e,5 ) + f2 (a,b,c) + words[offset+1 ] + 0x6ed9eba1 ; a = rotl (a,30 );
633- c += rotl (d,5 ) + f2 (e,a,b) + words[offset+2 ] + 0x6ed9eba1 ; e = rotl (e,30 );
634- b += rotl (c,5 ) + f2 (d,e,a) + words[offset+3 ] + 0x6ed9eba1 ; d = rotl (d,30 );
635- a += rotl (b,5 ) + f2 (c,d,e) + words[offset+4 ] + 0x6ed9eba1 ; c = rotl (c,30 );
636- }
603+ const unsigned temp = rotl (a, 5 ) + f + e + k + words[i];
604+ e = d;
605+ d = c;
606+ c = rotl (b, 30 );
607+ b = a;
608+ a = temp;
609+ };
637610
638- // third round
639- for (size_t i = 8 ; i < 12 ; ++i)
640- {
641- const size_t offset = 5 *i;
642- e += rotl (a,5 ) + f3 (b,c,d) + words[offset ] + 0x8f1bbcdc ; b = rotl (b,30 );
643- d += rotl (e,5 ) + f3 (a,b,c) + words[offset+1 ] + 0x8f1bbcdc ; a = rotl (a,30 );
644- c += rotl (d,5 ) + f3 (e,a,b) + words[offset+2 ] + 0x8f1bbcdc ; e = rotl (e,30 );
645- b += rotl (c,5 ) + f3 (d,e,a) + words[offset+3 ] + 0x8f1bbcdc ; d = rotl (d,30 );
646- a += rotl (b,5 ) + f3 (c,d,e) + words[offset+4 ] + 0x8f1bbcdc ; c = rotl (c,30 );
647- }
611+ for (; i < 20 ; ++i)
612+ fin (i, 0x5A827999 , (b & c) | (~b & d));
648613
649- // fourth round
650- for (size_t i = 12 ; i < 16 ; ++i)
651- {
652- const size_t offset = 5 *i;
653- e += rotl (a,5 ) + f2 (b,c,d) + words[offset ] + 0xca62c1d6 ; b = rotl (b,30 );
654- d += rotl (e,5 ) + f2 (a,b,c) + words[offset+1 ] + 0xca62c1d6 ; a = rotl (a,30 );
655- c += rotl (d,5 ) + f2 (e,a,b) + words[offset+2 ] + 0xca62c1d6 ; e = rotl (e,30 );
656- b += rotl (c,5 ) + f2 (d,e,a) + words[offset+3 ] + 0xca62c1d6 ; d = rotl (d,30 );
657- a += rotl (b,5 ) + f2 (c,d,e) + words[offset+4 ] + 0xca62c1d6 ; c = rotl (c,30 );
658- }
614+ for (; i < 40 ; ++i)
615+ fin (i, 0x6ED9EBA1 , b ^ c ^ d);
616+
617+ for (; i < 60 ; ++i)
618+ fin (i, 0x8F1BBCDC , (b & c) | (b & d) | (c & d));
619+
620+ for (; i < 80 ; ++i)
621+ fin (i, 0xCA62C1D6 , b ^ c ^ d);
659622
660623 // update hash
661624 hash[0 ] += a;
0 commit comments