Skip to content

Commit

Permalink
Introduce base::SHA1Digest type alias
Browse files Browse the repository at this point in the history
The current return type of base::SHA1HashSpan() is too long.

Change-Id: I6220807ba411933e9494bed09ce95bd0f5cc5c78
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2125625
Reviewed-by: Marc Treib <treib@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Daniel Hosseinian <dhoss@chromium.org>
Cr-Commit-Position: refs/heads/master@{#754781}
  • Loading branch information
Daniel Hosseinian authored and Commit Bot committed Mar 31, 2020
1 parent 4d86937 commit 5083e69
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions base/hash/sha1.cc
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ void SecureHashAlgorithm::Process() {
cursor = 0;
}

std::array<uint8_t, kSHA1Length> SHA1HashSpan(span<const uint8_t> data) {
std::array<uint8_t, kSHA1Length> hash;
SHA1Digest SHA1HashSpan(span<const uint8_t> data) {
SHA1Digest hash;
SHA1HashBytes(data.data(), data.size(), hash.data());
return hash;
}
Expand Down
6 changes: 4 additions & 2 deletions base/hash/sha1.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ namespace base {

enum { kSHA1Length = 20 }; // Length in bytes of a SHA-1 hash.

// The output of a SHA-1 operation.
using SHA1Digest = std::array<uint8_t, kSHA1Length>;

// Computes the SHA-1 hash of the input |data| and returns the full hash.
BASE_EXPORT std::array<uint8_t, kSHA1Length> SHA1HashSpan(
span<const uint8_t> data);
BASE_EXPORT SHA1Digest SHA1HashSpan(span<const uint8_t> data);

// Computes the SHA-1 hash of the input string |str| and returns the full
// hash.
Expand Down
4 changes: 2 additions & 2 deletions base/hash/sha1_boringssl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

namespace base {

std::array<uint8_t, kSHA1Length> SHA1HashSpan(span<const uint8_t> data) {
SHA1Digest SHA1HashSpan(span<const uint8_t> data) {
CRYPTO_library_init();
std::array<uint8_t, kSHA1Length> digest;
SHA1Digest digest;
SHA1(data.data(), data.size(), digest.data());
return digest;
}
Expand Down
6 changes: 3 additions & 3 deletions base/hash/sha1_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ TEST(SHA1Test, Test1BytesAndSpan) {
for (size_t i = 0; i < base::kSHA1Length; i++)
EXPECT_EQ(kExpected[i], output[i]);

std::array<uint8_t, base::kSHA1Length> output_array =
base::SHA1Digest output_array =
base::SHA1HashSpan(base::as_bytes(base::make_span(input)));
for (size_t i = 0; i < base::kSHA1Length; i++)
EXPECT_EQ(kExpected[i], output_array[i]);
Expand All @@ -88,7 +88,7 @@ TEST(SHA1Test, Test2BytesAndSpan) {
for (size_t i = 0; i < base::kSHA1Length; i++)
EXPECT_EQ(kExpected[i], output[i]);

std::array<uint8_t, base::kSHA1Length> output_array =
base::SHA1Digest output_array =
base::SHA1HashSpan(base::as_bytes(base::make_span(input)));
for (size_t i = 0; i < base::kSHA1Length; i++)
EXPECT_EQ(kExpected[i], output_array[i]);
Expand All @@ -108,7 +108,7 @@ TEST(SHA1Test, Test3BytesAndSpan) {
for (size_t i = 0; i < base::kSHA1Length; i++)
EXPECT_EQ(kExpected[i], output[i]);

std::array<uint8_t, base::kSHA1Length> output_array =
base::SHA1Digest output_array =
base::SHA1HashSpan(base::as_bytes(base::make_span(input)));
for (size_t i = 0; i < base::kSHA1Length; i++)
EXPECT_EQ(kExpected[i], output_array[i]);
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/supervised_user/supervised_user_site_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class SupervisedUserSiteList
size_t hash() const;

private:
std::array<uint8_t, base::kSHA1Length> bytes_;
base::SHA1Digest bytes_;
// Copy and assign are allowed.
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ std::string InferGuidForLegacyBookmark(

const std::string unique_tag =
base::StrCat({originator_cache_guid, originator_client_item_id});
const std::array<uint8_t, base::kSHA1Length> hash =
const base::SHA1Digest hash =
base::SHA1HashSpan(base::as_bytes(base::make_span(unique_tag)));

static_assert(base::kSHA1Length >= 16, "16 bytes needed to infer GUID");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ std::string InferGuidForLegacyBookmark(

const std::string unique_tag =
base::StrCat({originator_cache_guid, originator_client_item_id});
const std::array<uint8_t, base::kSHA1Length> hash =
const base::SHA1Digest hash =
base::SHA1HashSpan(base::as_bytes(base::make_span(unique_tag)));

static_assert(base::kSHA1Length >= 16, "16 bytes needed to infer GUID");
Expand Down
6 changes: 2 additions & 4 deletions printing/pdf_metafile_cg_mac_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,7 @@ TEST(PdfMetafileCgTest, RenderPageBasic) {
Metafile::MacRenderPageParams params;
params.autorotate = true;
ASSERT_TRUE(pdf_cg.RenderPage(1, context, bounds.ToCGRect(), params));
std::array<uint8_t, base::kSHA1Length> rendered_hash =
base::SHA1HashSpan(rendered_bitmap);
base::SHA1Digest rendered_hash = base::SHA1HashSpan(rendered_bitmap);

// Decode expected PNG and calculate the output hash.
std::vector<uint8_t> expected_png_bitmap;
Expand All @@ -179,8 +178,7 @@ TEST(PdfMetafileCgTest, RenderPageBasic) {
&expected_png_bitmap, &png_width, &png_height));
ASSERT_EQ(kExpectedWidth, png_width);
ASSERT_EQ(kExpectedHeight, png_height);
std::array<uint8_t, base::kSHA1Length> expected_hash =
base::SHA1HashSpan(expected_png_bitmap);
base::SHA1Digest expected_hash = base::SHA1HashSpan(expected_png_bitmap);

// Make sure the hashes match.
EXPECT_EQ(expected_hash, rendered_hash);
Expand Down

0 comments on commit 5083e69

Please sign in to comment.