mirror of
https://github.com/esphome/esphome.git
synced 2025-09-29 16:42:19 +01:00
Add sha256 support
This is a breakout from https://github.com/esphome/esphome/pull/10809
This commit is contained in:
@@ -39,32 +39,6 @@ void MD5Digest::add(const uint8_t *data, size_t len) { br_md5_update(&this->ctx_
|
|||||||
void MD5Digest::calculate() { br_md5_out(&this->ctx_, this->digest_); }
|
void MD5Digest::calculate() { br_md5_out(&this->ctx_, this->digest_); }
|
||||||
#endif // USE_RP2040
|
#endif // USE_RP2040
|
||||||
|
|
||||||
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]) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool MD5Digest::equals_hex(const char *expected) {
|
|
||||||
uint8_t parsed[16];
|
|
||||||
if (!parse_hex(expected, parsed, 16))
|
|
||||||
return false;
|
|
||||||
return equals_bytes(parsed);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace md5
|
} // namespace md5
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
#endif
|
#endif
|
||||||
|
@@ -3,6 +3,8 @@
|
|||||||
#include "esphome/core/defines.h"
|
#include "esphome/core/defines.h"
|
||||||
#ifdef USE_MD5
|
#ifdef USE_MD5
|
||||||
|
|
||||||
|
#include "esphome/core/hash_base.h"
|
||||||
|
|
||||||
#ifdef USE_ESP32
|
#ifdef USE_ESP32
|
||||||
#include "esp_rom_md5.h"
|
#include "esp_rom_md5.h"
|
||||||
#define MD5_CTX_TYPE md5_context_t
|
#define MD5_CTX_TYPE md5_context_t
|
||||||
@@ -26,38 +28,26 @@
|
|||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace md5 {
|
namespace md5 {
|
||||||
|
|
||||||
class MD5Digest {
|
class MD5Digest : public HashBase {
|
||||||
public:
|
public:
|
||||||
MD5Digest() = default;
|
MD5Digest() = default;
|
||||||
~MD5Digest() = default;
|
~MD5Digest() override = default;
|
||||||
|
|
||||||
/// Initialize a new MD5 digest computation.
|
/// Initialize a new MD5 digest computation.
|
||||||
void init();
|
void init() override;
|
||||||
|
|
||||||
/// Add bytes of data for the digest.
|
/// Add bytes of data for the digest.
|
||||||
void add(const uint8_t *data, size_t len);
|
void add(const uint8_t *data, size_t len) override;
|
||||||
void add(const char *data, size_t len) { this->add((const uint8_t *) data, len); }
|
using HashBase::add; // Bring base class overload into scope
|
||||||
|
|
||||||
/// Compute the digest, based on the provided data.
|
/// Compute the digest, based on the provided data.
|
||||||
void calculate();
|
void calculate() override;
|
||||||
|
|
||||||
/// Retrieve the MD5 digest as bytes.
|
/// Get the size of the hash in bytes (16 for MD5)
|
||||||
/// The output must be able to hold 16 bytes or more.
|
size_t get_size() const override { return 16; }
|
||||||
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);
|
|
||||||
|
|
||||||
/// Compare the digest against a provided byte-encoded digest (16 bytes).
|
|
||||||
bool equals_bytes(const uint8_t *expected);
|
|
||||||
|
|
||||||
/// Compare the digest against a provided hex-encoded digest (32 bytes).
|
|
||||||
bool equals_hex(const char *expected);
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
MD5_CTX_TYPE ctx_{};
|
MD5_CTX_TYPE ctx_{};
|
||||||
uint8_t digest_[16];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace md5
|
} // namespace md5
|
||||||
|
Reference in New Issue
Block a user