diff --git a/esphome/components/md5/md5.cpp b/esphome/components/md5/md5.cpp index 8f31e0de61..866f00eda4 100644 --- a/esphome/components/md5/md5.cpp +++ b/esphome/components/md5/md5.cpp @@ -39,13 +39,6 @@ void MD5Digest::add(const uint8_t *data, size_t len) { br_md5_update(&this->ctx_ void MD5Digest::calculate() { br_md5_out(&this->ctx_, this->digest_); } #endif // USE_RP2040 -bool MD5Digest::equals_hex(const char *expected) { - uint8_t parsed[16]; - if (!parse_hex(expected, parsed, 16)) - return false; - return equals_bytes(parsed); -} - } // namespace md5 } // namespace esphome #endif diff --git a/esphome/components/md5/md5.h b/esphome/components/md5/md5.h index a3a17b95c5..cb0acefd7d 100644 --- a/esphome/components/md5/md5.h +++ b/esphome/components/md5/md5.h @@ -45,9 +45,6 @@ class MD5Digest : public HashBase { /// Get the size of the hex output (32 for MD5) size_t get_hex_size() const override { return 32; } - /// Compare the digest against a provided hex-encoded digest (32 bytes). - bool equals_hex(const char *expected); - protected: MD5_CTX_TYPE ctx_{}; }; diff --git a/esphome/components/sha256/sha256.cpp b/esphome/components/sha256/sha256.cpp index 5bde76aaa8..24d15be4a7 100644 --- a/esphome/components/sha256/sha256.cpp +++ b/esphome/components/sha256/sha256.cpp @@ -84,14 +84,6 @@ std::string SHA256::get_hex_string() { return std::string(buf); } -bool SHA256::equals_hex(const char *expected) { - uint8_t parsed[32]; - if (!parse_hex(expected, parsed, 32)) { - return false; - } - return this->equals_bytes(parsed); -} - } // namespace esphome::sha256 #endif // Platform check diff --git a/esphome/components/sha256/sha256.h b/esphome/components/sha256/sha256.h index 8b6043729c..f650008ac7 100644 --- a/esphome/components/sha256/sha256.h +++ b/esphome/components/sha256/sha256.h @@ -38,8 +38,6 @@ class SHA256 : public esphome::HashBase { /// Get the size of the hex output (64 for SHA256) size_t get_hex_size() const override { return 64; } - bool equals_hex(const char *expected); - protected: #if defined(USE_ESP32) || defined(USE_LIBRETINY) mbedtls_sha256_context ctx_{}; diff --git a/esphome/core/hash_base.h b/esphome/core/hash_base.h index 346f2c6086..d92533df78 100644 --- a/esphome/core/hash_base.h +++ b/esphome/core/hash_base.h @@ -22,8 +22,18 @@ class HashBase { /// Compute the hash based on provided data 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); + } + /// Retrieve the hash as hex characters +<<<<<<< HEAD virtual void get_hex(char *output) { +======= + void get_hex(char *output) { +>>>>>>> integration const size_t hash_bytes = this->get_hex_size() / 2; for (size_t i = 0; i < hash_bytes; i++) { uint8_t byte = this->digest_[i]; @@ -32,23 +42,43 @@ class HashBase { } } +<<<<<<< HEAD /// 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); } +======= +>>>>>>> integration /// 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; } +<<<<<<< HEAD +======= + + /// 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)) { + return false; + } + return this->equals_bytes(parsed); + } +>>>>>>> integration /// Get the size of the hex output (32 for MD5, 64 for SHA256) virtual size_t get_hex_size() const = 0; protected: +<<<<<<< HEAD uint8_t digest_[32]; // Common digest storage (MD5 uses 16 bytes, SHA256 uses 32) +======= + uint8_t digest_[32]; // Common digest storage, sized for largest hash (SHA256) +>>>>>>> integration }; } // namespace esphome