Skip to content

Commit f55d696

Browse files
committed
sha256 add class and cpp functions
1 parent 1e43277 commit f55d696

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

src/sha256/SHA256.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
This file is part of the Arduino_CloudUtils library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
#include "SHA256.h"
12+
13+
void SHA256::begin() {
14+
sha256_init(&_ctx);
15+
}
16+
17+
void SHA256::update(uint8_t const * data, uint32_t const len) {
18+
sha256_update(&_ctx, data, len);
19+
}
20+
21+
void SHA256::finalize(uint8_t * hash) {
22+
sha256_final(&_ctx, hash);
23+
}

src/sha256/SHA256.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
This file is part of the Arduino_CloudUtils library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
#pragma once
12+
13+
#include "sha256.h"
14+
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+
30+
namespace arduino { namespace sha256 {
31+
32+
inline void begin(sha256_ctx * ctx) {
33+
return sha256_init(ctx);
34+
}
35+
inline void update(sha256_ctx *ctx, const uint8_t *input, uint32_t length) {
36+
return sha256_update(ctx, input, length);
37+
}
38+
inline void finalize(sha256_ctx * ctx, uint8_t digest[SHA256_DIGEST_SIZE]) {
39+
return sha256_final(ctx, digest);
40+
}
41+
inline void oneshot(const unsigned char *input, unsigned int ilen, unsigned char *output) {
42+
::sha256(input, ilen, output);
43+
}
44+
45+
}} // arduino::sha256

0 commit comments

Comments
 (0)