diff options
author | bryner@chromium.org <bryner@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-24 17:45:21 +0000 |
---|---|---|
committer | bryner@chromium.org <bryner@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-24 17:45:21 +0000 |
commit | 6c683f09fd3c1e0b7565b6c93d639b9840e7c1f8 (patch) | |
tree | 8966794d268cd876c5497e06c3b7a27409338a52 | |
parent | 4c9a3f412bd09d6378106b37662320d79a6b82ca (diff) | |
download | chromium_src-6c683f09fd3c1e0b7565b6c93d639b9840e7c1f8.zip chromium_src-6c683f09fd3c1e0b7565b6c93d639b9840e7c1f8.tar.gz chromium_src-6c683f09fd3c1e0b7565b6c93d639b9840e7c1f8.tar.bz2 |
Fix a bug in the string wrapper for SHA256HashString.
The wrapper was using the size of the input string as the number of hash bytes
to compute, rather than the size of the output string (SHA256_LENGTH). Added a
test for this.
BUG=none
TEST=Sha256Test.Test1_String
Review URL: http://codereview.chromium.org/3492008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@60491 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | base/sha2.cc | 2 | ||||
-rw-r--r-- | base/sha2_unittest.cc | 19 |
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 = |