mirror of
https://github.com/esphome/esphome.git
synced 2025-10-29 22:24:26 +00:00
preen
This commit is contained in:
@@ -41,14 +41,6 @@ void MD5Digest::calculate() { br_md5_out(&this->ctx_, this->digest_); }
|
||||
|
||||
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) {
|
||||
for (size_t i = 0; i < 16; i++) {
|
||||
if (expected[i] != this->digest_[i]) {
|
||||
|
||||
@@ -46,10 +46,6 @@ class MD5Digest : public HashBase {
|
||||
/// 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; }
|
||||
|
||||
@@ -61,7 +57,6 @@ class MD5Digest : public HashBase {
|
||||
|
||||
protected:
|
||||
MD5_CTX_TYPE ctx_{};
|
||||
uint8_t digest_[16];
|
||||
};
|
||||
|
||||
} // namespace md5
|
||||
|
||||
@@ -19,7 +19,7 @@ void SHA256::init() {
|
||||
|
||||
void SHA256::add(const uint8_t *data, size_t len) { mbedtls_sha256_update(&this->ctx_, data, len); }
|
||||
|
||||
void SHA256::calculate() { mbedtls_sha256_finish(&this->ctx_, this->hash_); }
|
||||
void SHA256::calculate() { mbedtls_sha256_finish(&this->ctx_, this->digest_); }
|
||||
|
||||
#elif defined(USE_ESP8266) || defined(USE_RP2040)
|
||||
|
||||
@@ -34,7 +34,7 @@ void SHA256::add(const uint8_t *data, size_t len) { br_sha256_update(&this->ctx_
|
||||
|
||||
void SHA256::calculate() {
|
||||
if (!this->calculated_) {
|
||||
br_sha256_out(&this->ctx_, this->hash_);
|
||||
br_sha256_out(&this->ctx_, this->digest_);
|
||||
this->calculated_ = true;
|
||||
}
|
||||
}
|
||||
@@ -69,7 +69,7 @@ void SHA256::calculate() {
|
||||
}
|
||||
if (!this->calculated_) {
|
||||
unsigned int len = 32;
|
||||
EVP_DigestFinal_ex(this->ctx_, this->hash_, &len);
|
||||
EVP_DigestFinal_ex(this->ctx_, this->digest_, &len);
|
||||
this->calculated_ = true;
|
||||
}
|
||||
}
|
||||
@@ -78,15 +78,7 @@ void SHA256::calculate() {
|
||||
#error "SHA256 not supported on this platform"
|
||||
#endif
|
||||
|
||||
void SHA256::get_bytes(uint8_t *output) { memcpy(output, this->hash_, 32); }
|
||||
|
||||
void SHA256::get_hex(char *output) {
|
||||
for (size_t i = 0; i < 32; i++) {
|
||||
uint8_t byte = this->hash_[i];
|
||||
output[i * 2] = format_hex_char(byte >> 4);
|
||||
output[i * 2 + 1] = format_hex_char(byte & 0x0F);
|
||||
}
|
||||
}
|
||||
void SHA256::get_bytes(uint8_t *output) { memcpy(output, this->digest_, 32); }
|
||||
|
||||
std::string SHA256::get_hex_string() {
|
||||
char buf[65];
|
||||
@@ -94,7 +86,7 @@ std::string SHA256::get_hex_string() {
|
||||
return std::string(buf);
|
||||
}
|
||||
|
||||
bool SHA256::equals_bytes(const uint8_t *expected) { return memcmp(this->hash_, expected, 32) == 0; }
|
||||
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];
|
||||
|
||||
@@ -34,7 +34,6 @@ 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)
|
||||
@@ -46,14 +45,11 @@ class SHA256 : public esphome::HashBase {
|
||||
protected:
|
||||
#if defined(USE_ESP32) || defined(USE_LIBRETINY)
|
||||
mbedtls_sha256_context ctx_{};
|
||||
uint8_t hash_[32];
|
||||
#elif defined(USE_ESP8266) || defined(USE_RP2040)
|
||||
br_sha256_context ctx_{};
|
||||
uint8_t hash_[32];
|
||||
bool calculated_{false};
|
||||
#elif defined(USE_HOST)
|
||||
EVP_MD_CTX *ctx_{nullptr};
|
||||
uint8_t hash_[32];
|
||||
bool calculated_{false};
|
||||
#else
|
||||
#error "SHA256 not supported on this platform"
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
namespace esphome {
|
||||
|
||||
@@ -21,10 +22,20 @@ class HashBase {
|
||||
virtual void calculate() = 0;
|
||||
|
||||
/// Retrieve the hash as hex characters
|
||||
virtual void get_hex(char *output) = 0;
|
||||
virtual 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);
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 (MD5 uses 16 bytes, SHA256 uses 32)
|
||||
};
|
||||
|
||||
} // namespace esphome
|
||||
|
||||
Reference in New Issue
Block a user