diff --git a/esphome/components/esphome/ota/ota_esphome.cpp b/esphome/components/esphome/ota/ota_esphome.cpp index f503ff795e..7fd16ce3d0 100644 --- a/esphome/components/esphome/ota/ota_esphome.cpp +++ b/esphome/components/esphome/ota/ota_esphome.cpp @@ -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, uint8_t auth_request, const LogString *name, char *buf) { // 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 diff --git a/esphome/components/md5/md5.h b/esphome/components/md5/md5.h index cb0acefd7d..73d99205c0 100644 --- a/esphome/components/md5/md5.h +++ b/esphome/components/md5/md5.h @@ -42,8 +42,8 @@ class MD5Digest : public HashBase { /// Compute the digest, based on the provided data. void calculate() override; - /// Get the size of the hex output (32 for MD5) - size_t get_hex_size() const override { return 32; } + /// Get the size of the hash in bytes (16 for MD5) + size_t get_size() const override { return 16; } protected: MD5_CTX_TYPE ctx_{}; diff --git a/esphome/components/sha256/sha256.h b/esphome/components/sha256/sha256.h index 78cccd80f2..004b1e50fc 100644 --- a/esphome/components/sha256/sha256.h +++ b/esphome/components/sha256/sha256.h @@ -33,8 +33,8 @@ class SHA256 : public esphome::HashBase { void calculate() override; - /// Get the size of the hex output (64 for SHA256) - size_t get_hex_size() const override { return 64; } + /// Get the size of the hash in bytes (32 for SHA256) + size_t get_size() const override { return 32; } protected: #if defined(USE_ESP32) || defined(USE_LIBRETINY) diff --git a/esphome/core/hash_base.h b/esphome/core/hash_base.h index e35aee0475..1af2fd8907 100644 --- a/esphome/core/hash_base.h +++ b/esphome/core/hash_base.h @@ -23,15 +23,11 @@ class HashBase { virtual void calculate() = 0; /// Retrieve the hash as bytes - void get_bytes(uint8_t *output) { - const size_t hash_bytes = this->get_hex_size() / 2; - memcpy(output, this->digest_, hash_bytes); - } + void get_bytes(uint8_t *output) { memcpy(output, this->digest_, this->get_size()); } /// Retrieve the hash as hex characters void get_hex(char *output) { - const size_t hash_bytes = this->get_hex_size() / 2; - for (size_t i = 0; i < hash_bytes; i++) { + for (size_t i = 0; i < this->get_size(); i++) { uint8_t byte = this->digest_[i]; output[i * 2] = format_hex_char(byte >> 4); output[i * 2 + 1] = format_hex_char(byte & 0x0F); @@ -39,23 +35,19 @@ class HashBase { } /// Compare the hash against a provided byte-encoded hash - bool equals_bytes(const uint8_t *expected) { - const size_t hash_bytes = this->get_hex_size() / 2; - return memcmp(this->digest_, expected, hash_bytes) == 0; - } + bool equals_bytes(const uint8_t *expected) { return memcmp(this->digest_, expected, this->get_size()) == 0; } /// Compare the hash against a provided hex-encoded hash bool equals_hex(const char *expected) { - const size_t hash_bytes = this->get_hex_size() / 2; 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 this->equals_bytes(parsed); } - /// Get the size of the hex output (32 for MD5, 64 for SHA256) - virtual size_t get_hex_size() const = 0; + /// Get the size of the hash in bytes (16 for MD5, 32 for SHA256) + virtual size_t get_size() const = 0; protected: uint8_t digest_[32]; // Common digest storage, sized for largest hash (SHA256)