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) {
return true;
}
void ComputedHashes::ComputeHashesForContent(const std::string& contents,
size_t block_size,
std::vector<std::string>* hashes) {
std::vector<std::string> ComputedHashes::GetHashesForContent(
const std::string& contents,
size_t block_size) {
size_t offset = 0;
std::vector<std::string> hashes;
// Even when the contents is empty, we want to output at least one hash
// block (the hash of the empty string).
do {
......@@ -223,10 +224,10 @@ void ComputedHashes::ComputeHashesForContent(const std::string& contents,
crypto::SecureHash::Create(crypto::SecureHash::SHA256));
hash->Update(block_start, bytes_to_read);
hashes->push_back(std::string());
std::string* buffer = &(hashes->back());
buffer->resize(crypto::kSHA256Length);
hash->Finish(base::data(*buffer), buffer->size());
std::string buffer;
buffer.resize(crypto::kSHA256Length);
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 (bytes_to_read == 0)
......@@ -234,6 +235,8 @@ void ComputedHashes::ComputeHashesForContent(const std::string& contents,
offset += bytes_to_read;
} while (offset < contents.size());
return hashes;
}
} // namespace extensions
......@@ -62,11 +62,10 @@ class ComputedHashes {
std::unique_ptr<base::ListValue> file_list_;
};
// Computes the SHA256 hash of each |block_size| chunk in |contents|, placing
// the results into |hashes|.
static void ComputeHashesForContent(const std::string& contents,
size_t block_size,
std::vector<std::string>* hashes);
// Returns the SHA256 hash of each |block_size| chunk in |contents|.
static std::vector<std::string> GetHashesForContent(
const std::string& contents,
size_t block_size);
};
} // namespace extensions
......
......@@ -120,15 +120,15 @@ TEST(ComputedHashesTest, ComputedHashes) {
// $ dd if=hello.txt bs=4096 count=1 | openssl dgst -sha256 -binary | base64
// $ dd if=hello.txt skip=1 bs=4096 count=1 |
// openssl dgst -sha256 -binary | base64
TEST(ComputedHashesTest, ComputeHashesForContent) {
TEST(ComputedHashesTest, GetHashesForContent) {
const int block_size = 4096;
// Simple short input.
std::string content1 = "hello world";
std::string content1_expected_hash =
"uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek=";
std::vector<std::string> hashes1;
ComputedHashes::ComputeHashesForContent(content1, block_size, &hashes1);
std::vector<std::string> hashes1 =
ComputedHashes::GetHashesForContent(content1, block_size);
ASSERT_EQ(1u, hashes1.size());
EXPECT_EQ(content1_expected_hash, Base64Encode(hashes1[0]));
......@@ -139,16 +139,16 @@ TEST(ComputedHashesTest, ComputeHashesForContent) {
const char* content2_expected_hashes[] = {
"bvtt5hXo8xvHrlzGAhhoqPL/r+4zJXHx+6wAvkv15V8=",
"lTD45F7P6I/HOdi8u7FLRA4qzAYL+7xSNVeusG6MJI0="};
std::vector<std::string> hashes2;
ComputedHashes::ComputeHashesForContent(content2, block_size, &hashes2);
std::vector<std::string> hashes2 =
ComputedHashes::GetHashesForContent(content2, block_size);
ASSERT_EQ(2u, hashes2.size());
EXPECT_EQ(content2_expected_hashes[0], Base64Encode(hashes2[0]));
EXPECT_EQ(content2_expected_hashes[1], Base64Encode(hashes2[1]));
// Now an empty input.
std::string content3;
std::vector<std::string> hashes3;
ComputedHashes::ComputeHashesForContent(content3, block_size, &hashes3);
std::vector<std::string> hashes3 =
ComputedHashes::GetHashesForContent(content3, block_size);
ASSERT_EQ(1u, hashes3.size());
ASSERT_EQ(std::string("47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU="),
Base64Encode(hashes3[0]));
......
......@@ -145,8 +145,8 @@ const ComputedHashes::Reader& ContentHash::computed_hashes() const {
// static
std::string ContentHash::ComputeTreeHashForContent(const std::string& contents,
int block_size) {
std::vector<std::string> hashes;
ComputedHashes::ComputeHashesForContent(contents, block_size, &hashes);
std::vector<std::string> hashes =
ComputedHashes::GetHashesForContent(contents, block_size);
return ComputeTreeHashRoot(hashes, block_size / crypto::kSHA256Length);
}
......@@ -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
// the file.
std::vector<std::string> hashes;
ComputedHashes::ComputeHashesForContent(contents, block_size_, &hashes);
std::vector<std::string> hashes =
ComputedHashes::GetHashesForContent(contents, block_size_);
std::string root =
ComputeTreeHashRoot(hashes, block_size_ / crypto::kSHA256Length);
if (!verified_contents_->TreeHashRootEquals(relative_unix_path, root)) {
......
......@@ -57,9 +57,8 @@ class TestExtensionBuilder {
ComputedHashes::Writer computed_hashes_writer;
for (const auto& resource : extension_resources_) {
std::vector<std::string> hashes;
ComputedHashes::ComputeHashesForContent(resource.contents, block_size,
&hashes);
std::vector<std::string> hashes =
ComputedHashes::GetHashesForContent(resource.contents, block_size);
computed_hashes_writer.AddHashes(resource.relative_path, block_size,
hashes);
}
......
......@@ -287,9 +287,9 @@ void WriteIncorrectComputedHashes(const base::FilePath& extension_path,
ComputedHashes::Writer incorrect_computed_hashes_writer;
// Write a valid computed_hashes.json with incorrect hash for |resource_path|.
std::vector<std::string> hashes;
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);
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