From 8ea13115a0304f1b124bb526d0170982fc55e211 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 21 Sep 2025 12:06:12 -0600 Subject: [PATCH] preen --- esphome/components/md5/md5.cpp | 12 ------------ esphome/components/md5/md5.h | 12 ------------ esphome/components/sha256/sha256.cpp | 12 ------------ esphome/components/sha256/sha256.h | 8 -------- esphome/core/hash_base.h | 23 +++++++++++++++++++++-- 5 files changed, 21 insertions(+), 46 deletions(-) diff --git a/esphome/components/md5/md5.cpp b/esphome/components/md5/md5.cpp index 202e25cadd..866f00eda4 100644 --- a/esphome/components/md5/md5.cpp +++ b/esphome/components/md5/md5.cpp @@ -39,18 +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 -void MD5Digest::get_bytes(uint8_t *output) { memcpy(output, this->digest_, 16); } - -void MD5Digest::get_hex(char *output) { - for (size_t i = 0; i < 16; i++) { - uint8_t byte = this->digest_[i]; - output[i * 2] = format_hex_char(byte >> 4); - output[i * 2 + 1] = format_hex_char(byte & 0x0F); - } -} - -bool MD5Digest::equals_bytes(const uint8_t *expected) { return memcmp(this->digest_, expected, 16) == 0; } - } // namespace md5 } // namespace esphome #endif diff --git a/esphome/components/md5/md5.h b/esphome/components/md5/md5.h index 4c741ea536..cb0acefd7d 100644 --- a/esphome/components/md5/md5.h +++ b/esphome/components/md5/md5.h @@ -42,23 +42,11 @@ class MD5Digest : public HashBase { /// Compute the digest, based on the provided data. void calculate() override; - /// Retrieve the MD5 digest as bytes. - /// The output must be able to hold 16 bytes or more. - void get_bytes(uint8_t *output); - - /// Retrieve the MD5 digest as hex characters. - /// The output must be able to hold 32 bytes or more. - void get_hex(char *output) override; - /// 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 byte-encoded digest (16 bytes) - bool equals_bytes(const uint8_t *expected) override; - protected: MD5_CTX_TYPE ctx_{}; - uint8_t digest_[16]; }; } // namespace md5 diff --git a/esphome/components/sha256/sha256.cpp b/esphome/components/sha256/sha256.cpp index 8042700a10..24d15be4a7 100644 --- a/esphome/components/sha256/sha256.cpp +++ b/esphome/components/sha256/sha256.cpp @@ -78,18 +78,6 @@ void SHA256::calculate() { #error "SHA256 not supported on this platform" #endif -void SHA256::get_bytes(uint8_t *output) { memcpy(output, this->digest_, 32); } - -void SHA256::get_hex(char *output) { - for (size_t i = 0; i < 32; i++) { - uint8_t byte = this->digest_[i]; - output[i * 2] = format_hex_char(byte >> 4); - output[i * 2 + 1] = format_hex_char(byte & 0x0F); - } -} - -bool SHA256::equals_bytes(const uint8_t *expected) { return memcmp(this->digest_, expected, 32) == 0; } - std::string SHA256::get_hex_string() { char buf[65]; this->get_hex(buf); diff --git a/esphome/components/sha256/sha256.h b/esphome/components/sha256/sha256.h index 9ba1b19802..f650008ac7 100644 --- a/esphome/components/sha256/sha256.h +++ b/esphome/components/sha256/sha256.h @@ -33,27 +33,19 @@ class SHA256 : public esphome::HashBase { void calculate() override; - void get_bytes(uint8_t *output); - void get_hex(char *output) override; std::string get_hex_string(); /// Get the size of the hex output (64 for SHA256) size_t get_hex_size() const override { return 64; } - /// Compare the digest against a provided byte-encoded digest (32 bytes) - bool equals_bytes(const uint8_t *expected) override; - protected: #if defined(USE_ESP32) || defined(USE_LIBRETINY) mbedtls_sha256_context ctx_{}; - uint8_t digest_[32]; #elif defined(USE_ESP8266) || defined(USE_RP2040) br_sha256_context ctx_{}; - uint8_t digest_[32]; bool calculated_{false}; #elif defined(USE_HOST) EVP_MD_CTX *ctx_{nullptr}; - uint8_t digest_[32]; bool calculated_{false}; #else #error "SHA256 not supported on this platform" diff --git a/esphome/core/hash_base.h b/esphome/core/hash_base.h index 48944b9e7a..e35aee0475 100644 --- a/esphome/core/hash_base.h +++ b/esphome/core/hash_base.h @@ -22,11 +22,27 @@ 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 - virtual void get_hex(char *output) = 0; + void get_hex(char *output) { + 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]; + output[i * 2] = format_hex_char(byte >> 4); + output[i * 2 + 1] = format_hex_char(byte & 0x0F); + } + } /// Compare the hash against a provided byte-encoded hash - virtual bool equals_bytes(const uint8_t *expected) = 0; + 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; + } /// Compare the hash against a provided hex-encoded hash bool equals_hex(const char *expected) { @@ -40,6 +56,9 @@ class HashBase { /// Get the size of the hex output (32 for MD5, 64 for SHA256) virtual size_t get_hex_size() const = 0; + + protected: + uint8_t digest_[32]; // Common digest storage, sized for largest hash (SHA256) }; } // namespace esphome