Commit 59637a67 authored by Istiaque Ahmed's avatar Istiaque Ahmed Committed by Commit Bot

[Extensions] Make ComputeHashesForContent return block hashes directly.

This avoids passing vector* to the function, slightly improves
callsite readability.

Bug: 796395
Change-Id: Ic7e6139d3ea2c9044fc6d7b6b668998c8a370ab0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1869675
Commit-Queue: Istiaque Ahmed <lazyboy@chromium.org>
Reviewed-by: default avatarOleg Davydov <burunduk@chromium.org>
Cr-Commit-Position: refs/heads/master@{#710832}
parent 52d48b3f
...@@ -209,10 +209,11 @@ bool ComputedHashes::Writer::WriteToFile(const base::FilePath& path) { ...@@ -209,10 +209,11 @@ bool ComputedHashes::Writer::WriteToFile(const base::FilePath& path) {
return true; return true;
} }
void ComputedHashes::ComputeHashesForContent(const std::string& contents, std::vector<std::string> ComputedHashes::GetHashesForContent(
size_t block_size, const std::string& contents,
std::vector<std::string>* hashes) { size_t block_size) {
size_t offset = 0; size_t offset = 0;
std::vector<std::string> hashes;
// Even when the contents is empty, we want to output at least one hash // Even when the contents is empty, we want to output at least one hash
// block (the hash of the empty string). // block (the hash of the empty string).
do { do {
...@@ -223,10 +224,10 @@ void ComputedHashes::ComputeHashesForContent(const std::string& contents, ...@@ -223,10 +224,10 @@ void ComputedHashes::ComputeHashesForContent(const std::string& contents,
crypto::SecureHash::Create(crypto::SecureHash::SHA256)); crypto::SecureHash::Create(crypto::SecureHash::SHA256));
hash->Update(block_start, bytes_to_read); hash->Update(block_start, bytes_to_read);
hashes->push_back(std::string()); std::string buffer;
std::string* buffer = &(hashes->back()); buffer.resize(crypto::kSHA256Length);
buffer->resize(crypto::kSHA256Length); hash->Finish(base::data(buffer), buffer.size());
hash->Finish(base::data(*buffer), buffer->size()); hashes.push_back(std::move(buffer));
// If |contents| is empty, then we want to just exit here. // If |contents| is empty, then we want to just exit here.
if (bytes_to_read == 0) if (bytes_to_read == 0)
...@@ -234,6 +235,8 @@ void ComputedHashes::ComputeHashesForContent(const std::string& contents, ...@@ -234,6 +235,8 @@ void ComputedHashes::ComputeHashesForContent(const std::string& contents,
offset += bytes_to_read; offset += bytes_to_read;
} while (offset < contents.size()); } while (offset < contents.size());
return hashes;
} }
} // namespace extensions } // namespace extensions
...@@ -62,11 +62,10 @@ class ComputedHashes { ...@@ -62,11 +62,10 @@ class ComputedHashes {
std::unique_ptr<base::ListValue> file_list_; std::unique_ptr<base::ListValue> file_list_;
}; };
// Computes the SHA256 hash of each |block_size| chunk in |contents|, placing // Returns the SHA256 hash of each |block_size| chunk in |contents|.
// the results into |hashes|. static std::vector<std::string> GetHashesForContent(
static void ComputeHashesForContent(const std::string& contents, const std::string& contents,
size_t block_size, size_t block_size);
std::vector<std::string>* hashes);
}; };
} // namespace extensions } // namespace extensions
......
...@@ -120,15 +120,15 @@ TEST(ComputedHashesTest, ComputedHashes) { ...@@ -120,15 +120,15 @@ TEST(ComputedHashesTest, ComputedHashes) {
// $ dd if=hello.txt bs=4096 count=1 | openssl dgst -sha256 -binary | base64 // $ dd if=hello.txt bs=4096 count=1 | openssl dgst -sha256 -binary | base64
// $ dd if=hello.txt skip=1 bs=4096 count=1 | // $ dd if=hello.txt skip=1 bs=4096 count=1 |
// openssl dgst -sha256 -binary | base64 // openssl dgst -sha256 -binary | base64
TEST(ComputedHashesTest, ComputeHashesForContent) { TEST(ComputedHashesTest, GetHashesForContent) {
const int block_size = 4096; const int block_size = 4096;
// Simple short input. // Simple short input.
std::string content1 = "hello world"; std::string content1 = "hello world";
std::string content1_expected_hash = std::string content1_expected_hash =
"uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek="; "uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek=";
std::vector<std::string> hashes1; std::vector<std::string> hashes1 =
ComputedHashes::ComputeHashesForContent(content1, block_size, &hashes1); ComputedHashes::GetHashesForContent(content1, block_size);
ASSERT_EQ(1u, hashes1.size()); ASSERT_EQ(1u, hashes1.size());
EXPECT_EQ(content1_expected_hash, Base64Encode(hashes1[0])); EXPECT_EQ(content1_expected_hash, Base64Encode(hashes1[0]));
...@@ -139,16 +139,16 @@ TEST(ComputedHashesTest, ComputeHashesForContent) { ...@@ -139,16 +139,16 @@ TEST(ComputedHashesTest, ComputeHashesForContent) {
const char* content2_expected_hashes[] = { const char* content2_expected_hashes[] = {
"bvtt5hXo8xvHrlzGAhhoqPL/r+4zJXHx+6wAvkv15V8=", "bvtt5hXo8xvHrlzGAhhoqPL/r+4zJXHx+6wAvkv15V8=",
"lTD45F7P6I/HOdi8u7FLRA4qzAYL+7xSNVeusG6MJI0="}; "lTD45F7P6I/HOdi8u7FLRA4qzAYL+7xSNVeusG6MJI0="};
std::vector<std::string> hashes2; std::vector<std::string> hashes2 =
ComputedHashes::ComputeHashesForContent(content2, block_size, &hashes2); ComputedHashes::GetHashesForContent(content2, block_size);
ASSERT_EQ(2u, hashes2.size()); ASSERT_EQ(2u, hashes2.size());
EXPECT_EQ(content2_expected_hashes[0], Base64Encode(hashes2[0])); EXPECT_EQ(content2_expected_hashes[0], Base64Encode(hashes2[0]));
EXPECT_EQ(content2_expected_hashes[1], Base64Encode(hashes2[1])); EXPECT_EQ(content2_expected_hashes[1], Base64Encode(hashes2[1]));
// Now an empty input. // Now an empty input.
std::string content3; std::string content3;
std::vector<std::string> hashes3; std::vector<std::string> hashes3 =
ComputedHashes::ComputeHashesForContent(content3, block_size, &hashes3); ComputedHashes::GetHashesForContent(content3, block_size);
ASSERT_EQ(1u, hashes3.size()); ASSERT_EQ(1u, hashes3.size());
ASSERT_EQ(std::string("47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU="), ASSERT_EQ(std::string("47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU="),
Base64Encode(hashes3[0])); Base64Encode(hashes3[0]));
......
...@@ -145,8 +145,8 @@ const ComputedHashes::Reader& ContentHash::computed_hashes() const { ...@@ -145,8 +145,8 @@ const ComputedHashes::Reader& ContentHash::computed_hashes() const {
// static // static
std::string ContentHash::ComputeTreeHashForContent(const std::string& contents, std::string ContentHash::ComputeTreeHashForContent(const std::string& contents,
int block_size) { int block_size) {
std::vector<std::string> hashes; std::vector<std::string> hashes =
ComputedHashes::ComputeHashesForContent(contents, block_size, &hashes); ComputedHashes::GetHashesForContent(contents, block_size);
return ComputeTreeHashRoot(hashes, block_size / crypto::kSHA256Length); return ComputeTreeHashRoot(hashes, block_size / crypto::kSHA256Length);
} }
...@@ -301,8 +301,8 @@ bool ContentHash::CreateHashes(const base::FilePath& hashes_file, ...@@ -301,8 +301,8 @@ bool ContentHash::CreateHashes(const base::FilePath& hashes_file,
// Iterate through taking the hash of each block of size (block_size_) of // Iterate through taking the hash of each block of size (block_size_) of
// the file. // the file.
std::vector<std::string> hashes; std::vector<std::string> hashes =
ComputedHashes::ComputeHashesForContent(contents, block_size_, &hashes); ComputedHashes::GetHashesForContent(contents, block_size_);
std::string root = std::string root =
ComputeTreeHashRoot(hashes, block_size_ / crypto::kSHA256Length); ComputeTreeHashRoot(hashes, block_size_ / crypto::kSHA256Length);
if (!verified_contents_->TreeHashRootEquals(relative_unix_path, root)) { if (!verified_contents_->TreeHashRootEquals(relative_unix_path, root)) {
......
...@@ -57,9 +57,8 @@ class TestExtensionBuilder { ...@@ -57,9 +57,8 @@ class TestExtensionBuilder {
ComputedHashes::Writer computed_hashes_writer; ComputedHashes::Writer computed_hashes_writer;
for (const auto& resource : extension_resources_) { for (const auto& resource : extension_resources_) {
std::vector<std::string> hashes; std::vector<std::string> hashes =
ComputedHashes::ComputeHashesForContent(resource.contents, block_size, ComputedHashes::GetHashesForContent(resource.contents, block_size);
&hashes);
computed_hashes_writer.AddHashes(resource.relative_path, block_size, computed_hashes_writer.AddHashes(resource.relative_path, block_size,
hashes); hashes);
} }
......
...@@ -287,9 +287,9 @@ void WriteIncorrectComputedHashes(const base::FilePath& extension_path, ...@@ -287,9 +287,9 @@ void WriteIncorrectComputedHashes(const base::FilePath& extension_path,
ComputedHashes::Writer incorrect_computed_hashes_writer; ComputedHashes::Writer incorrect_computed_hashes_writer;
// Write a valid computed_hashes.json with incorrect hash for |resource_path|. // Write a valid computed_hashes.json with incorrect hash for |resource_path|.
std::vector<std::string> hashes;
const std::string kFakeContents = "fake contents"; const std::string kFakeContents = "fake contents";
ComputedHashes::ComputeHashesForContent(kFakeContents, block_size, &hashes); std::vector<std::string> hashes =
ComputedHashes::GetHashesForContent(kFakeContents, block_size);
incorrect_computed_hashes_writer.AddHashes(resource_path, block_size, hashes); incorrect_computed_hashes_writer.AddHashes(resource_path, block_size, hashes);
ASSERT_TRUE(incorrect_computed_hashes_writer.WriteToFile( ASSERT_TRUE(incorrect_computed_hashes_writer.WriteToFile(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment