Skip to content

Sha256 refactor #13

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 4 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 5 additions & 15 deletions examples/sha256/sha256.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,25 @@
*/

#include <Arduino_SHA256.h>
#include <Arduino_HEX.h>
#include "buffer.h"

void setup() {
Serial.begin(9600);
while(!Serial);

uint8_t sha[SHA256::HASH_SIZE];
uint8_t sha[SHA256_DIGEST_SIZE];

SHA256 sha256;
sha256.begin();
sha256.update(buffer, sizeof(buffer));
sha256.finalize(sha);

Serial.println(hexEncode(sha, sizeof(sha)));
Serial.println(THEXT::encode(sha, sizeof(sha)));

/* One-shot */
arduino::sha256::sha256(buffer, sizeof(buffer), sha);
Serial.println(hexEncode(sha, sizeof(sha)));
}

static String hexEncode(uint8_t* in, uint32_t size) {
String out;
out.reserve((size * 2) + 1);

char* ptr = out.begin();
for (uint32_t i = 0; i < size; i++) {
ptr += sprintf(ptr, "%02X", in[i]);
}
return String(out.c_str());
SHA256::sha256(buffer, sizeof(buffer), sha);
Serial.println(THEXT::encode(sha, sizeof(sha)));
}

void loop() { }
1 change: 1 addition & 0 deletions src/Arduino_HEX.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
#pragma once

#include "./hex/hex.h"
using namespace arduino;
using namespace hex;
2 changes: 2 additions & 0 deletions src/Arduino_SHA256.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@
#pragma once

#include "./sha256/SHA256.h"
using namespace arduino;
using namespace sha256;
2 changes: 1 addition & 1 deletion src/bpid/bpid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace arduino { namespace bpid {
if (!get(data, sizeof(data))) {
return String("");
}
uint8_t out[SHA256::HASH_SIZE];
uint8_t out[SHA256_DIGEST_SIZE];
arduino::sha256::sha256(data, sizeof(data), out);
return arduino::hex::encode(out, sizeof(out));
}
Expand Down
23 changes: 0 additions & 23 deletions src/sha256/SHA256.cpp

This file was deleted.

36 changes: 21 additions & 15 deletions src/sha256/SHA256.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,6 @@

#include "sha2.h"

class SHA256 {

public:

static constexpr uint32_t HASH_SIZE = SHA256_DIGEST_SIZE;

void begin();
void update(uint8_t const * data, uint32_t const len);
void finalize(uint8_t * hash);

private:

sha256_ctx _ctx;
};

namespace arduino { namespace sha256 {

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

class SHA256 {
public:

inline void begin() {
return arduino::sha256::begin(&_ctx);
}
inline void update(uint8_t const * data, uint32_t const len) {
return arduino::sha256::update(&_ctx, data, len);
}
inline void finalize(uint8_t * hash) {
return arduino::sha256::finalize(&_ctx, hash);
}
static inline void sha256(uint8_t const * data, uint32_t const len, uint8_t * hash) {
return arduino::sha256::sha256(data, len, hash);
}

private:

sha256_ctx _ctx;
};

}} // arduino::sha256
Loading