1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-23 13:42:27 +01:00

sha256 for host

This commit is contained in:
J. Nick Koston
2025-09-21 10:35:20 -06:00
parent e47cecc5f0
commit 113fe6dfd5
9 changed files with 64 additions and 2 deletions

View File

@@ -1,5 +1,19 @@
import esphome.codegen as cg import esphome.codegen as cg
from esphome.core import CORE, IS_MACOS
CODEOWNERS = ["@esphome/core"] CODEOWNERS = ["@esphome/core"]
sha256_ns = cg.esphome_ns.namespace("sha256") sha256_ns = cg.esphome_ns.namespace("sha256")
async def to_code(config):
# Add OpenSSL library for host platform
if CORE.is_host:
if IS_MACOS:
# macOS needs special handling for Homebrew OpenSSL
cg.add_build_flag("-I/opt/homebrew/opt/openssl/include")
cg.add_build_flag("-L/opt/homebrew/opt/openssl/lib")
cg.add_build_flag("-lcrypto")
else:
# Linux and other Unix systems usually have OpenSSL in standard paths
cg.add_build_flag("-lcrypto")

View File

@@ -1,7 +1,7 @@
#include "sha256.h" #include "sha256.h"
// Only compile SHA256 implementation on platforms that support it // Only compile SHA256 implementation on platforms that support it
#if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_LIBRETINY) #if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_LIBRETINY) || defined(USE_HOST)
#include "esphome/core/helpers.h" #include "esphome/core/helpers.h"
#include <cstring> #include <cstring>
@@ -67,6 +67,35 @@ void SHA256::calculate() {
} }
} }
#elif defined(USE_HOST)
SHA256::~SHA256() = default;
void SHA256::init() {
if (!this->ctx_) {
this->ctx_ = std::make_unique<SHA256Context>();
}
SHA256_Init(&this->ctx_->ctx);
this->ctx_->calculated = false;
}
void SHA256::add(const uint8_t *data, size_t len) {
if (!this->ctx_) {
this->init();
}
SHA256_Update(&this->ctx_->ctx, data, len);
}
void SHA256::calculate() {
if (!this->ctx_) {
this->init();
}
if (!this->ctx_->calculated) {
SHA256_Final(this->ctx_->hash, &this->ctx_->ctx);
this->ctx_->calculated = true;
}
}
#elif defined(USE_ARDUINO) #elif defined(USE_ARDUINO)
SHA256::~SHA256() = default; SHA256::~SHA256() = default;

View File

@@ -3,7 +3,7 @@
#include "esphome/core/defines.h" #include "esphome/core/defines.h"
// Only define SHA256 on platforms that support it // Only define SHA256 on platforms that support it
#if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_LIBRETINY) #if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_LIBRETINY) || defined(USE_HOST)
#include <cstdint> #include <cstdint>
#include <string> #include <string>
@@ -13,6 +13,8 @@
#include "mbedtls/sha256.h" #include "mbedtls/sha256.h"
#elif defined(USE_ESP8266) || defined(USE_RP2040) #elif defined(USE_ESP8266) || defined(USE_RP2040)
#include <bearssl/bearssl_hash.h> #include <bearssl/bearssl_hash.h>
#elif defined(USE_HOST)
#include <openssl/sha.h>
#elif defined(USE_ARDUINO) #elif defined(USE_ARDUINO)
#include <SHA256.h> #include <SHA256.h>
#endif #endif
@@ -50,6 +52,12 @@ class SHA256 {
uint8_t hash[32]; uint8_t hash[32];
bool calculated{false}; bool calculated{false};
}; };
#elif defined(USE_HOST)
struct SHA256Context {
SHA256_CTX ctx;
uint8_t hash[32];
bool calculated{false};
};
#elif defined(USE_ARDUINO) #elif defined(USE_ARDUINO)
struct SHA256Context { struct SHA256Context {
::SHA256 sha; ::SHA256 sha;

View File

@@ -0,0 +1,5 @@
wifi:
ssid: MySSID
password: password1
sha256:

View File

@@ -0,0 +1,2 @@
packages:
common: !include common.yaml

View File

@@ -0,0 +1 @@
<<: !include common.yaml

View File

@@ -0,0 +1 @@
<<: !include common.yaml

View File

@@ -0,0 +1 @@
<<: !include common.yaml

View File

@@ -0,0 +1 @@
<<: !include common.yaml