ESPHome  2024.12.2
md5.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "esphome/core/defines.h"
4 #ifdef USE_MD5
5 
6 #ifdef USE_ESP_IDF
7 #include "esp_rom_md5.h"
8 #define MD5_CTX_TYPE md5_context_t
9 #endif
10 
11 #if defined(USE_ARDUINO) && defined(USE_ESP32)
12 #include "rom/md5_hash.h"
13 #define MD5_CTX_TYPE MD5Context
14 #endif
15 
16 #if defined(USE_ARDUINO) && defined(USE_ESP8266)
17 #include <md5.h>
18 #define MD5_CTX_TYPE md5_context_t
19 #endif
20 
21 #ifdef USE_RP2040
22 #include <MD5Builder.h>
23 #define MD5_CTX_TYPE br_md5_context
24 #endif
25 
26 #if defined(USE_LIBRETINY)
27 #include <MD5.h>
28 #define MD5_CTX_TYPE LT_MD5_CTX_T
29 #endif
30 
31 namespace esphome {
32 namespace md5 {
33 
34 class MD5Digest {
35  public:
36  MD5Digest() = default;
37  ~MD5Digest() = default;
38 
40  void init();
41 
43  void add(const uint8_t *data, size_t len);
44  void add(const char *data, size_t len) { this->add((const uint8_t *) data, len); }
45 
47  void calculate();
48 
51  void get_bytes(uint8_t *output);
52 
55  void get_hex(char *output);
56 
58  bool equals_bytes(const uint8_t *expected);
59 
61  bool equals_hex(const char *expected);
62 
63  protected:
64  MD5_CTX_TYPE ctx_{};
65  uint8_t digest_[16];
66 };
67 
68 } // namespace md5
69 } // namespace esphome
70 #endif
void init()
Initialize a new MD5 digest computation.
Definition: md5.cpp:11
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
void add(const char *data, size_t len)
Definition: md5.h:44
MD5_CTX_TYPE ctx_
Definition: md5.h:64
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_bytes(uint8_t *output)
Retrieve the MD5 digest as bytes.
Definition: md5.cpp:43
void get_hex(char *output)
Retrieve the MD5 digest as hex characters.
Definition: md5.cpp:45
std::string size_t len
Definition: helpers.h:293
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void calculate()
Compute the digest, based on the provided data.
Definition: md5.cpp:18