Skip to content

Commit 2404a60

Browse files
Sven Eliassonigrr
Sven Eliasson
authored andcommitted
Const-correctness for MD5Builder (#3222)
Resolves #1175
1 parent ace0622 commit 2404a60

File tree

4 files changed

+36
-35
lines changed

4 files changed

+36
-35
lines changed

cores/esp8266/MD5Builder.cpp

+9-18
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
#include <Arduino.h>
22
#include <MD5Builder.h>
33

4-
uint8_t hex_char_to_byte(uint8_t c)
5-
{
4+
uint8_t hex_char_to_byte(uint8_t c){
65
return (c >= 'a' && c <= 'f') ? (c - ((uint8_t)'a' - 0xa)) :
76
(c >= 'A' && c <= 'F') ? (c - ((uint8_t)'A' - 0xA)) :
87
(c >= '0' && c<= '9') ? (c - (uint8_t)'0') : 0;
98
}
109

11-
void MD5Builder::begin(void)
12-
{
10+
void MD5Builder::begin(void){
1311
memset(_buf, 0x00, 16);
1412
MD5Init(&_ctx);
1513
}
1614

17-
void MD5Builder::add(uint8_t * data, uint16_t len)
18-
{
15+
void MD5Builder::add(const uint8_t * data, const uint16_t len){
1916
MD5Update(&_ctx, data, len);
2017
}
2118

22-
void MD5Builder::addHexString(const char * data)
23-
{
19+
void MD5Builder::addHexString(const char * data){
2420
uint16_t i, len = strlen(data);
2521
uint8_t * tmp = (uint8_t*)malloc(len/2);
2622
if(tmp == NULL) {
@@ -35,8 +31,7 @@ void MD5Builder::addHexString(const char * data)
3531
free(tmp);
3632
}
3733

38-
bool MD5Builder::addStream(Stream & stream, const size_t maxLen)
39-
{
34+
bool MD5Builder::addStream(Stream & stream, const size_t maxLen){
4035
const int buf_size = 512;
4136
int maxLengthLeft = maxLen;
4237
uint8_t * buf = (uint8_t*) malloc(buf_size);
@@ -76,25 +71,21 @@ bool MD5Builder::addStream(Stream & stream, const size_t maxLen)
7671
return true;
7772
}
7873

79-
void MD5Builder::calculate(void)
80-
{
74+
void MD5Builder::calculate(void){
8175
MD5Final(_buf, &_ctx);
8276
}
8377

84-
void MD5Builder::getBytes(uint8_t * output)
85-
{
78+
void MD5Builder::getBytes(uint8_t * output){
8679
memcpy(output, _buf, 16);
8780
}
8881

89-
void MD5Builder::getChars(char * output)
90-
{
82+
void MD5Builder::getChars(char * output){
9183
for(uint8_t i = 0; i < 16; i++) {
9284
sprintf(output + (i * 2), "%02x", _buf[i]);
9385
}
9486
}
9587

96-
String MD5Builder::toString(void)
97-
{
88+
String MD5Builder::toString(void){
9889
char out[33];
9990
getChars(out);
10091
return String(out);

cores/esp8266/MD5Builder.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
/*
1+
/*
22
md5.h - exposed md5 ROM functions for esp8266
33
44
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
55
This file is part of the esp8266 core for Arduino environment.
6-
6+
77
This library is free software; you can redistribute it and/or
88
modify it under the terms of the GNU Lesser General Public
99
License as published by the Free Software Foundation; either
@@ -31,13 +31,13 @@ class MD5Builder {
3131
uint8_t _buf[16];
3232
public:
3333
void begin(void);
34-
void add(uint8_t * data, uint16_t len);
35-
void add(const char * data){ add((uint8_t*)data, strlen(data)); }
34+
void add(const uint8_t * data, const uint16_t len);
35+
void add(const char * data){ add((const uint8_t*)data, strlen(data)); }
3636
void add(char * data){ add((const char*)data); }
37-
void add(String data){ add(data.c_str()); }
37+
void add(const String data){ add(data.c_str()); }
3838
void addHexString(const char * data);
3939
void addHexString(char * data){ addHexString((const char*)data); }
40-
void addHexString(String data){ addHexString(data.c_str()); }
40+
void addHexString(const String data){ addHexString(data.c_str()); }
4141
bool addStream(Stream & stream, const size_t maxLen);
4242
void calculate(void);
4343
void getBytes(uint8_t * output);

cores/esp8266/md5.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
/*
1+
/*
22
md5.h - exposed md5 ROM functions for esp8266
33
44
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
55
This file is part of the esp8266 core for Arduino environment.
6-
6+
77
original C source from https://github.com/morrissinger/ESP8266-Websocket/raw/master/MD5.h
8-
8+
99
This library is free software; you can redistribute it and/or
1010
modify it under the terms of the GNU Lesser General Public
1111
License as published by the Free Software Foundation; either
@@ -34,7 +34,7 @@ typedef struct {
3434
} md5_context_t;
3535

3636
extern void MD5Init (md5_context_t *);
37-
extern void MD5Update (md5_context_t *, uint8_t *, uint16_t);
37+
extern void MD5Update (md5_context_t *, const uint8_t *, const uint16_t);
3838
extern void MD5Final (uint8_t [16], md5_context_t *);
3939

4040
#ifdef __cplusplus

tests/host/core/test_md5builder.cpp

+17-7
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,25 @@ TEST_CASE("MD5Builder::add works as expected", "[core][MD5Builder]")
3535

3636
TEST_CASE("MD5Builder::addHexString works as expected", "[core][MD5Builder]")
3737
{
38-
MD5Builder builder;
39-
builder.begin();
40-
builder.addHexString("1234567890abcdeffedcba98765432106469676974616c7369676e61747572656170706c69636174696f6e73");
41-
builder.calculate();
42-
REQUIRE(builder.toString() == "47b937a6f9f12a4c389fa5854e023efb");
38+
WHEN("A char array is parsed"){
39+
MD5Builder builder;
40+
builder.begin();
41+
const char * myPayload = "1234567890abcdeffedcba98765432106469676974616c7369676e61747572656170706c69636174696f6e73";
42+
builder.addHexString(myPayload);
43+
builder.calculate();
44+
REQUIRE(builder.toString() == "47b937a6f9f12a4c389fa5854e023efb");
45+
}
46+
47+
WHEN("A Arduino String is parsed"){
48+
MD5Builder builder;
49+
builder.begin();
50+
builder.addHexString(String("1234567890abcdeffedcba98765432106469676974616c7369676e61747572656170706c69636174696f6e73"));
51+
builder.calculate();
52+
REQUIRE(builder.toString() == "47b937a6f9f12a4c389fa5854e023efb");
53+
}
4354
}
4455

45-
TEST_CASE("MD5Builder::addStream works", "[core][MD5Builder]")
46-
{
56+
TEST_CASE("MD5Builder::addStream works", "[core][MD5Builder]"){
4757
MD5Builder builder;
4858
const char* str = "MD5Builder::addStream_works_longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong";
4959
{

0 commit comments

Comments
 (0)