Skip to content

Commit a7ca3c2

Browse files
authored
Merge pull request #13 from pennam/sha256-refactor
Sha256 refactor
2 parents 75a6b17 + 8db266c commit a7ca3c2

File tree

6 files changed

+30
-54
lines changed

6 files changed

+30
-54
lines changed

examples/sha256/sha256.ino

+5-15
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,25 @@
99
*/
1010

1111
#include <Arduino_SHA256.h>
12+
#include <Arduino_HEX.h>
1213
#include "buffer.h"
1314

1415
void setup() {
1516
Serial.begin(9600);
1617
while(!Serial);
1718

18-
uint8_t sha[SHA256::HASH_SIZE];
19+
uint8_t sha[SHA256_DIGEST_SIZE];
1920

2021
SHA256 sha256;
2122
sha256.begin();
2223
sha256.update(buffer, sizeof(buffer));
2324
sha256.finalize(sha);
2425

25-
Serial.println(hexEncode(sha, sizeof(sha)));
26+
Serial.println(THEXT::encode(sha, sizeof(sha)));
2627

2728
/* One-shot */
28-
arduino::sha256::sha256(buffer, sizeof(buffer), sha);
29-
Serial.println(hexEncode(sha, sizeof(sha)));
30-
}
31-
32-
static String hexEncode(uint8_t* in, uint32_t size) {
33-
String out;
34-
out.reserve((size * 2) + 1);
35-
36-
char* ptr = out.begin();
37-
for (uint32_t i = 0; i < size; i++) {
38-
ptr += sprintf(ptr, "%02X", in[i]);
39-
}
40-
return String(out.c_str());
29+
SHA256::sha256(buffer, sizeof(buffer), sha);
30+
Serial.println(THEXT::encode(sha, sizeof(sha)));
4131
}
4232

4333
void loop() { }

src/Arduino_HEX.h

+1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
#pragma once
1111

1212
#include "./hex/hex.h"
13+
using namespace arduino;
1314
using namespace hex;

src/Arduino_SHA256.h

+2
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@
1010
#pragma once
1111

1212
#include "./sha256/SHA256.h"
13+
using namespace arduino;
14+
using namespace sha256;

src/bpid/bpid.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ namespace arduino { namespace bpid {
3838
if (!get(data, sizeof(data))) {
3939
return String("");
4040
}
41-
uint8_t out[SHA256::HASH_SIZE];
41+
uint8_t out[SHA256_DIGEST_SIZE];
4242
arduino::sha256::sha256(data, sizeof(data), out);
4343
return arduino::hex::encode(out, sizeof(out));
4444
}

src/sha256/SHA256.cpp

-23
This file was deleted.

src/sha256/SHA256.h

+21-15
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,6 @@
1212

1313
#include "sha2.h"
1414

15-
class SHA256 {
16-
17-
public:
18-
19-
static constexpr uint32_t HASH_SIZE = SHA256_DIGEST_SIZE;
20-
21-
void begin();
22-
void update(uint8_t const * data, uint32_t const len);
23-
void finalize(uint8_t * hash);
24-
25-
private:
26-
27-
sha256_ctx _ctx;
28-
};
29-
3015
namespace arduino { namespace sha256 {
3116

3217
inline void begin(sha256_ctx * ctx) {
@@ -42,4 +27,25 @@ namespace arduino { namespace sha256 {
4227
::sha256(input, ilen, output);
4328
}
4429

30+
class SHA256 {
31+
public:
32+
33+
inline void begin() {
34+
return arduino::sha256::begin(&_ctx);
35+
}
36+
inline void update(uint8_t const * data, uint32_t const len) {
37+
return arduino::sha256::update(&_ctx, data, len);
38+
}
39+
inline void finalize(uint8_t * hash) {
40+
return arduino::sha256::finalize(&_ctx, hash);
41+
}
42+
static inline void sha256(uint8_t const * data, uint32_t const len, uint8_t * hash) {
43+
return arduino::sha256::sha256(data, len, hash);
44+
}
45+
46+
private:
47+
48+
sha256_ctx _ctx;
49+
};
50+
4551
}} // arduino::sha256

0 commit comments

Comments
 (0)