Skip to content

Commit

Permalink
crypto: Add SecureHashTest.Equality test
Browse files Browse the repository at this point in the history
Make sure Update(a) + Update(b) == Update(a + b), which is not obvious
for readers not familiar with crypto. Also updates the comment to
emphasize this.

Change-Id: Iae8adeb4743c247053d60c7040e7d79807ce9475
Reviewed-on: https://chromium-review.googlesource.com/729701
Reviewed-by: David Benjamin <davidben@chromium.org>
Commit-Queue: Xiaohan Wang <xhwang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#510979}
  • Loading branch information
xhwang-chromium authored and Commit Bot committed Oct 24, 2017
1 parent 53bdeca commit bb6c0b8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
3 changes: 2 additions & 1 deletion crypto/secure_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
namespace crypto {

// A wrapper to calculate secure hashes incrementally, allowing to
// be used when the full input is not known in advance.
// be used when the full input is not known in advance. The end result will the
// same as if we have the full input in advance.
class CRYPTO_EXPORT SecureHash {
public:
enum Algorithm {
Expand Down
25 changes: 25 additions & 0 deletions crypto/secure_hash_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,28 @@ TEST(SecureHashTest, TestLength) {
crypto::SecureHash::Create(crypto::SecureHash::SHA256));
EXPECT_EQ(crypto::kSHA256Length, ctx->GetHashLength());
}

TEST(SecureHashTest, Equality) {
std::string input1(10001, 'a'); // 'a' repeated 10001 times
std::string input2(10001, 'd'); // 'd' repeated 10001 times

uint8_t output1[crypto::kSHA256Length];
uint8_t output2[crypto::kSHA256Length];

// Call Update() twice on input1 and input2.
std::unique_ptr<crypto::SecureHash> ctx1(
crypto::SecureHash::Create(crypto::SecureHash::SHA256));
ctx1->Update(input1.data(), input1.size());
ctx1->Update(input2.data(), input2.size());
ctx1->Finish(output1, sizeof(output1));

// Call Update() once one input1 + input2 (concatenation).
std::unique_ptr<crypto::SecureHash> ctx2(
crypto::SecureHash::Create(crypto::SecureHash::SHA256));
std::string input3 = input1 + input2;
ctx2->Update(input3.data(), input3.size());
ctx2->Finish(output2, sizeof(output2));

// The hash should be the same.
EXPECT_EQ(0, memcmp(output1, output2, crypto::kSHA256Length));
}

0 comments on commit bb6c0b8

Please sign in to comment.