refactor: convert HashAlgorithm to enum class (#38233)

This commit is contained in:
Milan Burda 2023-05-12 15:23:42 +02:00 committed by GitHub
parent 3dbc0a365f
commit 88a9962e22
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 12 additions and 12 deletions

View file

@ -46,11 +46,11 @@ void AsarFileValidator::OnRead(base::span<char> buffer,
if (!current_hash_) { if (!current_hash_) {
current_hash_byte_count_ = 0; current_hash_byte_count_ = 0;
switch (integrity_.algorithm) { switch (integrity_.algorithm) {
case HashAlgorithm::SHA256: case HashAlgorithm::kSHA256:
current_hash_ = current_hash_ =
crypto::SecureHash::Create(crypto::SecureHash::SHA256); crypto::SecureHash::Create(crypto::SecureHash::SHA256);
break; break;
case HashAlgorithm::NONE: case HashAlgorithm::kNone:
CHECK(false); CHECK(false);
break; break;
} }

View file

@ -89,10 +89,10 @@ class Archive : public node::ObjectWrap {
gin_helper::Dictionary integrity(isolate, v8::Object::New(isolate)); gin_helper::Dictionary integrity(isolate, v8::Object::New(isolate));
asar::HashAlgorithm algorithm = info.integrity.value().algorithm; asar::HashAlgorithm algorithm = info.integrity.value().algorithm;
switch (algorithm) { switch (algorithm) {
case asar::HashAlgorithm::SHA256: case asar::HashAlgorithm::kSHA256:
integrity.Set("algorithm", "SHA256"); integrity.Set("algorithm", "SHA256");
break; break;
case asar::HashAlgorithm::NONE: case asar::HashAlgorithm::kNone:
CHECK(false); CHECK(false);
break; break;
} }

View file

@ -138,7 +138,7 @@ bool FillFileInfoWithNode(Archive::FileInfo* info,
} }
} }
if (*algorithm == "SHA256") { if (*algorithm == "SHA256") {
integrity_payload.algorithm = HashAlgorithm::SHA256; integrity_payload.algorithm = HashAlgorithm::kSHA256;
info->integrity = std::move(integrity_payload); info->integrity = std::move(integrity_payload);
} }
} }
@ -157,7 +157,7 @@ bool FillFileInfoWithNode(Archive::FileInfo* info,
} // namespace } // namespace
IntegrityPayload::IntegrityPayload() IntegrityPayload::IntegrityPayload()
: algorithm(HashAlgorithm::NONE), block_size(0) {} : algorithm(HashAlgorithm::kNone), block_size(0) {}
IntegrityPayload::~IntegrityPayload() = default; IntegrityPayload::~IntegrityPayload() = default;
IntegrityPayload::IntegrityPayload(const IntegrityPayload& other) = default; IntegrityPayload::IntegrityPayload(const IntegrityPayload& other) = default;
@ -252,7 +252,7 @@ bool Archive::Init() {
// Currently we only support the sha256 algorithm, we can add support for // Currently we only support the sha256 algorithm, we can add support for
// more below ensure we read them in preference order from most secure to // more below ensure we read them in preference order from most secure to
// least // least
if (integrity.value().algorithm != HashAlgorithm::NONE) { if (integrity.value().algorithm != HashAlgorithm::kNone) {
ValidateIntegrityOrDie(header.c_str(), header.length(), ValidateIntegrityOrDie(header.c_str(), header.length(),
integrity.value()); integrity.value());
} else { } else {

View file

@ -20,9 +20,9 @@ namespace asar {
class ScopedTemporaryFile; class ScopedTemporaryFile;
enum HashAlgorithm { enum class HashAlgorithm {
SHA256, kSHA256,
NONE, kNone,
}; };
struct IntegrityPayload { struct IntegrityPayload {

View file

@ -56,7 +56,7 @@ absl::optional<IntegrityPayload> Archive::HeaderIntegrity() const {
NSString* hash = [integrity_payload objectForKey:@"hash"]; NSString* hash = [integrity_payload objectForKey:@"hash"];
if (algorithm && hash && [algorithm isEqualToString:@"SHA256"]) { if (algorithm && hash && [algorithm isEqualToString:@"SHA256"]) {
IntegrityPayload header_integrity; IntegrityPayload header_integrity;
header_integrity.algorithm = HashAlgorithm::SHA256; header_integrity.algorithm = HashAlgorithm::kSHA256;
header_integrity.hash = base::SysNSStringToUTF8(hash); header_integrity.hash = base::SysNSStringToUTF8(hash);
return header_integrity; return header_integrity;
} }

View file

@ -152,7 +152,7 @@ bool ReadFileToString(const base::FilePath& path, std::string* contents) {
void ValidateIntegrityOrDie(const char* data, void ValidateIntegrityOrDie(const char* data,
size_t size, size_t size,
const IntegrityPayload& integrity) { const IntegrityPayload& integrity) {
if (integrity.algorithm == HashAlgorithm::SHA256) { if (integrity.algorithm == HashAlgorithm::kSHA256) {
uint8_t hash[crypto::kSHA256Length]; uint8_t hash[crypto::kSHA256Length];
auto hasher = crypto::SecureHash::Create(crypto::SecureHash::SHA256); auto hasher = crypto::SecureHash::Create(crypto::SecureHash::SHA256);
hasher->Update(data, size); hasher->Update(data, size);