Introduce hex parsing & formatting helper functions (#2882)

This commit is contained in:
Oxan van Leeuwen
2021-12-12 21:15:23 +01:00
committed by GitHub
parent b2f05faee0
commit beeb0c7c5a
20 changed files with 186 additions and 109 deletions
+5 -13
View File
@@ -23,7 +23,7 @@ void MD5Digest::get_hex(char *output) {
}
}
bool MD5Digest::equals_bytes(const char *expected) {
bool MD5Digest::equals_bytes(const uint8_t *expected) {
for (size_t i = 0; i < 16; i++) {
if (expected[i] != this->digest_[i]) {
return false;
@@ -33,18 +33,10 @@ bool MD5Digest::equals_bytes(const char *expected) {
}
bool MD5Digest::equals_hex(const char *expected) {
for (size_t i = 0; i < 16; i++) {
auto high = parse_hex(expected[i * 2]);
auto low = parse_hex(expected[i * 2 + 1]);
if (!high.has_value() || !low.has_value()) {
return false;
}
auto value = (*high << 4) | *low;
if (value != this->digest_[i]) {
return false;
}
}
return true;
uint8_t parsed[16];
if (!parse_hex(expected, parsed, 16))
return false;
return equals_bytes(parsed);
}
} // namespace md5
+1 -1
View File
@@ -44,7 +44,7 @@ class MD5Digest {
void get_hex(char *output);
/// Compare the digest against a provided byte-encoded digest (16 bytes).
bool equals_bytes(const char *expected);
bool equals_bytes(const uint8_t *expected);
/// Compare the digest against a provided hex-encoded digest (32 bytes).
bool equals_hex(const char *expected);