diff --git a/md5/md5.cpp b/md5/md5.cpp index caf0f30d..7b5d617d 100644 --- a/md5/md5.cpp +++ b/md5/md5.cpp @@ -126,9 +126,9 @@ std::array MD5::digest() const noexcept { auto buf = tmpbuf_; std::size_t n = tmpbuf_n_; - if (n > 56) { - // must append current block, length won't fit. - // n is never buf.size() yet + if (n >= 56) { + // must append current block, required padding bit and length won't fit. + // n is never buf.size() (= 64) yet buf[n++] = 0x80U; while (n < buf.size()) buf[n++] = 0; @@ -137,7 +137,7 @@ std::array MD5::digest() const noexcept { // all zeroes, except for length std::fill(buf.begin(), buf.begin() + 56, 0); - } else if (n < 56) { + } else { // n < 56 // append padding to tmpbuf and then the length buf[n++] = 0x80U; while (n < 56) diff --git a/tests/md5_tests.cpp b/tests/md5_tests.cpp index 51608b24..2bd22772 100644 --- a/tests/md5_tests.cpp +++ b/tests/md5_tests.cpp @@ -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(2), "98327a3f4ee311c9bdc8613508922c95"); 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(1999), "82eeff8c7d574c8232b0ca6ca2c9dd40"); }