From 8b5afd27f2a27c13bf903624bd1792f59c5ac7a0 Mon Sep 17 00:00:00 2001 From: pennam Date: Wed, 4 Dec 2024 11:37:58 +0100 Subject: [PATCH 1/4] sha256: move SHA256 class inside sha256 namespace --- examples/sha256/sha256.ino | 2 +- src/bpid/bpid.cpp | 2 +- src/sha256/SHA256.cpp | 23 ----------------------- src/sha256/SHA256.h | 36 +++++++++++++++++++++--------------- 4 files changed, 23 insertions(+), 40 deletions(-) delete mode 100644 src/sha256/SHA256.cpp diff --git a/examples/sha256/sha256.ino b/examples/sha256/sha256.ino index bddbd49..9dea9b1 100644 --- a/examples/sha256/sha256.ino +++ b/examples/sha256/sha256.ino @@ -15,7 +15,7 @@ void setup() { Serial.begin(9600); while(!Serial); - uint8_t sha[SHA256::HASH_SIZE]; + uint8_t sha[SHA256_DIGEST_SIZE]; SHA256 sha256; sha256.begin(); diff --git a/src/bpid/bpid.cpp b/src/bpid/bpid.cpp index 44a3eda..2876e63 100644 --- a/src/bpid/bpid.cpp +++ b/src/bpid/bpid.cpp @@ -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)); } diff --git a/src/sha256/SHA256.cpp b/src/sha256/SHA256.cpp deleted file mode 100644 index 4251efd..0000000 --- a/src/sha256/SHA256.cpp +++ /dev/null @@ -1,23 +0,0 @@ -/* - This file is part of the Arduino_CloudUtils library. - - Copyright (c) 2024 Arduino SA - - This Source Code Form is subject to the terms of the Mozilla Public - License, v. 2.0. If a copy of the MPL was not distributed with this - file, You can obtain one at http://mozilla.org/MPL/2.0/. -*/ - -#include "SHA256.h" - -void SHA256::begin() { - sha256_init(&_ctx); -} - -void SHA256::update(uint8_t const * data, uint32_t const len) { - sha256_update(&_ctx, data, len); -} - -void SHA256::finalize(uint8_t * hash) { - sha256_final(&_ctx, hash); -} diff --git a/src/sha256/SHA256.h b/src/sha256/SHA256.h index a69dda2..504f249 100644 --- a/src/sha256/SHA256.h +++ b/src/sha256/SHA256.h @@ -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) { @@ -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 From 6508d7a15d76f7890cbfac1a51190e20bd8a25c9 Mon Sep 17 00:00:00 2001 From: pennam Date: Wed, 4 Dec 2024 11:38:44 +0100 Subject: [PATCH 2/4] sha256: use sha256 namespace if Arduino_SHA256 is included --- src/Arduino_SHA256.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Arduino_SHA256.h b/src/Arduino_SHA256.h index 3c5587b..71bb37b 100644 --- a/src/Arduino_SHA256.h +++ b/src/Arduino_SHA256.h @@ -10,3 +10,4 @@ #pragma once #include "./sha256/SHA256.h" +using namespace sha256; From c7fbe6d3bda34906090be6beaee2030cc4386b73 Mon Sep 17 00:00:00 2001 From: pennam Date: Wed, 4 Dec 2024 11:43:22 +0100 Subject: [PATCH 3/4] sha256 update example to use THEXT class --- examples/sha256/sha256.ino | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/examples/sha256/sha256.ino b/examples/sha256/sha256.ino index 9dea9b1..b3be9f7 100644 --- a/examples/sha256/sha256.ino +++ b/examples/sha256/sha256.ino @@ -9,6 +9,7 @@ */ #include +#include #include "buffer.h" void setup() { @@ -22,22 +23,11 @@ void setup() { 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() { } From 8db266c26ce3364b8df2dc6d4c410133f5f159aa Mon Sep 17 00:00:00 2001 From: pennam Date: Wed, 4 Dec 2024 12:30:20 +0100 Subject: [PATCH 4/4] Fix build for ESP32 boards --- src/Arduino_HEX.h | 1 + src/Arduino_SHA256.h | 1 + 2 files changed, 2 insertions(+) diff --git a/src/Arduino_HEX.h b/src/Arduino_HEX.h index c99ca04..9da8844 100644 --- a/src/Arduino_HEX.h +++ b/src/Arduino_HEX.h @@ -10,4 +10,5 @@ #pragma once #include "./hex/hex.h" +using namespace arduino; using namespace hex; diff --git a/src/Arduino_SHA256.h b/src/Arduino_SHA256.h index 71bb37b..6376d53 100644 --- a/src/Arduino_SHA256.h +++ b/src/Arduino_SHA256.h @@ -10,4 +10,5 @@ #pragma once #include "./sha256/SHA256.h" +using namespace arduino; using namespace sha256;