ESPHome 2025.9.3
Loading...
Searching...
No Matches
md5.cpp
Go to the documentation of this file.
1#include <cstring>
2#include "md5.h"
3#ifdef USE_MD5
5
6namespace esphome {
7namespace md5 {
8
9#if defined(USE_ARDUINO) && !defined(USE_RP2040) && !defined(USE_ESP32)
11 memset(this->digest_, 0, 16);
12 MD5Init(&this->ctx_);
13}
14
15void MD5Digest::add(const uint8_t *data, size_t len) { MD5Update(&this->ctx_, data, len); }
16
17void MD5Digest::calculate() { MD5Final(this->digest_, &this->ctx_); }
18#endif // USE_ARDUINO && !USE_RP2040
19
20#ifdef USE_ESP32
21void MD5Digest::init() {
22 memset(this->digest_, 0, 16);
23 esp_rom_md5_init(&this->ctx_);
24}
25
26void MD5Digest::add(const uint8_t *data, size_t len) { esp_rom_md5_update(&this->ctx_, data, len); }
27
28void MD5Digest::calculate() { esp_rom_md5_final(this->digest_, &this->ctx_); }
29#endif // USE_ESP32
30
31#ifdef USE_RP2040
32void MD5Digest::init() {
33 memset(this->digest_, 0, 16);
34 br_md5_init(&this->ctx_);
35}
36
37void MD5Digest::add(const uint8_t *data, size_t len) { br_md5_update(&this->ctx_, data, len); }
38
39void MD5Digest::calculate() { br_md5_out(&this->ctx_, this->digest_); }
40#endif // USE_RP2040
41
42void MD5Digest::get_bytes(uint8_t *output) { memcpy(output, this->digest_, 16); }
43
44void MD5Digest::get_hex(char *output) {
45 for (size_t i = 0; i < 16; i++) {
46 uint8_t byte = this->digest_[i];
47 output[i * 2] = format_hex_char(byte >> 4);
48 output[i * 2 + 1] = format_hex_char(byte & 0x0F);
49 }
50}
51
52bool MD5Digest::equals_bytes(const uint8_t *expected) {
53 for (size_t i = 0; i < 16; i++) {
54 if (expected[i] != this->digest_[i]) {
55 return false;
56 }
57 }
58 return true;
59}
60
61bool MD5Digest::equals_hex(const char *expected) {
62 uint8_t parsed[16];
63 if (!parse_hex(expected, parsed, 16))
64 return false;
65 return equals_bytes(parsed);
66}
67
68} // namespace md5
69} // namespace esphome
70#endif
bool equals_hex(const char *expected)
Compare the digest against a provided hex-encoded digest (32 bytes).
Definition md5.cpp:61
void add(const uint8_t *data, size_t len)
Add bytes of data for the digest.
Definition md5.cpp:15
bool equals_bytes(const uint8_t *expected)
Compare the digest against a provided byte-encoded digest (16 bytes).
Definition md5.cpp:52
uint8_t digest_[16]
Definition md5.h:60
void get_hex(char *output)
Retrieve the MD5 digest as hex characters.
Definition md5.cpp:44
MD5_CTX_TYPE ctx_
Definition md5.h:59
void init()
Initialize a new MD5 digest computation.
Definition md5.cpp:10
void get_bytes(uint8_t *output)
Retrieve the MD5 digest as bytes.
Definition md5.cpp:42
void calculate()
Compute the digest, based on the provided data.
Definition md5.cpp:17
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:280
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:239
char format_hex_char(uint8_t v)
Convert a nibble (0-15) to lowercase hex char.
Definition helpers.h:384