diff --git a/examples/sha256/sha256.ino b/examples/sha256/sha256.ino index bddbd49..b3be9f7 100644 --- a/examples/sha256/sha256.ino +++ b/examples/sha256/sha256.ino @@ -9,35 +9,25 @@ */ #include +#include #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() { } 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 3c5587b..6376d53 100644 --- a/src/Arduino_SHA256.h +++ b/src/Arduino_SHA256.h @@ -10,3 +10,5 @@ #pragma once #include "./sha256/SHA256.h" +using namespace arduino; +using namespace sha256; 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