From 991409d315a9103aa1ab93c64bb35845a975c743 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 21 Sep 2025 11:56:16 -0600 Subject: [PATCH] cleanup --- esphome/components/md5/md5.cpp | 11 ----------- esphome/components/md5/md5.h | 7 ------- esphome/components/sha256/sha256.cpp | 4 ---- esphome/components/sha256/sha256.h | 2 -- esphome/core/hash_base.h | 13 +++++++++++++ 5 files changed, 13 insertions(+), 24 deletions(-) diff --git a/esphome/components/md5/md5.cpp b/esphome/components/md5/md5.cpp index ae4f0b108a..8f31e0de61 100644 --- a/esphome/components/md5/md5.cpp +++ b/esphome/components/md5/md5.cpp @@ -39,17 +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); } - -bool MD5Digest::equals_bytes(const uint8_t *expected) { - for (size_t i = 0; i < 16; i++) { - if (expected[i] != this->digest_[i]) { - return false; - } - } - return true; -} - bool MD5Digest::equals_hex(const char *expected) { uint8_t parsed[16]; if (!parse_hex(expected, parsed, 16)) diff --git a/esphome/components/md5/md5.h b/esphome/components/md5/md5.h index 6f1c92cf47..a3a17b95c5 100644 --- a/esphome/components/md5/md5.h +++ b/esphome/components/md5/md5.h @@ -42,16 +42,9 @@ 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); - /// 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); - /// Compare the digest against a provided hex-encoded digest (32 bytes). bool equals_hex(const char *expected); diff --git a/esphome/components/sha256/sha256.cpp b/esphome/components/sha256/sha256.cpp index e59543cfba..5bde76aaa8 100644 --- a/esphome/components/sha256/sha256.cpp +++ b/esphome/components/sha256/sha256.cpp @@ -78,16 +78,12 @@ void SHA256::calculate() { #error "SHA256 not supported on this platform" #endif -void SHA256::get_bytes(uint8_t *output) { memcpy(output, this->digest_, 32); } - std::string SHA256::get_hex_string() { char buf[65]; this->get_hex(buf); return std::string(buf); } -bool SHA256::equals_bytes(const uint8_t *expected) { return memcmp(this->digest_, expected, 32) == 0; } - bool SHA256::equals_hex(const char *expected) { uint8_t parsed[32]; if (!parse_hex(expected, parsed, 32)) { diff --git a/esphome/components/sha256/sha256.h b/esphome/components/sha256/sha256.h index db12daac3f..8b6043729c 100644 --- a/esphome/components/sha256/sha256.h +++ b/esphome/components/sha256/sha256.h @@ -33,13 +33,11 @@ class SHA256 : public esphome::HashBase { void calculate() override; - void get_bytes(uint8_t *output); std::string get_hex_string(); /// Get the size of the hex output (64 for SHA256) size_t get_hex_size() const override { return 64; } - bool equals_bytes(const uint8_t *expected); bool equals_hex(const char *expected); protected: diff --git a/esphome/core/hash_base.h b/esphome/core/hash_base.h index 6a1e821169..346f2c6086 100644 --- a/esphome/core/hash_base.h +++ b/esphome/core/hash_base.h @@ -2,6 +2,7 @@ #include #include +#include #include "esphome/core/helpers.h" namespace esphome { @@ -31,6 +32,18 @@ class HashBase { } } + /// 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); + } + + /// 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; + } + /// Get the size of the hex output (32 for MD5, 64 for SHA256) virtual size_t get_hex_size() const = 0;