ESPHome 2025.5.0
Loading...
Searching...
No Matches
hmac_md5.cpp
Go to the documentation of this file.
1#include <cstdio>
2#include <cstring>
3#include "hmac_md5.h"
4#ifdef USE_MD5
6
7namespace esphome {
8namespace hmac_md5 {
9void HmacMD5::init(const uint8_t *key, size_t len) {
10 uint8_t ipad[64], opad[64];
11
12 memset(ipad, 0, sizeof(ipad));
13 if (len > 64) {
14 md5::MD5Digest keymd5;
15 keymd5.init();
16 keymd5.add(key, len);
17 keymd5.calculate();
18 keymd5.get_bytes(ipad);
19 } else {
20 memcpy(ipad, key, len);
21 }
22 memcpy(opad, ipad, sizeof(opad));
23
24 for (int i = 0; i < 64; i++) {
25 ipad[i] ^= 0x36;
26 opad[i] ^= 0x5c;
27 }
28
29 this->ihash_.init();
30 this->ihash_.add(ipad, sizeof(ipad));
31
32 this->ohash_.init();
33 this->ohash_.add(opad, sizeof(opad));
34}
35
36void HmacMD5::add(const uint8_t *data, size_t len) { this->ihash_.add(data, len); }
37
39 uint8_t ibytes[16];
40
41 this->ihash_.calculate();
42 this->ihash_.get_bytes(ibytes);
43
44 this->ohash_.add(ibytes, sizeof(ibytes));
45 this->ohash_.calculate();
46}
47
48void HmacMD5::get_bytes(uint8_t *output) { this->ohash_.get_bytes(output); }
49
50void HmacMD5::get_hex(char *output) { this->ohash_.get_hex(output); }
51
52bool HmacMD5::equals_bytes(const uint8_t *expected) { return this->ohash_.equals_bytes(expected); }
53
54bool HmacMD5::equals_hex(const char *expected) { return this->ohash_.equals_hex(expected); }
55
56} // namespace hmac_md5
57} // namespace esphome
58#endif
bool equals_hex(const char *expected)
Compare the digest against a provided hex-encoded digest (32 bytes).
Definition hmac_md5.cpp:54
void init(const uint8_t *key, size_t len)
Initialize a new MD5 digest computation.
Definition hmac_md5.cpp:9
void calculate()
Compute the digest, based on the provided data.
Definition hmac_md5.cpp:38
md5::MD5Digest ihash_
Definition hmac_md5.h:43
md5::MD5Digest ohash_
Definition hmac_md5.h:44
void get_hex(char *output)
Retrieve the HMAC-MD5 digest as hex characters.
Definition hmac_md5.cpp:50
void add(const uint8_t *data, size_t len)
Add bytes of data for the digest.
Definition hmac_md5.cpp:36
bool equals_bytes(const uint8_t *expected)
Compare the digest against a provided byte-encoded digest (16 bytes).
Definition hmac_md5.cpp:52
void get_bytes(uint8_t *output)
Retrieve the HMAC-MD5 digest as bytes.
Definition hmac_md5.cpp:48
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
void get_hex(char *output)
Retrieve the MD5 digest as hex characters.
Definition md5.cpp:45
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