Skip to content

Commit

Permalink
[Support] Update MD5 to follow other hashes.
Browse files Browse the repository at this point in the history
Introduce `StringRef final()` and `StringRef result()`.

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D107781
  • Loading branch information
Alexandre Rames committed Aug 19, 2021
1 parent 44a3241 commit cd28003
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
10 changes: 10 additions & 0 deletions llvm/include/llvm/Support/MD5.h
Expand Up @@ -78,6 +78,14 @@ class MD5 {
/// Finishes off the hash and puts the result in result.
void final(MD5Result &Result);

/// Finishes off the hash, and returns a reference to the 16-byte hash data.
StringRef final();

/// Finishes off the hash, and returns a reference to the 16-byte hash data.
/// This is suitable for getting the MD5 at any time without invalidating the
/// internal state, so that more calls can be made into `update`.
StringRef result();

/// Translates the bytes in \p Res to a hex string that is
/// deposited into \p Str. The result will be of length 32.
static void stringifyResult(MD5Result &Result, SmallString<32> &Str);
Expand All @@ -101,6 +109,8 @@ class MD5 {
MD5_u32plus block[16];
} InternalState;

MD5Result Result;

const uint8_t *body(ArrayRef<uint8_t> Data);
};

Expand Down
17 changes: 17 additions & 0 deletions llvm/lib/Support/MD5.cpp
Expand Up @@ -262,6 +262,23 @@ void MD5::final(MD5Result &Result) {
support::endian::write32le(&Result[12], InternalState.d);
}

StringRef MD5::final() {
final(Result);
return StringRef(reinterpret_cast<char *>(Result.Bytes.data()),
Result.Bytes.size());
}

StringRef MD5::result() {
auto StateToRestore = InternalState;

auto Hash = final();

// Restore the state
InternalState = StateToRestore;

return Hash;
}

SmallString<32> MD5::MD5Result::digest() const {
SmallString<32> Str;
raw_svector_ostream Res(Str);
Expand Down
31 changes: 31 additions & 0 deletions llvm/unittests/Support/MD5Test.cpp
Expand Up @@ -68,4 +68,35 @@ TEST(MD5HashTest, MD5) {
EXPECT_EQ(0x3be167ca6c49fb7dULL, MD5Res.high());
EXPECT_EQ(0x00e49261d7d3fcc3ULL, MD5Res.low());
}

TEST(MD5Test, FinalAndResultHelpers) {
MD5 Hash;

Hash.update("abcd");

{
MD5 ReferenceHash;
ReferenceHash.update("abcd");
MD5::MD5Result ReferenceResult;
ReferenceHash.final(ReferenceResult);
StringRef ExpectedResult =
StringRef(reinterpret_cast<char *>(ReferenceResult.Bytes.data()),
ReferenceResult.Bytes.size());
EXPECT_EQ(Hash.result(), ExpectedResult);
}

Hash.update("xyz");

{
MD5 ReferenceHash;
ReferenceHash.update("abcd");
ReferenceHash.update("xyz");
MD5::MD5Result ReferenceResult;
ReferenceHash.final(ReferenceResult);
StringRef ExpectedResult =
StringRef(reinterpret_cast<char *>(ReferenceResult.Bytes.data()),
ReferenceResult.Bytes.size());
EXPECT_EQ(Hash.final(), ExpectedResult);
}
}
} // namespace

0 comments on commit cd28003

Please sign in to comment.