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