ESPHome  2024.10.2
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
5 #include "esphome/core/helpers.h"
6 
7 namespace esphome {
8 namespace hmac_md5 {
9 void 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 
36 void 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 
48 void HmacMD5::get_bytes(uint8_t *output) { this->ohash_.get_bytes(output); }
49 
50 void HmacMD5::get_hex(char *output) { this->ohash_.get_hex(output); }
51 
52 bool HmacMD5::equals_bytes(const uint8_t *expected) { return this->ohash_.equals_bytes(expected); }
53 
54 bool HmacMD5::equals_hex(const char *expected) { return this->ohash_.equals_hex(expected); }
55 
56 } // namespace hmac_md5
57 } // namespace esphome
58 #endif
void init()
Initialize a new MD5 digest computation.
Definition: md5.cpp:11
md5::MD5Digest ohash_
Definition: hmac_md5.h:44
void init(const uint8_t *key, size_t len)
Initialize a new MD5 digest computation.
Definition: hmac_md5.cpp:9
bool equals_hex(const char *expected)
Compare the digest against a provided hex-encoded digest (32 bytes).
Definition: md5.cpp:60
void get_bytes(uint8_t *output)
Retrieve the HMAC-MD5 digest as bytes.
Definition: hmac_md5.cpp:48
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: hmac_md5.cpp:52
bool equals_hex(const char *expected)
Compare the digest against a provided hex-encoded digest (32 bytes).
Definition: hmac_md5.cpp:54
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: md5.cpp:51
md5::MD5Digest ihash_
Definition: hmac_md5.h:43
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: hmac_md5.cpp:38
void get_hex(char *output)
Retrieve the MD5 digest as hex characters.
Definition: md5.cpp:45
std::string size_t len
Definition: helpers.h:292
void get_hex(char *output)
Retrieve the HMAC-MD5 digest as hex characters.
Definition: hmac_md5.cpp:50
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void calculate()
Compute the digest, based on the provided data.
Definition: md5.cpp:18