Skip to content

Remove duplicated sha1 implementation (Fixes #6568) #6569

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 16 additions & 37 deletions libraries/Hash/src/Hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,18 @@
*/

#include <Arduino.h>
#include <bearssl/bearssl_hash.h>

#include "Hash.h"

extern "C" {
#include "sha1/sha1.h"
}

/**
* create a sha1 hash from data
* @param data uint8_t *
* @param size uint32_t
* @param hash uint8_t[20]
*/
void sha1(uint8_t * data, uint32_t size, uint8_t hash[20]) {

SHA1_CTX ctx;
void sha1(const uint8_t* data, uint32_t size, uint8_t hash[20]) {
br_sha1_context ctx;

#ifdef DEBUG_SHA1
os_printf("DATA:");
Expand All @@ -53,9 +49,9 @@ void sha1(uint8_t * data, uint32_t size, uint8_t hash[20]) {
os_printf("\n");
#endif

SHA1Init(&ctx);
SHA1Update(&ctx, data, size);
SHA1Final(hash, &ctx);
br_sha1_init(&ctx);
br_sha1_update(&ctx, data, size);
br_sha1_out(&ctx, hash);

#ifdef DEBUG_SHA1
os_printf("SHA1:");
Expand All @@ -66,52 +62,35 @@ void sha1(uint8_t * data, uint32_t size, uint8_t hash[20]) {
#endif
}

void sha1(char * data, uint32_t size, uint8_t hash[20]) {
sha1((uint8_t *) data, size, hash);
}

void sha1(const uint8_t * data, uint32_t size, uint8_t hash[20]) {
sha1((uint8_t *) data, size, hash);
}

void sha1(const char * data, uint32_t size, uint8_t hash[20]) {
sha1((uint8_t *) data, size, hash);
void sha1(const char* data, uint32_t size, uint8_t hash[20]) {
sha1((const uint8_t *) data, size, hash);
}

void sha1(String data, uint8_t hash[20]) {
void sha1(const String& data, uint8_t hash[20]) {
sha1(data.c_str(), data.length(), hash);
}

String sha1(uint8_t* data, uint32_t size) {
String sha1(const uint8_t* data, uint32_t size) {
uint8_t hash[20];
String hashStr = "";
String hashStr((const char*)nullptr);
hashStr.reserve(20 * 2 + 1);

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

for(uint16_t i = 0; i < 20; i++) {
String hex = String(hash[i], HEX);
if(hex.length() < 2) {
hex = "0" + hex;
}
char hex[3];
snprintf(hex, sizeof(hex), "%02x", hash[i]);
hashStr += hex;
}

return hashStr;
}

String sha1(char* data, uint32_t size) {
return sha1((uint8_t*) data, size);
}

String sha1(const uint8_t* data, uint32_t size) {
return sha1((uint8_t*) data, size);
}

String sha1(const char* data, uint32_t size) {
return sha1((uint8_t*) data, size);
return sha1((const uint8_t*) data, size);
}

String sha1(String data) {
String sha1(const String& data) {
return sha1(data.c_str(), data.length());
}

12 changes: 4 additions & 8 deletions libraries/Hash/src/Hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,12 @@

//#define DEBUG_SHA1

void sha1(uint8_t * data, uint32_t size, uint8_t hash[20]);
void sha1(char * data, uint32_t size, uint8_t hash[20]);
void sha1(const uint8_t * data, uint32_t size, uint8_t hash[20]);
void sha1(const char * data, uint32_t size, uint8_t hash[20]);
void sha1(String data, uint8_t hash[20]);
void sha1(const uint8_t* data, uint32_t size, uint8_t hash[20]);
void sha1(const char* data, uint32_t size, uint8_t hash[20]);
void sha1(const String& data, uint8_t hash[20]);

String sha1(uint8_t* data, uint32_t size);
String sha1(char* data, uint32_t size);
String sha1(const uint8_t* data, uint32_t size);
String sha1(const char* data, uint32_t size);
String sha1(String data);
String sha1(const String& data);

#endif /* HASH_H_ */
208 changes: 0 additions & 208 deletions libraries/Hash/src/sha1/sha1.c

This file was deleted.

32 changes: 0 additions & 32 deletions libraries/Hash/src/sha1/sha1.h

This file was deleted.