Skip to content

Commit 129c1cd

Browse files
committed
feat(hash): Add hashing library and SHA3 algorithm
1 parent 4eff7f9 commit 129c1cd

File tree

20 files changed

+751
-68
lines changed

20 files changed

+751
-68
lines changed

CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ set(CORE_SRCS
5454
cores/esp32/freertos_stats.cpp
5555
cores/esp32/FunctionalInterrupt.cpp
5656
cores/esp32/HardwareSerial.cpp
57-
cores/esp32/HEXBuilder.cpp
57+
cores/esp32/HashBuilder.cpp
58+
cores/esp32/HexBuilder.cpp
5859
cores/esp32/IPAddress.cpp
5960
cores/esp32/libb64/cdecode.c
6061
cores/esp32/libb64/cencode.c
6162
cores/esp32/MacAddress.cpp
6263
cores/esp32/main.cpp
6364
cores/esp32/MD5Builder.cpp
6465
cores/esp32/Print.cpp
65-
cores/esp32/SHA1Builder.cpp
6666
cores/esp32/stdlib_noniso.c
6767
cores/esp32/Stream.cpp
6868
cores/esp32/StreamString.cpp
@@ -93,6 +93,7 @@ set(ARDUINO_ALL_LIBRARIES
9393
Ethernet
9494
FFat
9595
FS
96+
Hash
9697
HTTPClient
9798
HTTPUpdate
9899
Insights
@@ -154,6 +155,11 @@ set(ARDUINO_LIBRARY_FS_SRCS
154155
libraries/FS/src/FS.cpp
155156
libraries/FS/src/vfs_api.cpp)
156157

158+
set(ARDUINO_LIBRARY_Hash_SRCS
159+
libraries/Hash/src/SHA1Builder.cpp
160+
libraries/Hash/src/SHA3Builder.cpp
161+
)
162+
157163
set(ARDUINO_LIBRARY_HTTPClient_SRCS libraries/HTTPClient/src/HTTPClient.cpp)
158164

159165
set(ARDUINO_LIBRARY_HTTPUpdate_SRCS libraries/HTTPUpdate/src/HTTPUpdate.cpp)

cores/esp32/HashBuilder.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2025 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "HashBuilder.h"
16+
17+
void HashBuilder::add(const char *data) {
18+
add((const uint8_t *)data, strlen(data));
19+
}
20+
21+
void HashBuilder::add(String data) {
22+
add(data.c_str());
23+
}
24+
25+
void HashBuilder::addHexString(const char *data) {
26+
size_t len = strlen(data);
27+
uint8_t *tmp = (uint8_t *)malloc(len / 2);
28+
if (tmp == NULL) {
29+
return;
30+
}
31+
hex2bytes(tmp, len / 2, data);
32+
add(tmp, len / 2);
33+
free(tmp);
34+
}
35+
36+
void HashBuilder::addHexString(String data) {
37+
addHexString(data.c_str());
38+
}

cores/esp32/HashBuilder.h

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,21 @@
1818
#include <WString.h>
1919
#include <Stream.h>
2020

21-
#include "HEXBuilder.h"
21+
#include "HexBuilder.h"
2222

23-
class HashBuilder : public HEXBuilder {
23+
// Base class for hash builders
24+
25+
class HashBuilder : public HexBuilder {
2426
public:
2527
virtual ~HashBuilder() {}
2628
virtual void begin() = 0;
2729

2830
virtual void add(const uint8_t *data, size_t len) = 0;
29-
virtual void add(const char *data) {
30-
add((const uint8_t *)data, strlen(data));
31-
}
32-
virtual void add(String data) {
33-
add(data.c_str());
34-
}
35-
36-
virtual void addHexString(const char *data) = 0;
37-
virtual void addHexString(String data) {
38-
addHexString(data.c_str());
39-
}
31+
void add(const char *data);
32+
void add(String data);
33+
34+
void addHexString(const char *data);
35+
void addHexString(String data);
4036

4137
virtual bool addStream(Stream &stream, const size_t maxLen) = 0;
4238
virtual void calculate() = 0;

cores/esp32/HEXBuilder.cpp renamed to cores/esp32/HexBuilder.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1818
*/
1919

20-
#include <Arduino.h>
21-
#include <HEXBuilder.h>
20+
#include "HexBuilder.h"
2221

2322
static uint8_t hex_char_to_byte(uint8_t c) {
2423
return (c >= 'a' && c <= 'f') ? (c - ((uint8_t)'a' - 0xa))
@@ -27,11 +26,11 @@ static uint8_t hex_char_to_byte(uint8_t c) {
2726
: 0x10; // unknown char is 16
2827
}
2928

30-
size_t HEXBuilder::hex2bytes(unsigned char *out, size_t maxlen, String &in) {
29+
size_t HexBuilder::hex2bytes(unsigned char *out, size_t maxlen, String &in) {
3130
return hex2bytes(out, maxlen, in.c_str());
3231
}
3332

34-
size_t HEXBuilder::hex2bytes(unsigned char *out, size_t maxlen, const char *in) {
33+
size_t HexBuilder::hex2bytes(unsigned char *out, size_t maxlen, const char *in) {
3534
size_t len = 0;
3635
for (; *in; in++) {
3736
uint8_t c = hex_char_to_byte(*in);
@@ -54,7 +53,7 @@ size_t HEXBuilder::hex2bytes(unsigned char *out, size_t maxlen, const char *in)
5453
return (len + 1) / 2;
5554
}
5655

57-
size_t HEXBuilder::bytes2hex(char *out, size_t maxlen, const unsigned char *in, size_t len) {
56+
size_t HexBuilder::bytes2hex(char *out, size_t maxlen, const unsigned char *in, size_t len) {
5857
for (size_t i = 0; i < len; i++) {
5958
if (i * 2 + 1 < maxlen) {
6059
sprintf(out + (i * 2), "%02x", in[i]);
@@ -63,7 +62,7 @@ size_t HEXBuilder::bytes2hex(char *out, size_t maxlen, const unsigned char *in,
6362
return len * 2 + 1;
6463
}
6564

66-
String HEXBuilder::bytes2hex(const unsigned char *in, size_t len) {
65+
String HexBuilder::bytes2hex(const unsigned char *in, size_t len) {
6766
size_t maxlen = len * 2 + 1;
6867
char *out = (char *)malloc(maxlen);
6968
if (!out) {

cores/esp32/HEXBuilder.h renamed to cores/esp32/HexBuilder.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1818
*/
1919

20-
#ifndef HEXBuilder_h
21-
#define HEXBuilder_h
20+
#ifndef HexBuilder_h
21+
#define HexBuilder_h
2222

2323
#include <WString.h>
2424
#include <Stream.h>
2525

26-
class HEXBuilder {
26+
// Basic hex/byte conversion class to be used by hash builders
27+
28+
class HexBuilder {
2729
public:
2830
static size_t hex2bytes(unsigned char *out, size_t maxlen, String &in);
2931
static size_t hex2bytes(unsigned char *out, size_t maxlen, const char *in);

cores/esp32/MD5Builder.cpp

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1818
*/
1919

20-
#include <Arduino.h>
21-
#include <HEXBuilder.h>
22-
#include <MD5Builder.h>
20+
#include "HexBuilder.h"
21+
#include "MD5Builder.h"
2322

2423
void MD5Builder::begin(void) {
2524
memset(_buf, 0x00, ESP_ROM_MD5_DIGEST_LEN);
@@ -30,17 +29,6 @@ void MD5Builder::add(const uint8_t *data, size_t len) {
3029
esp_rom_md5_update(&_ctx, data, len);
3130
}
3231

33-
void MD5Builder::addHexString(const char *data) {
34-
size_t len = strlen(data);
35-
uint8_t *tmp = (uint8_t *)malloc(len / 2);
36-
if (tmp == NULL) {
37-
return;
38-
}
39-
hex2bytes(tmp, len / 2, data);
40-
add(tmp, len / 2);
41-
free(tmp);
42-
}
43-
4432
bool MD5Builder::addStream(Stream &stream, const size_t maxLen) {
4533
const int buf_size = 512;
4634
int maxLengthLeft = maxLen;

cores/esp32/MD5Builder.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,10 @@ class MD5Builder : public HashBuilder {
3535
uint8_t _buf[ESP_ROM_MD5_DIGEST_LEN];
3636

3737
public:
38-
void begin(void) override;
39-
4038
using HashBuilder::add;
41-
void add(const uint8_t *data, size_t len) override;
42-
43-
using HashBuilder::addHexString;
44-
void addHexString(const char *data) override;
4539

40+
void begin(void) override;
41+
void add(const uint8_t *data, size_t len) override;
4642
bool addStream(Stream &stream, const size_t maxLen) override;
4743
void calculate(void) override;
4844
void getBytes(uint8_t *output) override;

libraries/ESP32/examples/Utilities/HEXBuilder/HEXBuilder.ino renamed to libraries/Hash/examples/Hex/Hex.ino

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
#include <HEXBuilder.h>
1+
/*
2+
Usage example for the HexBuilder class.
3+
4+
This example shows how to convert a HEX string to a binary buffer and vice versa.
5+
*/
6+
7+
#include <HexBuilder.h>
28

39
void setup() {
410
Serial.begin(115200);
@@ -11,7 +17,7 @@ void setup() {
1117
const char *hexin = "48656c6c6f20576f726c6400"; // As the string above is \0 terminated too
1218

1319
unsigned char buff[256];
14-
size_t len = HEXBuilder::hex2bytes(buff, sizeof(buff), hexin);
20+
size_t len = HexBuilder::hex2bytes(buff, sizeof(buff), hexin);
1521

1622
if (len != 1 + strlen(out)) {
1723
Serial.println("Odd - length 1 is wrong");
@@ -30,7 +36,7 @@ void setup() {
3036
const char hello[] = "Hello World";
3137

3238
unsigned char buff[256];
33-
size_t len = HEXBuilder::hex2bytes(buff, sizeof(buff), helloHEX);
39+
size_t len = HexBuilder::hex2bytes(buff, sizeof(buff), helloHEX);
3440

3541
if (len != strlen(hello)) {
3642
Serial.println("Odd - length 2 is wrong");
@@ -45,7 +51,7 @@ void setup() {
4551
const unsigned char helloBytes[] = {0x48, 0x56, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64};
4652
String helloHEX = "48566c6c6f20576f726c64";
4753

48-
String out = HEXBuilder::bytes2hex(helloBytes, sizeof(helloBytes));
54+
String out = HexBuilder::bytes2hex(helloBytes, sizeof(helloBytes));
4955
if (out.length() != 2 * sizeof(helloBytes)) {
5056
Serial.println("Odd - length 3 is wrong");
5157
}
@@ -61,7 +67,7 @@ void setup() {
6167
const char helloHex[] = "6c6c6f20576f726c64";
6268

6369
char buff[256];
64-
size_t len = HEXBuilder::bytes2hex(buff, sizeof(buff), helloBytes, sizeof(helloBytes));
70+
size_t len = HexBuilder::bytes2hex(buff, sizeof(buff), helloBytes, sizeof(helloBytes));
6571
if (len != 1 + 2 * sizeof(helloBytes)) {
6672
Serial.println("Odd - length 4 is wrong");
6773
}

0 commit comments

Comments
 (0)