Skip to content

Commit 794de50

Browse files
authored
Migrate MD5Builder to use esp_rom functions directly rather than rely on wrappers that can be omitted from compilation based on sdkconfig. (espressif#5941)
1 parent 5e7db8d commit 794de50

File tree

2 files changed

+12
-24
lines changed

2 files changed

+12
-24
lines changed

Diff for: cores/esp32/MD5Builder.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include <Arduino.h>
2020
#include <MD5Builder.h>
2121

22-
uint8_t hex_char_to_byte(uint8_t c)
22+
static uint8_t hex_char_to_byte(uint8_t c)
2323
{
2424
return (c >= 'a' && c <= 'f') ? (c - ((uint8_t)'a' - 0xa)) :
2525
(c >= 'A' && c <= 'F') ? (c - ((uint8_t)'A' - 0xA)) :
@@ -28,13 +28,13 @@ uint8_t hex_char_to_byte(uint8_t c)
2828

2929
void MD5Builder::begin(void)
3030
{
31-
memset(_buf, 0x00, 16);
32-
MD5Init(&_ctx);
31+
memset(_buf, 0x00, ESP_ROM_MD5_DIGEST_LEN);
32+
esp_rom_md5_init(&_ctx);
3333
}
3434

3535
void MD5Builder::add(uint8_t * data, uint16_t len)
3636
{
37-
MD5Update(&_ctx, data, len);
37+
esp_rom_md5_update(&_ctx, data, len);
3838
}
3939

4040
void MD5Builder::addHexString(const char * data)
@@ -82,7 +82,7 @@ bool MD5Builder::addStream(Stream & stream, const size_t maxLen)
8282
}
8383

8484
// Update MD5 with buffer payload
85-
MD5Update(&_ctx, buf, numBytesRead);
85+
esp_rom_md5_update(&_ctx, buf, numBytesRead);
8686

8787
// update available number of bytes
8888
maxLengthLeft -= numBytesRead;
@@ -94,24 +94,24 @@ bool MD5Builder::addStream(Stream & stream, const size_t maxLen)
9494

9595
void MD5Builder::calculate(void)
9696
{
97-
MD5Final(_buf, &_ctx);
97+
esp_rom_md5_final(_buf, &_ctx);
9898
}
9999

100100
void MD5Builder::getBytes(uint8_t * output)
101101
{
102-
memcpy(output, _buf, 16);
102+
memcpy(output, _buf, ESP_ROM_MD5_DIGEST_LEN);
103103
}
104104

105105
void MD5Builder::getChars(char * output)
106106
{
107-
for(uint8_t i = 0; i < 16; i++) {
107+
for(uint8_t i = 0; i < ESP_ROM_MD5_DIGEST_LEN; i++) {
108108
sprintf(output + (i * 2), "%02x", _buf[i]);
109109
}
110110
}
111111

112112
String MD5Builder::toString(void)
113113
{
114-
char out[33];
114+
char out[(ESP_ROM_MD5_DIGEST_LEN * 2) + 1];
115115
getChars(out);
116116
return String(out);
117117
}

Diff for: cores/esp32/MD5Builder.h

+3-15
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,13 @@
2323
#include <Stream.h>
2424

2525
#include "esp_system.h"
26-
#ifdef ESP_IDF_VERSION_MAJOR // IDF 4+
27-
#if CONFIG_IDF_TARGET_ESP32 // ESP32/PICO-D4
28-
#include "esp32/rom/md5_hash.h"
29-
#elif CONFIG_IDF_TARGET_ESP32S2
30-
#include "esp32s2/rom/md5_hash.h"
31-
#elif CONFIG_IDF_TARGET_ESP32C3
32-
#include "esp32c3/rom/md5_hash.h"
33-
#else
34-
#error Target CONFIG_IDF_TARGET is not supported
35-
#endif
36-
#else // ESP32 Before IDF 4.0
37-
#include "rom/md5_hash.h"
38-
#endif
26+
#include "esp_rom_md5.h"
3927

4028
class MD5Builder
4129
{
4230
private:
43-
struct MD5Context _ctx;
44-
uint8_t _buf[16];
31+
md5_context_t _ctx;
32+
uint8_t _buf[ESP_ROM_MD5_DIGEST_LEN];
4533
public:
4634
void begin(void);
4735
void add(uint8_t * data, uint16_t len);

0 commit comments

Comments
 (0)