ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
hash_base.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <cstddef>
5#include <cstring>
7
8namespace esphome {
9
11class HashBase {
12 public:
13 virtual ~HashBase() = default;
14
16 virtual void init() = 0;
17
19 virtual void add(const uint8_t *data, size_t len) = 0;
20 void add(const char *data, size_t len) { this->add((const uint8_t *) data, len); }
21
23 virtual void calculate() = 0;
24
26 void get_bytes(uint8_t *output) { memcpy(output, this->digest_, this->get_size()); }
27
29 void get_hex(char *output) { format_hex_to(output, this->get_size() * 2 + 1, this->digest_, this->get_size()); }
30
32 bool equals_bytes(const uint8_t *expected) { return constant_time_equals(this->digest_, expected, this->get_size()); }
33
36 bool equals_hex(const char *expected) {
37 uint8_t parsed[32]; // Fixed size for max hash (SHA256 = 32 bytes)
38 const size_t size = this->get_size();
39 if (parse_hex(expected, size * 2, parsed, size) != size * 2) {
40 return false;
41 }
42 return this->equals_bytes(parsed);
43 }
44
46 virtual size_t get_size() const = 0;
47
48 protected:
49// ESP32 variants with DMA-based hardware SHA (all except original ESP32) require 32-byte aligned buffers.
50// Original ESP32 uses a different hardware SHA implementation without DMA alignment requirements.
51// Other platforms (ESP8266, RP2040, LibreTiny) use software SHA and don't need alignment.
52// Storage sized for max(MD5=16, SHA256=32) bytes
53#if defined(USE_ESP32) && !defined(USE_ESP32_VARIANT_ESP32)
54 alignas(32) uint8_t digest_[32];
55#else
56 uint8_t digest_[32];
57#endif
58};
59
60} // namespace esphome
Base class for hash algorithms.
Definition hash_base.h:11
void get_hex(char *output)
Retrieve the hash as hex characters. Output buffer must hold get_size() * 2 + 1 bytes.
Definition hash_base.h:29
virtual ~HashBase()=default
bool equals_hex(const char *expected)
Compare the hash against a provided hex-encoded hash of get_size() * 2 characters,...
Definition hash_base.h:36
virtual void calculate()=0
Compute the hash based on provided data.
virtual void init()=0
Initialize a new hash computation.
bool equals_bytes(const uint8_t *expected)
Compare the hash against a provided byte-encoded hash.
Definition hash_base.h:32
virtual size_t get_size() const =0
Get the size of the hash in bytes (16 for MD5, 32 for SHA256)
void add(const char *data, size_t len)
Definition hash_base.h:20
virtual void add(const uint8_t *data, size_t len)=0
Add bytes of data for the hash.
uint8_t digest_[32]
Definition hash_base.h:54
void get_bytes(uint8_t *output)
Retrieve the hash as bytes.
Definition hash_base.h:26
const void size_t len
Definition hal.h:68
size_t parse_hex(const char *str, size_t length, uint8_t *data, size_t count)
Parse bytes from a hex-encoded string into a byte array.
Definition helpers.cpp:370
uint16_t size
Definition helpers.cpp:34
bool constant_time_equals(const uint8_t *a, const uint8_t *b, size_t len)
Compare len bytes of a and b in a time that does not depend on where they first differ.
Definition helpers.h:1117
char * format_hex_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length)
Format byte array as lowercase hex to buffer (base implementation).
Definition helpers.cpp:428