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