[PATCH] D26988: Add convenient functions to compute hashes of byte vectors.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 22 14:25:17 PST 2016


ruiu updated this revision to Diff 78949.
ruiu added a comment.

- Changed the type from std::vector<uint8_t> to SmallVector<uint8_t, 16 or 20>.
- Added tests.


https://reviews.llvm.org/D26988

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


Index: unittests/Support/raw_sha1_ostream_test.cpp
===================================================================
--- unittests/Support/raw_sha1_ostream_test.cpp
+++ unittests/Support/raw_sha1_ostream_test.cpp
@@ -37,6 +37,13 @@
   ASSERT_EQ("2EF7BDE608CE5404E97D5F042F95F89F1C232871", Hash);
 }
 
+TEST(sha1_hash_test, Basic) {
+  ArrayRef<uint8_t> Input((const uint8_t *)"Hello World!", 12);
+  SmallVector<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) {
Index: unittests/Support/MD5Test.cpp
===================================================================
--- unittests/Support/MD5Test.cpp
+++ unittests/Support/MD5Test.cpp
@@ -57,4 +57,14 @@
              "81948d1f1554f58cd1a56ebb01f808cb");
   TestMD5Sum("abcdefghijklmnopqrstuvwxyz", "c3fcd3d76192e4007dfb496cca67e13b");
 }
+
+TEST(MD5HashTest, MD5) {
+  ArrayRef<uint8_t> Input((const uint8_t *)"abcdefghijklmnopqrstuvwxyz", 26);
+  SmallVector<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");
+}
 }
Index: lib/Support/SHA1.cpp
===================================================================
--- lib/Support/SHA1.cpp
+++ lib/Support/SHA1.cpp
@@ -269,3 +269,11 @@
   // Return pointer to hash (20 characters)
   return Hash;
 }
+
+SmallVector<uint8_t, 20> SHA1::hash(ArrayRef<uint8_t> Data) {
+  SHA1 Hash;
+  Hash.update(Data);
+  StringRef S = Hash.final().data();
+  auto *P = reinterpret_cast<const uint8_t *>(S.data());
+  return {P, P + S.size()};
+}
Index: lib/Support/MD5.cpp
===================================================================
--- lib/Support/MD5.cpp
+++ lib/Support/MD5.cpp
@@ -283,4 +283,12 @@
     Res << format("%.2x", Result[i]);
 }
 
+SmallVector<uint8_t, 16> MD5::hash(ArrayRef<uint8_t> Data) {
+  MD5 Hash;
+  Hash.update(Data);
+  MD5::MD5Result Res;
+  Hash.final(Res);
+  return {Res, Res + sizeof(Res)};
+}
+
 }
Index: include/llvm/Support/SHA1.h
===================================================================
--- include/llvm/Support/SHA1.h
+++ include/llvm/Support/SHA1.h
@@ -53,6 +53,9 @@
   /// made into update.
   StringRef result();
 
+  /// Returns a raw 160-bit SHA1 hash for the given data.
+  static SmallVector<uint8_t, 20> hash(ArrayRef<uint8_t> Data);
+
 private:
   /// Define some constants.
   /// "static constexpr" would be cleaner but MSVC does not support it yet.
Index: include/llvm/Support/MD5.h
===================================================================
--- include/llvm/Support/MD5.h
+++ include/llvm/Support/MD5.h
@@ -62,6 +62,9 @@
   /// 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 SmallVector<uint8_t, 16> hash(ArrayRef<uint8_t> Data);
+
 private:
   const uint8_t *body(ArrayRef<uint8_t> Data);
 };


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D26988.78949.patch
Type: text/x-patch
Size: 3249 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161122/314b850a/attachment.bin>


More information about the llvm-commits mailing list