[PATCH] D107781: [Support] Introduce `SmallString<32> MD5::final()`...

Alexandre Rames via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 9 13:32:40 PDT 2021


arames created this revision.
arames added reviewers: dexonsmith, Bigcheese, jansvoboda11, t.p.northover.
Herald added a subscriber: hiraditya.
arames requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

and `std::pair<uint64_t, uint64_t> finalWords()`.
The helpers allow more simply retrieving the result hash. It aligns `MD5` with
other hashes such as `SHA1` or `SHA256`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D107781

Files:
  llvm/include/llvm/Support/MD5.h
  llvm/lib/Support/MD5.cpp
  llvm/unittests/Support/MD5Test.cpp


Index: llvm/unittests/Support/MD5Test.cpp
===================================================================
--- llvm/unittests/Support/MD5Test.cpp
+++ llvm/unittests/Support/MD5Test.cpp
@@ -68,4 +68,22 @@
   EXPECT_EQ(0x3be167ca6c49fb7dULL, MD5Res.high());
   EXPECT_EQ(0x00e49261d7d3fcc3ULL, MD5Res.low());
 }
+
+TEST(MD5Test, FinalHelpers) {
+  {
+    MD5 Hash;
+    Hash.update("abcd");
+    SmallString<32> Result = Hash.final();
+    const char *Expected = "e2fc714c4727ee9395f324cd2e7f331f";
+    EXPECT_EQ(Result, Expected);
+  }
+  {
+    MD5 Hash;
+    Hash.update("abcd");
+    std::pair<uint64_t, uint64_t> Result = Hash.finalWords();
+    auto Expected = std::make_pair<uint64_t, uint64_t>(2248280477974983573ULL,
+                                                       10659500555211242722ULL);
+    EXPECT_EQ(Result, Expected);
+  }
 }
+} // namespace
Index: llvm/lib/Support/MD5.cpp
===================================================================
--- llvm/lib/Support/MD5.cpp
+++ llvm/lib/Support/MD5.cpp
@@ -262,6 +262,20 @@
   support::endian::write32le(&Result[12], d);
 }
 
+/// Finish and return the hash as a pair of words.
+std::pair<uint64_t, uint64_t> MD5::finalWords() {
+  MD5Result Result;
+  final(Result);
+  return Result.words();
+}
+
+/// Finish and return the hash as a hex string of length 32.
+SmallString<32> MD5::final() {
+  MD5Result Result;
+  final(Result);
+  return Result.digest();
+}
+
 SmallString<32> MD5::MD5Result::digest() const {
   SmallString<32> Str;
   raw_svector_ostream Res(Str);
Index: llvm/include/llvm/Support/MD5.h
===================================================================
--- llvm/include/llvm/Support/MD5.h
+++ llvm/include/llvm/Support/MD5.h
@@ -90,6 +90,12 @@
   /// Finishes off the hash and puts the result in result.
   void final(MD5Result &Result);
 
+  /// Finishes the hash, and returns the result as a pair of words.
+  std::pair<uint64_t, uint64_t> finalWords();
+
+  /// Finishes the hash, and returns the result as a hex string of length 32.
+  SmallString<32> final();
+
   /// 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);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D107781.365280.patch
Type: text/x-patch
Size: 2263 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210809/30b8f3c5/attachment.bin>


More information about the llvm-commits mailing list