[llvm] r287726 - Add convenient functions to compute hashes of byte vectors.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 22 16:46:10 PST 2016


Author: ruiu
Date: Tue Nov 22 18:46:09 2016
New Revision: 287726

URL: http://llvm.org/viewvc/llvm-project?rev=287726&view=rev
Log:
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

Modified:
    llvm/trunk/include/llvm/Support/MD5.h
    llvm/trunk/include/llvm/Support/SHA1.h
    llvm/trunk/lib/Support/MD5.cpp
    llvm/trunk/lib/Support/SHA1.cpp
    llvm/trunk/unittests/Support/MD5Test.cpp
    llvm/trunk/unittests/Support/raw_sha1_ostream_test.cpp

Modified: llvm/trunk/include/llvm/Support/MD5.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/MD5.h?rev=287726&r1=287725&r2=287726&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/MD5.h (original)
+++ llvm/trunk/include/llvm/Support/MD5.h Tue Nov 22 18:46:09 2016
@@ -31,6 +31,7 @@
 #include "llvm/ADT/SmallString.h"
 #include "llvm/Support/DataTypes.h"
 #include "llvm/Support/Endian.h"
+#include <array>
 
 namespace llvm {
 template <typename T> 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<uint8_t, 16> hash(ArrayRef<uint8_t> Data);
+
 private:
   const uint8_t *body(ArrayRef<uint8_t> Data);
 };

Modified: llvm/trunk/include/llvm/Support/SHA1.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/SHA1.h?rev=287726&r1=287725&r2=287726&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/SHA1.h (original)
+++ llvm/trunk/include/llvm/Support/SHA1.h Tue Nov 22 18:46:09 2016
@@ -18,6 +18,7 @@
 
 #include "llvm/ADT/ArrayRef.h"
 
+#include <array>
 #include <cstdint>
 
 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<uint8_t, 20> hash(ArrayRef<uint8_t> Data);
+
 private:
   /// Define some constants.
   /// "static constexpr" would be cleaner but MSVC does not support it yet.

Modified: llvm/trunk/lib/Support/MD5.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/MD5.cpp?rev=287726&r1=287725&r2=287726&view=diff
==============================================================================
--- llvm/trunk/lib/Support/MD5.cpp (original)
+++ llvm/trunk/lib/Support/MD5.cpp Tue Nov 22 18:46:09 2016
@@ -283,4 +283,14 @@ void MD5::stringifyResult(MD5Result &Res
     Res << format("%.2x", Result[i]);
 }
 
+std::array<uint8_t, 16> MD5::hash(ArrayRef<uint8_t> Data) {
+  MD5 Hash;
+  Hash.update(Data);
+  MD5::MD5Result Res;
+  Hash.final(Res);
+
+  std::array<uint8_t, 16> Arr;
+  memcpy(Arr.data(), Res, sizeof(Res));
+  return Arr;
+}
 }

Modified: llvm/trunk/lib/Support/SHA1.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/SHA1.cpp?rev=287726&r1=287725&r2=287726&view=diff
==============================================================================
--- llvm/trunk/lib/Support/SHA1.cpp (original)
+++ llvm/trunk/lib/Support/SHA1.cpp Tue Nov 22 18:46:09 2016
@@ -269,3 +269,13 @@ StringRef SHA1::result() {
   // Return pointer to hash (20 characters)
   return Hash;
 }
+
+std::array<uint8_t, 20> SHA1::hash(ArrayRef<uint8_t> Data) {
+  SHA1 Hash;
+  Hash.update(Data);
+  StringRef S = Hash.final().data();
+
+  std::array<uint8_t, 20> Arr;
+  memcpy(Arr.data(), S.data(), S.size());
+  return Arr;
+}

Modified: llvm/trunk/unittests/Support/MD5Test.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/MD5Test.cpp?rev=287726&r1=287725&r2=287726&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/MD5Test.cpp (original)
+++ llvm/trunk/unittests/Support/MD5Test.cpp Tue Nov 22 18:46:09 2016
@@ -57,4 +57,14 @@ TEST(MD5Test, MD5) {
              "81948d1f1554f58cd1a56ebb01f808cb");
   TestMD5Sum("abcdefghijklmnopqrstuvwxyz", "c3fcd3d76192e4007dfb496cca67e13b");
 }
+
+TEST(MD5HashTest, MD5) {
+  ArrayRef<uint8_t> Input((const uint8_t *)"abcdefghijklmnopqrstuvwxyz", 26);
+  std::array<uint8_t, 16> Vec = MD5::hash(Input);
+  MD5::MD5Result MD5Res;
+  SmallString<32> Res;
+  memcpy(MD5Res, Vec.data(), Vec.size());
+  MD5::stringifyResult(MD5Res, Res);
+  EXPECT_EQ(Res, "c3fcd3d76192e4007dfb496cca67e13b");
+}
 }

Modified: llvm/trunk/unittests/Support/raw_sha1_ostream_test.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/raw_sha1_ostream_test.cpp?rev=287726&r1=287725&r2=287726&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/raw_sha1_ostream_test.cpp (original)
+++ llvm/trunk/unittests/Support/raw_sha1_ostream_test.cpp Tue Nov 22 18:46:09 2016
@@ -37,6 +37,13 @@ TEST(raw_sha1_ostreamTest, Basic) {
   ASSERT_EQ("2EF7BDE608CE5404E97D5F042F95F89F1C232871", Hash);
 }
 
+TEST(sha1_hash_test, Basic) {
+  ArrayRef<uint8_t> Input((const uint8_t *)"Hello World!", 12);
+  std::array<uint8_t, 20> 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) {




More information about the llvm-commits mailing list