1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-23 21:52:23 +01:00
This commit is contained in:
J. Nick Koston
2025-09-21 15:53:23 -06:00
parent ba5e995fc1
commit b1f90fb78d
4 changed files with 11 additions and 19 deletions

View File

@@ -532,7 +532,7 @@ void ESPHomeOTAComponent::log_auth_warning_(const LogString *action, const LogSt
bool ESPHomeOTAComponent::perform_hash_auth_(HashBase *hasher, const std::string &password, size_t nonce_size, bool ESPHomeOTAComponent::perform_hash_auth_(HashBase *hasher, const std::string &password, size_t nonce_size,
uint8_t auth_request, const LogString *name, char *buf) { uint8_t auth_request, const LogString *name, char *buf) {
// Get sizes from the hasher // Get sizes from the hasher
const size_t hex_size = hasher->get_hex_size(); const size_t hex_size = hasher->get_size() * 2; // Hex is twice the byte size
// Use the provided buffer for all hex operations // Use the provided buffer for all hex operations

View File

@@ -42,8 +42,8 @@ class MD5Digest : public HashBase {
/// Compute the digest, based on the provided data. /// Compute the digest, based on the provided data.
void calculate() override; void calculate() override;
/// Get the size of the hex output (32 for MD5) /// Get the size of the hash in bytes (16 for MD5)
size_t get_hex_size() const override { return 32; } size_t get_size() const override { return 16; }
protected: protected:
MD5_CTX_TYPE ctx_{}; MD5_CTX_TYPE ctx_{};

View File

@@ -33,8 +33,8 @@ class SHA256 : public esphome::HashBase {
void calculate() override; void calculate() override;
/// Get the size of the hex output (64 for SHA256) /// Get the size of the hash in bytes (32 for SHA256)
size_t get_hex_size() const override { return 64; } size_t get_size() const override { return 32; }
protected: protected:
#if defined(USE_ESP32) || defined(USE_LIBRETINY) #if defined(USE_ESP32) || defined(USE_LIBRETINY)

View File

@@ -23,15 +23,11 @@ class HashBase {
virtual void calculate() = 0; virtual void calculate() = 0;
/// Retrieve the hash as bytes /// Retrieve the hash as bytes
void get_bytes(uint8_t *output) { void get_bytes(uint8_t *output) { memcpy(output, this->digest_, this->get_size()); }
const size_t hash_bytes = this->get_hex_size() / 2;
memcpy(output, this->digest_, hash_bytes);
}
/// Retrieve the hash as hex characters /// Retrieve the hash as hex characters
void get_hex(char *output) { void get_hex(char *output) {
const size_t hash_bytes = this->get_hex_size() / 2; for (size_t i = 0; i < this->get_size(); i++) {
for (size_t i = 0; i < hash_bytes; i++) {
uint8_t byte = this->digest_[i]; uint8_t byte = this->digest_[i];
output[i * 2] = format_hex_char(byte >> 4); output[i * 2] = format_hex_char(byte >> 4);
output[i * 2 + 1] = format_hex_char(byte & 0x0F); output[i * 2 + 1] = format_hex_char(byte & 0x0F);
@@ -39,23 +35,19 @@ class HashBase {
} }
/// Compare the hash against a provided byte-encoded hash /// Compare the hash against a provided byte-encoded hash
bool equals_bytes(const uint8_t *expected) { bool equals_bytes(const uint8_t *expected) { return memcmp(this->digest_, expected, this->get_size()) == 0; }
const size_t hash_bytes = this->get_hex_size() / 2;
return memcmp(this->digest_, expected, hash_bytes) == 0;
}
/// Compare the hash against a provided hex-encoded hash /// Compare the hash against a provided hex-encoded hash
bool equals_hex(const char *expected) { bool equals_hex(const char *expected) {
const size_t hash_bytes = this->get_hex_size() / 2;
uint8_t parsed[32]; // Max size for SHA256 uint8_t parsed[32]; // Max size for SHA256
if (!parse_hex(expected, parsed, hash_bytes)) { if (!parse_hex(expected, parsed, this->get_size())) {
return false; return false;
} }
return this->equals_bytes(parsed); return this->equals_bytes(parsed);
} }
/// Get the size of the hex output (32 for MD5, 64 for SHA256) /// Get the size of the hash in bytes (16 for MD5, 32 for SHA256)
virtual size_t get_hex_size() const = 0; virtual size_t get_size() const = 0;
protected: protected:
uint8_t digest_[32]; // Common digest storage, sized for largest hash (SHA256) uint8_t digest_[32]; // Common digest storage, sized for largest hash (SHA256)