[PATCH] D107781: [Support] Update `MD5` to follow other hashes.
Alexandre Rames via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 19 14:13:21 PDT 2021
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGcd28003336c7: [Support] Update `MD5` to follow other hashes. (authored by arames).
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D107781/new/
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,35 @@
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
Index: llvm/lib/Support/MD5.cpp
===================================================================
--- llvm/lib/Support/MD5.cpp
+++ llvm/lib/Support/MD5.cpp
@@ -262,6 +262,23 @@
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);
Index: llvm/include/llvm/Support/MD5.h
===================================================================
--- llvm/include/llvm/Support/MD5.h
+++ llvm/include/llvm/Support/MD5.h
@@ -78,6 +78,14 @@
/// 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);
@@ -101,6 +109,8 @@
MD5_u32plus block[16];
} InternalState;
+ MD5Result Result;
+
const uint8_t *body(ArrayRef<uint8_t> Data);
};
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D107781.367617.patch
Type: text/x-patch
Size: 2866 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210819/ec9cb547/attachment.bin>
More information about the llvm-commits
mailing list