summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
Diffstat (limited to 'base')
-rw-r--r--base/sha2.cc2
-rw-r--r--base/sha2_unittest.cc19
2 files changed, 20 insertions, 1 deletions
diff --git a/base/sha2.cc b/base/sha2.cc
index 47d381b..c0fd0ab 100644
--- a/base/sha2.cc
+++ b/base/sha2.cc
@@ -22,7 +22,7 @@ void SHA256HashString(const std::string& str, void* output, size_t len) {
std::string SHA256HashString(const std::string& str) {
std::string output(SHA256_LENGTH, 0);
- SHA256HashString(str, string_as_array(&output), str.size());
+ SHA256HashString(str, string_as_array(&output), output.size());
return output;
}
diff --git a/base/sha2_unittest.cc b/base/sha2_unittest.cc
index a6844dd..b0321e8 100644
--- a/base/sha2_unittest.cc
+++ b/base/sha2_unittest.cc
@@ -30,6 +30,25 @@ TEST(Sha256Test, Test1) {
EXPECT_EQ(expected1[i], static_cast<int>(output_truncated1[i]));
}
+TEST(Sha256Test, Test1_String) {
+ // Same as the above, but using the wrapper that returns a std::string.
+ // Example B.1 from FIPS 180-2: one-block message.
+ std::string input1 = "abc";
+ int expected1[] = { 0xba, 0x78, 0x16, 0xbf,
+ 0x8f, 0x01, 0xcf, 0xea,
+ 0x41, 0x41, 0x40, 0xde,
+ 0x5d, 0xae, 0x22, 0x23,
+ 0xb0, 0x03, 0x61, 0xa3,
+ 0x96, 0x17, 0x7a, 0x9c,
+ 0xb4, 0x10, 0xff, 0x61,
+ 0xf2, 0x00, 0x15, 0xad };
+
+ std::string output1 = base::SHA256HashString(input1);
+ ASSERT_EQ(base::SHA256_LENGTH, output1.size());
+ for (size_t i = 0; i < base::SHA256_LENGTH; i++)
+ EXPECT_EQ(expected1[i], static_cast<uint8>(output1[i]));
+}
+
TEST(Sha256Test, Test2) {
// Example B.2 from FIPS 180-2: multi-block message.
std::string input2 =