fix 64n+56 bug

This commit is contained in:
ziplantil 2024-05-08 20:58:59 +03:00
parent b12baa9715
commit 9c1f6e4b73
2 changed files with 5 additions and 4 deletions

View File

@ -126,9 +126,9 @@ std::array<unsigned char, 16> MD5::digest() const noexcept {
auto buf = tmpbuf_; auto buf = tmpbuf_;
std::size_t n = tmpbuf_n_; std::size_t n = tmpbuf_n_;
if (n > 56) { if (n >= 56) {
// must append current block, length won't fit. // must append current block, required padding bit and length won't fit.
// n is never buf.size() yet // n is never buf.size() (= 64) yet
buf[n++] = 0x80U; buf[n++] = 0x80U;
while (n < buf.size()) while (n < buf.size())
buf[n++] = 0; buf[n++] = 0;
@ -137,7 +137,7 @@ std::array<unsigned char, 16> MD5::digest() const noexcept {
// all zeroes, except for length // all zeroes, except for length
std::fill(buf.begin(), buf.begin() + 56, 0); std::fill(buf.begin(), buf.begin() + 56, 0);
} else if (n < 56) { } else { // n < 56
// append padding to tmpbuf and then the length // append padding to tmpbuf and then the length
buf[n++] = 0x80U; buf[n++] = 0x80U;
while (n < 56) while (n < 56)

View File

@ -52,6 +52,7 @@ TEST(D3, MD5) {
EXPECT_EQ(hexdigest_after_n_rounds_of_fuzz(1), "c56c005504f5b9b17df9d83f1106e9b2"); EXPECT_EQ(hexdigest_after_n_rounds_of_fuzz(1), "c56c005504f5b9b17df9d83f1106e9b2");
EXPECT_EQ(hexdigest_after_n_rounds_of_fuzz(2), "98327a3f4ee311c9bdc8613508922c95"); EXPECT_EQ(hexdigest_after_n_rounds_of_fuzz(2), "98327a3f4ee311c9bdc8613508922c95");
EXPECT_EQ(hexdigest_after_n_rounds_of_fuzz(4), "f2343ce526646c087df92d708e883675"); EXPECT_EQ(hexdigest_after_n_rounds_of_fuzz(4), "f2343ce526646c087df92d708e883675");
EXPECT_EQ(hexdigest_after_n_rounds_of_fuzz(8), "73396b338a0a769c4e6ae5b7b0bc187e");
EXPECT_EQ(hexdigest_after_n_rounds_of_fuzz(100), "99b30c1dcc42d97c3d914c26a14bb1d7"); EXPECT_EQ(hexdigest_after_n_rounds_of_fuzz(100), "99b30c1dcc42d97c3d914c26a14bb1d7");
EXPECT_EQ(hexdigest_after_n_rounds_of_fuzz(1999), "82eeff8c7d574c8232b0ca6ca2c9dd40"); EXPECT_EQ(hexdigest_after_n_rounds_of_fuzz(1999), "82eeff8c7d574c8232b0ca6ca2c9dd40");
} }