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:
@@ -1,5 +1,19 @@
|
||||
import esphome.codegen as cg
|
||||
from esphome.core import CORE, IS_MACOS
|
||||
|
||||
CODEOWNERS = ["@esphome/core"]
|
||||
|
||||
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")
|
||||
|
@@ -1,7 +1,7 @@
|
||||
#include "sha256.h"
|
||||
|
||||
// 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 <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)
|
||||
|
||||
SHA256::~SHA256() = default;
|
||||
|
@@ -3,7 +3,7 @@
|
||||
#include "esphome/core/defines.h"
|
||||
|
||||
// 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 <string>
|
||||
@@ -13,6 +13,8 @@
|
||||
#include "mbedtls/sha256.h"
|
||||
#elif defined(USE_ESP8266) || defined(USE_RP2040)
|
||||
#include <bearssl/bearssl_hash.h>
|
||||
#elif defined(USE_HOST)
|
||||
#include <openssl/sha.h>
|
||||
#elif defined(USE_ARDUINO)
|
||||
#include <SHA256.h>
|
||||
#endif
|
||||
@@ -50,6 +52,12 @@ class SHA256 {
|
||||
uint8_t hash[32];
|
||||
bool calculated{false};
|
||||
};
|
||||
#elif defined(USE_HOST)
|
||||
struct SHA256Context {
|
||||
SHA256_CTX ctx;
|
||||
uint8_t hash[32];
|
||||
bool calculated{false};
|
||||
};
|
||||
#elif defined(USE_ARDUINO)
|
||||
struct SHA256Context {
|
||||
::SHA256 sha;
|
||||
|
5
tests/components/sha512/common.yaml
Normal file
5
tests/components/sha512/common.yaml
Normal file
@@ -0,0 +1,5 @@
|
||||
wifi:
|
||||
ssid: MySSID
|
||||
password: password1
|
||||
|
||||
sha256:
|
2
tests/components/sha512/test.bk72xx-ard.yaml
Normal file
2
tests/components/sha512/test.bk72xx-ard.yaml
Normal file
@@ -0,0 +1,2 @@
|
||||
packages:
|
||||
common: !include common.yaml
|
1
tests/components/sha512/test.esp32-idf.yaml
Normal file
1
tests/components/sha512/test.esp32-idf.yaml
Normal file
@@ -0,0 +1 @@
|
||||
<<: !include common.yaml
|
1
tests/components/sha512/test.esp8266-ard.yaml
Normal file
1
tests/components/sha512/test.esp8266-ard.yaml
Normal file
@@ -0,0 +1 @@
|
||||
<<: !include common.yaml
|
1
tests/components/sha512/test.host.yaml
Normal file
1
tests/components/sha512/test.host.yaml
Normal file
@@ -0,0 +1 @@
|
||||
<<: !include common.yaml
|
1
tests/components/sha512/test.rp2040-ard.yaml
Normal file
1
tests/components/sha512/test.rp2040-ard.yaml
Normal file
@@ -0,0 +1 @@
|
||||
<<: !include common.yaml
|
Reference in New Issue
Block a user