Skip to content

Commit d2d0c62

Browse files
committed
Remove duplicated sha1 implementation (Fixes #6568)
The Hash library had its own copy of a loop-unrolled sha1 implementation adding a large code footprint for no good reason, as there are several sha1 implementations already in tree (one in NONOS-SDK as well as one in bearssl). Switching to the bearssl one is straightforward and removes about 3kb of code size overhead. Also cleanup some obvious inefficiencies (copy by value, string summing, no reservation causing repeated reallocations) in the implementation.
1 parent ffe5476 commit d2d0c62

File tree

4 files changed

+13
-255
lines changed

4 files changed

+13
-255
lines changed

libraries/Hash/src/Hash.cpp

+11-13
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,10 @@
2323
*/
2424

2525
#include <Arduino.h>
26+
#include <bearssl/bearssl_hash.h>
2627

2728
#include "Hash.h"
2829

29-
extern "C" {
30-
#include "sha1/sha1.h"
31-
}
32-
3330
/**
3431
* create a sha1 hash from data
3532
* @param data uint8_t *
@@ -38,7 +35,7 @@ extern "C" {
3835
*/
3936
void sha1(uint8_t * data, uint32_t size, uint8_t hash[20]) {
4037

41-
SHA1_CTX ctx;
38+
br_sha1_context ctx;
4239

4340
#ifdef DEBUG_SHA1
4441
os_printf("DATA:");
@@ -53,9 +50,9 @@ void sha1(uint8_t * data, uint32_t size, uint8_t hash[20]) {
5350
os_printf("\n");
5451
#endif
5552

56-
SHA1Init(&ctx);
57-
SHA1Update(&ctx, data, size);
58-
SHA1Final(hash, &ctx);
53+
br_sha1_init(&ctx);
54+
br_sha1_update(&ctx, data, size);
55+
br_sha1_out(&ctx, hash);
5956

6057
#ifdef DEBUG_SHA1
6158
os_printf("SHA1:");
@@ -78,20 +75,21 @@ void sha1(const char * data, uint32_t size, uint8_t hash[20]) {
7875
sha1((uint8_t *) data, size, hash);
7976
}
8077

81-
void sha1(String data, uint8_t hash[20]) {
78+
void sha1(const String& data, uint8_t hash[20]) {
8279
sha1(data.c_str(), data.length(), hash);
8380
}
8481

8582
String sha1(uint8_t* data, uint32_t size) {
8683
uint8_t hash[20];
87-
String hashStr = "";
84+
String hashStr((const char*)nullptr);
85+
hashStr.reserve(20 * 2 + 1);
8886

8987
sha1(&data[0], size, &hash[0]);
9088

9189
for(uint16_t i = 0; i < 20; i++) {
9290
String hex = String(hash[i], HEX);
93-
if(hex.length() < 2) {
94-
hex = "0" + hex;
91+
if(hash[i] < 0x10) {
92+
hashStr += '0';
9593
}
9694
hashStr += hex;
9795
}
@@ -111,7 +109,7 @@ String sha1(const char* data, uint32_t size) {
111109
return sha1((uint8_t*) data, size);
112110
}
113111

114-
String sha1(String data) {
112+
String sha1(const String& data) {
115113
return sha1(data.c_str(), data.length());
116114
}
117115

libraries/Hash/src/Hash.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ void sha1(uint8_t * data, uint32_t size, uint8_t hash[20]);
3131
void sha1(char * data, uint32_t size, uint8_t hash[20]);
3232
void sha1(const uint8_t * data, uint32_t size, uint8_t hash[20]);
3333
void sha1(const char * data, uint32_t size, uint8_t hash[20]);
34-
void sha1(String data, uint8_t hash[20]);
34+
void sha1(const String& data, uint8_t hash[20]);
3535

3636
String sha1(uint8_t* data, uint32_t size);
3737
String sha1(char* data, uint32_t size);
3838
String sha1(const uint8_t* data, uint32_t size);
3939
String sha1(const char* data, uint32_t size);
40-
String sha1(String data);
40+
String sha1(const String& data);
4141

4242
#endif /* HASH_H_ */

libraries/Hash/src/sha1/sha1.c

-208
This file was deleted.

libraries/Hash/src/sha1/sha1.h

-32
This file was deleted.

0 commit comments

Comments
 (0)