ESPHome  2023.5.5
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 namespace esphome {
26 namespace md5 {
27 
28 class MD5Digest {
29  public:
30  MD5Digest() = default;
31  ~MD5Digest() = default;
32 
34  void init();
35 
37  void add(const uint8_t *data, size_t len);
38  void add(const char *data, size_t len) { this->add((const uint8_t *) data, len); }
39 
41  void calculate();
42 
45  void get_bytes(uint8_t *output);
46 
49  void get_hex(char *output);
50 
52  bool equals_bytes(const uint8_t *expected);
53 
55  bool equals_hex(const char *expected);
56 
57  protected:
58  MD5_CTX_TYPE ctx_{};
59  uint8_t digest_[16];
60 };
61 
62 } // namespace md5
63 } // 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:38
MD5_CTX_TYPE ctx_
Definition: md5.h:58
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:59
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:286
Definition: a4988.cpp:4
void calculate()
Compute the digest, based on the provided data.
Definition: md5.cpp:17