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