[llvm] r183054 - Add support for adding the contents of a StringRef to the MD5 hash.
Eric Christopher
echristo at gmail.com
Fri May 31 15:34:57 PDT 2013
Author: echristo
Date: Fri May 31 17:34:56 2013
New Revision: 183054
URL: http://llvm.org/viewvc/llvm-project?rev=183054&view=rev
Log:
Add support for adding the contents of a StringRef to the MD5 hash.
Modified:
llvm/trunk/include/llvm/Support/MD5.h
llvm/trunk/lib/Support/MD5.cpp
llvm/trunk/unittests/Support/MD5Test.cpp
Modified: llvm/trunk/include/llvm/Support/MD5.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/MD5.h?rev=183054&r1=183053&r2=183054&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/MD5.h (original)
+++ llvm/trunk/include/llvm/Support/MD5.h Fri May 31 17:34:56 2013
@@ -49,9 +49,12 @@ public:
MD5();
- /// \brief Updates the hash for arguments provided.
+ /// \brief Updates the hash for the byte stream provided.
void update(ArrayRef<uint8_t> Data);
+ /// \brief Updates the hash for the StringRef provided.
+ void update(StringRef Str);
+
/// \brief Finishes off the hash and puts the result in result.
void final(MD5Result &result);
Modified: llvm/trunk/lib/Support/MD5.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/MD5.cpp?rev=183054&r1=183053&r2=183054&view=diff
==============================================================================
--- llvm/trunk/lib/Support/MD5.cpp (original)
+++ llvm/trunk/lib/Support/MD5.cpp Fri May 31 17:34:56 2013
@@ -219,6 +219,14 @@ void MD5::update(ArrayRef<uint8_t> Data)
memcpy(buffer, Ptr, Size);
}
+/// Add the bytes in the StringRef \p Str to the hash.
+// Note that this isn't a string and so this won't include any trailing NULL
+// bytes.
+void MD5::update(StringRef Str) {
+ ArrayRef<uint8_t> SVal((const uint8_t *)Str.data(), Str.size());
+ update(SVal);
+}
+
/// \brief Finish the hash and place the resulting hash into \p result.
/// \param result is assumed to be a minimum of 16-bytes in size.
void MD5::final(MD5Result &result) {
Modified: llvm/trunk/unittests/Support/MD5Test.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/MD5Test.cpp?rev=183054&r1=183053&r2=183054&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/MD5Test.cpp (original)
+++ llvm/trunk/unittests/Support/MD5Test.cpp Fri May 31 17:34:56 2013
@@ -30,22 +30,31 @@ void TestMD5Sum(ArrayRef<uint8_t> Input,
EXPECT_EQ(Res, Final);
}
+void TestMD5Sum(StringRef Input, StringRef Final) {
+ MD5 Hash;
+ Hash.update(Input);
+ MD5::MD5Result MD5Res;
+ Hash.final(MD5Res);
+ SmallString<32> Res;
+ MD5::stringifyResult(MD5Res, Res);
+ EXPECT_EQ(Res, Final);
+}
+
TEST(MD5Test, MD5) {
TestMD5Sum(ArrayRef<uint8_t>((const uint8_t *)"", (size_t) 0),
"d41d8cd98f00b204e9800998ecf8427e");
TestMD5Sum(ArrayRef<uint8_t>((const uint8_t *)"a", (size_t) 1),
"0cc175b9c0f1b6a831c399e269772661");
- TestMD5Sum(ArrayRef<uint8_t>(
- (const uint8_t *)"abcdefghijklmnopqrstuvwxyz",
- (size_t) 26),
+ TestMD5Sum(ArrayRef<uint8_t>((const uint8_t *)"abcdefghijklmnopqrstuvwxyz",
+ (size_t) 26),
"c3fcd3d76192e4007dfb496cca67e13b");
TestMD5Sum(ArrayRef<uint8_t>((const uint8_t *)"\0", (size_t) 1),
"93b885adfe0da089cdf634904fd59f71");
TestMD5Sum(ArrayRef<uint8_t>((const uint8_t *)"a\0", (size_t) 2),
"4144e195f46de78a3623da7364d04f11");
- TestMD5Sum(ArrayRef<uint8_t>(
- (const uint8_t *)"abcdefghijklmnopqrstuvwxyz\0",
- (size_t) 27),
+ TestMD5Sum(ArrayRef<uint8_t>((const uint8_t *)"abcdefghijklmnopqrstuvwxyz\0",
+ (size_t) 27),
"81948d1f1554f58cd1a56ebb01f808cb");
+ TestMD5Sum("abcdefghijklmnopqrstuvwxyz", "c3fcd3d76192e4007dfb496cca67e13b");
}
}
More information about the llvm-commits
mailing list