Convert SHA256_LENGTH from a constant-in-anonymous-enum to a static const. This defines the constant where it's declared to preserve the existing readability.
Normally this makes things like DCHECK_EQ() unhappy, but when I'd originally tested this I didn't seem to need to make any changes due to that. Will be watching the trybots... The original motiviation for this change was to find a way to eliminate some cases of passing anonymous-typed values as template arguments (which happens when you use a value from the enum in e.g. EXPECT_EQ()), which is technically illegal in C++03, though we don't warn about it. Simply naming the enum would have done this, but in general naming enums used to declare constants like this is bizarre ("enum Constants { ... }"?). BUG=92247 TEST=Compiles Review URL: http://codereview.chromium.org/7823004 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@102369 0039d316-1c4b-4281-b951-d872f2087c98
This commit is contained in:
chrome/browser
extensions
safe_browsing
crypto
net/base
@ -73,10 +73,10 @@ scoped_refptr<Extension> ConvertUserScriptToExtension(
|
||||
// identity is its namespace+name, so we hash that to create a public key.
|
||||
// There will be no corresponding private key, which means user scripts cannot
|
||||
// be auto-updated, or claimed in the gallery.
|
||||
char raw[crypto::SHA256_LENGTH] = {0};
|
||||
char raw[crypto::kSHA256Length] = {0};
|
||||
std::string key;
|
||||
crypto::SHA256HashString(script_name, raw, crypto::SHA256_LENGTH);
|
||||
base::Base64Encode(std::string(raw, crypto::SHA256_LENGTH), &key);
|
||||
crypto::SHA256HashString(script_name, raw, crypto::kSHA256Length);
|
||||
base::Base64Encode(std::string(raw, crypto::kSHA256Length), &key);
|
||||
|
||||
// The script may not have a name field, but we need one for an extension. If
|
||||
// it is missing, use the filename of the original URL.
|
||||
|
@ -46,12 +46,11 @@ const char kIconsDirName[] = "icons";
|
||||
// auto-updated using ExtensionUpdater. But Chrome does notice updates to the
|
||||
// manifest and regenerates these extensions.
|
||||
std::string GenerateKey(const GURL& manifest_url) {
|
||||
char raw[crypto::SHA256_LENGTH] = {0};
|
||||
char raw[crypto::kSHA256Length] = {0};
|
||||
std::string key;
|
||||
crypto::SHA256HashString(manifest_url.spec().c_str(),
|
||||
raw,
|
||||
crypto::SHA256_LENGTH);
|
||||
base::Base64Encode(std::string(raw, crypto::SHA256_LENGTH), &key);
|
||||
crypto::SHA256HashString(manifest_url.spec().c_str(), raw,
|
||||
crypto::kSHA256Length);
|
||||
base::Base64Encode(std::string(raw, crypto::kSHA256Length), &key);
|
||||
return key;
|
||||
}
|
||||
|
||||
|
@ -794,10 +794,10 @@ void ExtensionUpdater::HandleManifestResults(
|
||||
void ExtensionUpdater::ProcessBlacklist(const std::string& data) {
|
||||
DCHECK(alive_);
|
||||
// Verify sha256 hash value.
|
||||
char sha256_hash_value[crypto::SHA256_LENGTH];
|
||||
crypto::SHA256HashString(data, sha256_hash_value, crypto::SHA256_LENGTH);
|
||||
char sha256_hash_value[crypto::kSHA256Length];
|
||||
crypto::SHA256HashString(data, sha256_hash_value, crypto::kSHA256Length);
|
||||
std::string hash_in_hex = base::HexEncode(sha256_hash_value,
|
||||
crypto::SHA256_LENGTH);
|
||||
crypto::kSHA256Length);
|
||||
|
||||
if (current_extension_fetch_.package_hash != hash_in_hex) {
|
||||
NOTREACHED() << "Fetched blacklist checksum is not as expected. "
|
||||
|
@ -563,7 +563,7 @@ void ClientSideDetectionService::SetBadSubnets(const ClientSideModel& model,
|
||||
DLOG(ERROR) << "Invalid bad subnet size: " << size;
|
||||
continue;
|
||||
}
|
||||
if (model.bad_subnet(i).prefix().size() != crypto::SHA256_LENGTH) {
|
||||
if (model.bad_subnet(i).prefix().size() != crypto::kSHA256Length) {
|
||||
DLOG(ERROR) << "Invalid bad subnet prefix length: "
|
||||
<< model.bad_subnet(i).prefix().size();
|
||||
continue;
|
||||
|
@ -432,11 +432,11 @@ TEST_F(ClientSideDetectionServiceTest, SetBadSubnets) {
|
||||
|
||||
// Bad subnets are skipped.
|
||||
ClientSideModel::IPSubnet* subnet = model.add_bad_subnet();
|
||||
subnet->set_prefix(std::string(crypto::SHA256_LENGTH, '.'));
|
||||
subnet->set_prefix(std::string(crypto::kSHA256Length, '.'));
|
||||
subnet->set_size(130); // Invalid size.
|
||||
|
||||
subnet = model.add_bad_subnet();
|
||||
subnet->set_prefix(std::string(crypto::SHA256_LENGTH, '.'));
|
||||
subnet->set_prefix(std::string(crypto::kSHA256Length, '.'));
|
||||
subnet->set_size(-1); // Invalid size.
|
||||
|
||||
subnet = model.add_bad_subnet();
|
||||
@ -447,19 +447,19 @@ TEST_F(ClientSideDetectionServiceTest, SetBadSubnets) {
|
||||
EXPECT_EQ(0U, bad_subnets.size());
|
||||
|
||||
subnet = model.add_bad_subnet();
|
||||
subnet->set_prefix(std::string(crypto::SHA256_LENGTH, '.'));
|
||||
subnet->set_prefix(std::string(crypto::kSHA256Length, '.'));
|
||||
subnet->set_size(64);
|
||||
|
||||
subnet = model.add_bad_subnet();
|
||||
subnet->set_prefix(std::string(crypto::SHA256_LENGTH, ','));
|
||||
subnet->set_prefix(std::string(crypto::kSHA256Length, ','));
|
||||
subnet->set_size(64);
|
||||
|
||||
subnet = model.add_bad_subnet();
|
||||
subnet->set_prefix(std::string(crypto::SHA256_LENGTH, '.'));
|
||||
subnet->set_prefix(std::string(crypto::kSHA256Length, '.'));
|
||||
subnet->set_size(128);
|
||||
|
||||
subnet = model.add_bad_subnet();
|
||||
subnet->set_prefix(std::string(crypto::SHA256_LENGTH, '.'));
|
||||
subnet->set_prefix(std::string(crypto::kSHA256Length, '.'));
|
||||
subnet->set_size(100);
|
||||
|
||||
ClientSideDetectionService::SetBadSubnets(model, &bad_subnets);
|
||||
@ -467,16 +467,16 @@ TEST_F(ClientSideDetectionServiceTest, SetBadSubnets) {
|
||||
ClientSideDetectionService::BadSubnetMap::const_iterator it;
|
||||
std::string mask = std::string(8, '\xFF') + std::string(8, '\x00');
|
||||
EXPECT_TRUE(bad_subnets.count(mask));
|
||||
EXPECT_TRUE(bad_subnets[mask].count(std::string(crypto::SHA256_LENGTH, '.')));
|
||||
EXPECT_TRUE(bad_subnets[mask].count(std::string(crypto::SHA256_LENGTH, ',')));
|
||||
EXPECT_TRUE(bad_subnets[mask].count(std::string(crypto::kSHA256Length, '.')));
|
||||
EXPECT_TRUE(bad_subnets[mask].count(std::string(crypto::kSHA256Length, ',')));
|
||||
|
||||
mask = std::string(16, '\xFF');
|
||||
EXPECT_TRUE(bad_subnets.count(mask));
|
||||
EXPECT_TRUE(bad_subnets[mask].count(std::string(crypto::SHA256_LENGTH, '.')));
|
||||
EXPECT_TRUE(bad_subnets[mask].count(std::string(crypto::kSHA256Length, '.')));
|
||||
|
||||
mask = std::string(12, '\xFF') + "\xF0" + std::string(3, '\x00');
|
||||
EXPECT_TRUE(bad_subnets.count(mask));
|
||||
EXPECT_TRUE(bad_subnets[mask].count(std::string(crypto::SHA256_LENGTH, '.')));
|
||||
EXPECT_TRUE(bad_subnets[mask].count(std::string(crypto::kSHA256Length, '.')));
|
||||
}
|
||||
|
||||
TEST_F(ClientSideDetectionServiceTest, IsBadIpAddress) {
|
||||
|
@ -539,12 +539,12 @@ GURL GeneratePhishingReportUrl(const std::string& report_page,
|
||||
}
|
||||
|
||||
void StringToSBFullHash(const std::string& hash_in, SBFullHash* hash_out) {
|
||||
DCHECK_EQ(static_cast<size_t>(crypto::SHA256_LENGTH), hash_in.size());
|
||||
memcpy(hash_out->full_hash, hash_in.data(), crypto::SHA256_LENGTH);
|
||||
DCHECK_EQ(crypto::kSHA256Length, hash_in.size());
|
||||
memcpy(hash_out->full_hash, hash_in.data(), crypto::kSHA256Length);
|
||||
}
|
||||
|
||||
std::string SBFullHashToString(const SBFullHash& hash) {
|
||||
DCHECK_EQ(static_cast<size_t>(crypto::SHA256_LENGTH), sizeof(hash.full_hash));
|
||||
DCHECK_EQ(crypto::kSHA256Length, sizeof(hash.full_hash));
|
||||
return std::string(hash.full_hash, sizeof(hash.full_hash));
|
||||
}
|
||||
} // namespace safe_browsing_util
|
||||
|
@ -21,7 +21,7 @@ TEST(SecureHashTest, TestUpdate) {
|
||||
0x04, 0x6d, 0x39, 0xcc,
|
||||
0xc7, 0x11, 0x2c, 0xd0 };
|
||||
|
||||
uint8 output3[crypto::SHA256_LENGTH];
|
||||
uint8 output3[crypto::kSHA256Length];
|
||||
|
||||
scoped_ptr<crypto::SecureHash> ctx(crypto::SecureHash::Create(
|
||||
crypto::SecureHash::SHA256));
|
||||
@ -29,6 +29,6 @@ TEST(SecureHashTest, TestUpdate) {
|
||||
ctx->Update(input3.data(), input3.size());
|
||||
|
||||
ctx->Finish(output3, sizeof(output3));
|
||||
for (size_t i = 0; i < crypto::SHA256_LENGTH; i++)
|
||||
for (size_t i = 0; i < crypto::kSHA256Length; i++)
|
||||
EXPECT_EQ(expected3[i], static_cast<int>(output3[i]));
|
||||
}
|
||||
|
@ -17,7 +17,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);
|
||||
std::string output(kSHA256Length, 0);
|
||||
SHA256HashString(str, string_as_array(&output), output.size());
|
||||
return output;
|
||||
}
|
||||
|
@ -16,9 +16,7 @@ namespace crypto {
|
||||
//
|
||||
// Functions for SHA-384 and SHA-512 can be added when the need arises.
|
||||
|
||||
enum {
|
||||
SHA256_LENGTH = 32 // length in bytes of a SHA-256 hash
|
||||
};
|
||||
static const size_t kSHA256Length = 32; // length in bytes of a SHA-256 hash
|
||||
|
||||
// Computes the SHA-256 hash of the input string 'str' and stores the first
|
||||
// 'len' bytes of the hash in the output buffer 'output'. If 'len' > 32,
|
||||
|
@ -19,9 +19,9 @@ TEST(Sha256Test, Test1) {
|
||||
0xb4, 0x10, 0xff, 0x61,
|
||||
0xf2, 0x00, 0x15, 0xad };
|
||||
|
||||
uint8 output1[crypto::SHA256_LENGTH];
|
||||
uint8 output1[crypto::kSHA256Length];
|
||||
crypto::SHA256HashString(input1, output1, sizeof(output1));
|
||||
for (size_t i = 0; i < crypto::SHA256_LENGTH; i++)
|
||||
for (size_t i = 0; i < crypto::kSHA256Length; i++)
|
||||
EXPECT_EQ(expected1[i], static_cast<int>(output1[i]));
|
||||
|
||||
uint8 output_truncated1[4]; // 4 bytes == 32 bits
|
||||
@ -45,8 +45,8 @@ TEST(Sha256Test, Test1_String) {
|
||||
0xf2, 0x00, 0x15, 0xad };
|
||||
|
||||
std::string output1 = crypto::SHA256HashString(input1);
|
||||
ASSERT_EQ(crypto::SHA256_LENGTH, output1.size());
|
||||
for (size_t i = 0; i < crypto::SHA256_LENGTH; i++)
|
||||
ASSERT_EQ(crypto::kSHA256Length, output1.size());
|
||||
for (size_t i = 0; i < crypto::kSHA256Length; i++)
|
||||
EXPECT_EQ(expected1[i], static_cast<uint8>(output1[i]));
|
||||
}
|
||||
|
||||
@ -63,9 +63,9 @@ TEST(Sha256Test, Test2) {
|
||||
0xf6, 0xec, 0xed, 0xd4,
|
||||
0x19, 0xdb, 0x06, 0xc1 };
|
||||
|
||||
uint8 output2[crypto::SHA256_LENGTH];
|
||||
uint8 output2[crypto::kSHA256Length];
|
||||
crypto::SHA256HashString(input2, output2, sizeof(output2));
|
||||
for (size_t i = 0; i < crypto::SHA256_LENGTH; i++)
|
||||
for (size_t i = 0; i < crypto::kSHA256Length; i++)
|
||||
EXPECT_EQ(expected2[i], static_cast<int>(output2[i]));
|
||||
|
||||
uint8 output_truncated2[6];
|
||||
@ -87,9 +87,9 @@ TEST(Sha256Test, Test3) {
|
||||
0x04, 0x6d, 0x39, 0xcc,
|
||||
0xc7, 0x11, 0x2c, 0xd0 };
|
||||
|
||||
uint8 output3[crypto::SHA256_LENGTH];
|
||||
uint8 output3[crypto::kSHA256Length];
|
||||
crypto::SHA256HashString(input3, output3, sizeof(output3));
|
||||
for (size_t i = 0; i < crypto::SHA256_LENGTH; i++)
|
||||
for (size_t i = 0; i < crypto::kSHA256Length; i++)
|
||||
EXPECT_EQ(expected3[i], static_cast<int>(output3[i]));
|
||||
|
||||
uint8 output_truncated3[12];
|
||||
|
@ -152,10 +152,10 @@ static const int kCurrentFileVersion = 0;
|
||||
|
||||
static bool ReadCRL(base::StringPiece* data, std::string* out_parent_spki_hash,
|
||||
std::vector<std::string>* out_serials) {
|
||||
if (data->size() < crypto::SHA256_LENGTH)
|
||||
if (data->size() < crypto::kSHA256Length)
|
||||
return false;
|
||||
*out_parent_spki_hash = std::string(data->data(), crypto::SHA256_LENGTH);
|
||||
data->remove_prefix(crypto::SHA256_LENGTH);
|
||||
*out_parent_spki_hash = std::string(data->data(), crypto::kSHA256Length);
|
||||
data->remove_prefix(crypto::kSHA256Length);
|
||||
|
||||
if (data->size() < sizeof(uint32))
|
||||
return false;
|
||||
|
@ -428,7 +428,7 @@ bool DNSSECChainVerifier::DigestKey(base::StringPiece* out,
|
||||
uint16 keyid,
|
||||
uint8 algorithm) {
|
||||
std::string temp;
|
||||
uint8 temp2[crypto::SHA256_LENGTH];
|
||||
uint8 temp2[crypto::kSHA256Length];
|
||||
const uint8* digest;
|
||||
unsigned digest_len;
|
||||
|
||||
|
@ -47,7 +47,7 @@ TransportSecurityState::TransportSecurityState(const std::string& hsts_hosts)
|
||||
}
|
||||
|
||||
static std::string HashHost(const std::string& canonicalized_host) {
|
||||
char hashed[crypto::SHA256_LENGTH];
|
||||
char hashed[crypto::kSHA256Length];
|
||||
crypto::SHA256HashString(canonicalized_host, hashed, sizeof(hashed));
|
||||
return std::string(hashed, sizeof(hashed));
|
||||
}
|
||||
@ -522,7 +522,7 @@ static std::string HashedDomainToExternalString(const std::string& hashed) {
|
||||
static std::string ExternalStringToHashedDomain(const std::string& external) {
|
||||
std::string out;
|
||||
if (!base::Base64Decode(external, &out) ||
|
||||
out.size() != crypto::SHA256_LENGTH) {
|
||||
out.size() != crypto::kSHA256Length) {
|
||||
return std::string();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user