From ada1b00cad83e84bd78813bd8f1350388dec5aa9 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 21 Sep 2025 10:44:09 -0600 Subject: [PATCH] use evp interface --- esphome/components/sha256/sha256.cpp | 18 ++++++++++++++---- esphome/components/sha256/sha256.h | 4 ++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/esphome/components/sha256/sha256.cpp b/esphome/components/sha256/sha256.cpp index 6fa17bb7c0..f3c0625a5a 100644 --- a/esphome/components/sha256/sha256.cpp +++ b/esphome/components/sha256/sha256.cpp @@ -69,13 +69,22 @@ void SHA256::calculate() { #elif defined(USE_HOST) -SHA256::~SHA256() = default; +SHA256::~SHA256() { + if (this->ctx_ && this->ctx_->ctx) { + EVP_MD_CTX_free(this->ctx_->ctx); + this->ctx_->ctx = nullptr; + } +} void SHA256::init() { if (!this->ctx_) { this->ctx_ = std::make_unique(); } - SHA256_Init(&this->ctx_->ctx); + if (this->ctx_->ctx) { + EVP_MD_CTX_free(this->ctx_->ctx); + } + this->ctx_->ctx = EVP_MD_CTX_new(); + EVP_DigestInit_ex(this->ctx_->ctx, EVP_sha256(), nullptr); this->ctx_->calculated = false; } @@ -83,7 +92,7 @@ void SHA256::add(const uint8_t *data, size_t len) { if (!this->ctx_) { this->init(); } - SHA256_Update(&this->ctx_->ctx, data, len); + EVP_DigestUpdate(this->ctx_->ctx, data, len); } void SHA256::calculate() { @@ -91,7 +100,8 @@ void SHA256::calculate() { this->init(); } if (!this->ctx_->calculated) { - SHA256_Final(this->ctx_->hash, &this->ctx_->ctx); + unsigned int len = 32; + EVP_DigestFinal_ex(this->ctx_->ctx, this->ctx_->hash, &len); this->ctx_->calculated = true; } } diff --git a/esphome/components/sha256/sha256.h b/esphome/components/sha256/sha256.h index 246a7ca891..89b3218166 100644 --- a/esphome/components/sha256/sha256.h +++ b/esphome/components/sha256/sha256.h @@ -14,7 +14,7 @@ #elif defined(USE_ESP8266) || defined(USE_RP2040) #include #elif defined(USE_HOST) -#include +#include #elif defined(USE_ARDUINO) #include #endif @@ -54,7 +54,7 @@ class SHA256 { }; #elif defined(USE_HOST) struct SHA256Context { - SHA256_CTX ctx; + EVP_MD_CTX *ctx{nullptr}; uint8_t hash[32]; bool calculated{false}; };