ESPHome  2024.4.0
md5.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "esphome/core/defines.h"
4 
5 #ifdef USE_ESP_IDF
6 #include "esp_rom_md5.h"
7 #define MD5_CTX_TYPE md5_context_t
8 #endif
9 
10 #if defined(USE_ARDUINO) && defined(USE_ESP32)
11 #include "rom/md5_hash.h"
12 #define MD5_CTX_TYPE MD5Context
13 #endif
14 
15 #if defined(USE_ARDUINO) && defined(USE_ESP8266)
16 #include <md5.h>
17 #define MD5_CTX_TYPE md5_context_t
18 #endif
19 
20 #ifdef USE_RP2040
21 #include <MD5Builder.h>
22 #define MD5_CTX_TYPE br_md5_context
23 #endif
24 
25 #if defined(USE_LIBRETINY)
26 #include <MD5.h>
27 #define MD5_CTX_TYPE LT_MD5_CTX_T
28 #endif
29 
30 namespace esphome {
31 namespace md5 {
32 
33 class MD5Digest {
34  public:
35  MD5Digest() = default;
36  ~MD5Digest() = default;
37 
39  void init();
40 
42  void add(const uint8_t *data, size_t len);
43  void add(const char *data, size_t len) { this->add((const uint8_t *) data, len); }
44 
46  void calculate();
47 
50  void get_bytes(uint8_t *output);
51 
54  void get_hex(char *output);
55 
57  bool equals_bytes(const uint8_t *expected);
58 
60  bool equals_hex(const char *expected);
61 
62  protected:
63  MD5_CTX_TYPE ctx_{};
64  uint8_t digest_[16];
65 };
66 
67 } // namespace md5
68 } // namespace esphome
void init()
Initialize a new MD5 digest computation.
Definition: md5.cpp:10
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
void add(const char *data, size_t len)
Definition: md5.h:43
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