From 6356463f9a642e9fe24d1fb0de7051637972b054 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Wed, 23 Nov 2016 00:46:09 +0000 Subject: [PATCH] Add convenient functions to compute hashes of byte vectors. In many sitautions, you just want to compute a hash for one chunk of data. This patch adds convenient functions for that purpose. Differential Revision: https://reviews.llvm.org/D26988 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@287726 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/MD5.h | 4 ++++ include/llvm/Support/SHA1.h | 4 ++++ lib/Support/MD5.cpp | 10 ++++++++++ lib/Support/SHA1.cpp | 10 ++++++++++ unittests/Support/MD5Test.cpp | 10 ++++++++++ unittests/Support/raw_sha1_ostream_test.cpp | 7 +++++++ 6 files changed, 45 insertions(+) diff --git a/include/llvm/Support/MD5.h b/include/llvm/Support/MD5.h index 42d8ca8a1eb..eb181bfe8a5 100644 --- a/include/llvm/Support/MD5.h +++ b/include/llvm/Support/MD5.h @@ -31,6 +31,7 @@ #include "llvm/ADT/SmallString.h" #include "llvm/Support/DataTypes.h" #include "llvm/Support/Endian.h" +#include namespace llvm { template class ArrayRef; @@ -62,6 +63,9 @@ public: /// deposited into \p Str. The result will be of length 32. static void stringifyResult(MD5Result &Result, SmallString<32> &Str); + /// \brief Computes the hash for a given bytes. + static std::array hash(ArrayRef Data); + private: const uint8_t *body(ArrayRef Data); }; diff --git a/include/llvm/Support/SHA1.h b/include/llvm/Support/SHA1.h index 1bd694999ce..1fc60a878f9 100644 --- a/include/llvm/Support/SHA1.h +++ b/include/llvm/Support/SHA1.h @@ -18,6 +18,7 @@ #include "llvm/ADT/ArrayRef.h" +#include #include namespace llvm { @@ -53,6 +54,9 @@ public: /// made into update. StringRef result(); + /// Returns a raw 160-bit SHA1 hash for the given data. + static std::array hash(ArrayRef Data); + private: /// Define some constants. /// "static constexpr" would be cleaner but MSVC does not support it yet. diff --git a/lib/Support/MD5.cpp b/lib/Support/MD5.cpp index ceab580984d..e589bfd297a 100644 --- a/lib/Support/MD5.cpp +++ b/lib/Support/MD5.cpp @@ -283,4 +283,14 @@ void MD5::stringifyResult(MD5Result &Result, SmallString<32> &Str) { Res << format("%.2x", Result[i]); } +std::array MD5::hash(ArrayRef Data) { + MD5 Hash; + Hash.update(Data); + MD5::MD5Result Res; + Hash.final(Res); + + std::array Arr; + memcpy(Arr.data(), Res, sizeof(Res)); + return Arr; +} } diff --git a/lib/Support/SHA1.cpp b/lib/Support/SHA1.cpp index 88c76395ead..016f580ae6d 100644 --- a/lib/Support/SHA1.cpp +++ b/lib/Support/SHA1.cpp @@ -269,3 +269,13 @@ StringRef SHA1::result() { // Return pointer to hash (20 characters) return Hash; } + +std::array SHA1::hash(ArrayRef Data) { + SHA1 Hash; + Hash.update(Data); + StringRef S = Hash.final().data(); + + std::array Arr; + memcpy(Arr.data(), S.data(), S.size()); + return Arr; +} diff --git a/unittests/Support/MD5Test.cpp b/unittests/Support/MD5Test.cpp index c4fa5cd92c1..4d790254503 100644 --- a/unittests/Support/MD5Test.cpp +++ b/unittests/Support/MD5Test.cpp @@ -57,4 +57,14 @@ TEST(MD5Test, MD5) { "81948d1f1554f58cd1a56ebb01f808cb"); TestMD5Sum("abcdefghijklmnopqrstuvwxyz", "c3fcd3d76192e4007dfb496cca67e13b"); } + +TEST(MD5HashTest, MD5) { + ArrayRef Input((const uint8_t *)"abcdefghijklmnopqrstuvwxyz", 26); + std::array Vec = MD5::hash(Input); + MD5::MD5Result MD5Res; + SmallString<32> Res; + memcpy(MD5Res, Vec.data(), Vec.size()); + MD5::stringifyResult(MD5Res, Res); + EXPECT_EQ(Res, "c3fcd3d76192e4007dfb496cca67e13b"); +} } diff --git a/unittests/Support/raw_sha1_ostream_test.cpp b/unittests/Support/raw_sha1_ostream_test.cpp index db2a3e9ab64..1bb4e2eb1d5 100644 --- a/unittests/Support/raw_sha1_ostream_test.cpp +++ b/unittests/Support/raw_sha1_ostream_test.cpp @@ -37,6 +37,13 @@ TEST(raw_sha1_ostreamTest, Basic) { ASSERT_EQ("2EF7BDE608CE5404E97D5F042F95F89F1C232871", Hash); } +TEST(sha1_hash_test, Basic) { + ArrayRef Input((const uint8_t *)"Hello World!", 12); + std::array Vec = SHA1::hash(Input); + std::string Hash = toHex({(const char *)Vec.data(), 20}); + ASSERT_EQ("2EF7BDE608CE5404E97D5F042F95F89F1C232871", Hash); +} + // Check that getting the intermediate hash in the middle of the stream does // not invalidate the final result. TEST(raw_sha1_ostreamTest, Intermediate) { -- 2.50.1