Skip to content

Commit 289f59d

Browse files
committed
Add HashBuilder abstract class
1 parent dc1a920 commit 289f59d

File tree

3 files changed

+85
-38
lines changed

3 files changed

+85
-38
lines changed

Diff for: cores/esp32/HashBuilder.h

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2024 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+
#ifndef HashBuilder_h
16+
#define HashBuilder_h
17+
18+
#include <WString.h>
19+
#include <Stream.h>
20+
21+
#include "HEXBuilder.h"
22+
23+
class HashBuilder : public HEXBuilder
24+
{
25+
public:
26+
virtual ~HashBuilder() {}
27+
virtual void begin() = 0;
28+
29+
virtual void add(uint8_t* data, size_t len) = 0;
30+
virtual void add(const char* data)
31+
{
32+
add((uint8_t*)data, strlen(data));
33+
}
34+
virtual void add(char* data)
35+
{
36+
add((const char*)data);
37+
}
38+
virtual void add(String data)
39+
{
40+
add(data.c_str());
41+
}
42+
43+
virtual void addHexString(const char* data) = 0;
44+
virtual void addHexString(char* data)
45+
{
46+
addHexString((const char*)data);
47+
}
48+
virtual void addHexString(String data)
49+
{
50+
addHexString(data.c_str());
51+
}
52+
53+
virtual bool addStream(Stream& stream, const size_t maxLen) = 0;
54+
virtual void calculate() = 0;
55+
virtual void getBytes(uint8_t* output) = 0;
56+
virtual void getChars(char* output) = 0;
57+
virtual String toString() = 0;
58+
};
59+
60+
#endif

Diff for: cores/esp32/MD5Builder.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
License along with this library; if not, write to the Free Software
1717
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1818
*/
19+
1920
#include <Arduino.h>
2021
#include <HEXBuilder.h>
2122
#include <MD5Builder.h>
@@ -26,14 +27,14 @@ void MD5Builder::begin(void)
2627
esp_rom_md5_init(&_ctx);
2728
}
2829

29-
void MD5Builder::add(uint8_t * data, uint16_t len)
30+
void MD5Builder::add(uint8_t * data, size_t len)
3031
{
3132
esp_rom_md5_update(&_ctx, data, len);
3233
}
3334

3435
void MD5Builder::addHexString(const char * data)
3536
{
36-
uint16_t len = strlen(data);
37+
size_t len = strlen(data);
3738
uint8_t * tmp = (uint8_t*)malloc(len/2);
3839
if(tmp == NULL) {
3940
return;

Diff for: cores/esp32/MD5Builder.h

+22-36
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
/*
1+
/*
22
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
3-
This file is part of the esp8266 core for Arduino environment.
4-
3+
This file is part of the esp32 core for Arduino environment.
4+
55
This library is free software; you can redistribute it and/or
66
modify it under the terms of the GNU Lesser General Public
77
License as published by the Free Software Foundation; either
@@ -15,53 +15,39 @@
1515
You should have received a copy of the GNU Lesser General Public
1616
License along with this library; if not, write to the Free Software
1717
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
19+
Modified 10 Jan 2024 by Lucas Saavedra Vaz (Use abstract class HashBuilder)
1820
*/
19-
#ifndef __ESP8266_MD5_BUILDER__
20-
#define __ESP8266_MD5_BUILDER__
21+
#ifndef MD5Builder_h
22+
#define MD5Builder_h
2123

2224
#include <WString.h>
2325
#include <Stream.h>
2426

2527
#include "esp_system.h"
2628
#include "esp_rom_md5.h"
2729

28-
#include "HEXBuilder.h"
30+
#include "HashBuilder.h"
2931

30-
class MD5Builder : HEXBuilder
32+
class MD5Builder : public HashBuilder
3133
{
3234
private:
3335
md5_context_t _ctx;
3436
uint8_t _buf[ESP_ROM_MD5_DIGEST_LEN];
3537
public:
36-
void begin(void);
37-
void add(uint8_t * data, uint16_t len);
38-
void add(const char * data)
39-
{
40-
add((uint8_t*)data, strlen(data));
41-
}
42-
void add(char * data)
43-
{
44-
add((const char*)data);
45-
}
46-
void add(String data)
47-
{
48-
add(data.c_str());
49-
}
50-
void addHexString(const char * data);
51-
void addHexString(char * data)
52-
{
53-
addHexString((const char*)data);
54-
}
55-
void addHexString(String data)
56-
{
57-
addHexString(data.c_str());
58-
}
59-
bool addStream(Stream & stream, const size_t maxLen);
60-
void calculate(void);
61-
void getBytes(uint8_t * output);
62-
void getChars(char * output);
63-
String toString(void);
64-
};
38+
void begin(void) override;
6539

40+
using HashBuilder::add;
41+
void add(uint8_t * data, size_t len) override;
42+
43+
using HashBuilder::addHexString;
44+
void addHexString(const char * data) override;
45+
46+
bool addStream(Stream & stream, const size_t maxLen) override;
47+
void calculate(void) override;
48+
void getBytes(uint8_t * output) override;
49+
void getChars(char * output) override;
50+
String toString(void) override;
51+
};
6652

6753
#endif

0 commit comments

Comments
 (0)