KPN Things Device SDK
sha256.h
Go to the documentation of this file.
1// This file is part of cryptosuite. //
2// //
3// cryptosuite is free software: you can redistribute it and/or modify //
4// it under the terms of the GNU General Public License as published by //
5// the Free Software Foundation, either version 3 of the License, or //
6// (at your option) any later version. //
7// //
8// cryptosuite is distributed in the hope that it will be useful, //
9// but WITHOUT ANY WARRANTY; without even the implied warranty of //
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
11// GNU General Public License for more details. //
12// //
13// You should have received a copy of the GNU General Public License //
14// along with cryptosuite. If not, see <http://www.gnu.org/licenses/>. //
15// //
16#ifndef Sha256_h
17#define Sha256_h
18
19#include <inttypes.h>
20
21#define HASH_LENGTH 32
22#define BLOCK_LENGTH 64
23
24class Sha256 {
25
26 union Buffer {
27 uint8_t b[BLOCK_LENGTH];
28 uint32_t w[BLOCK_LENGTH / 4];
29 };
30
31 union State {
32 uint8_t b[HASH_LENGTH];
33 uint32_t w[HASH_LENGTH / 4];
34 };
35
36 public:
37 static void hash(const char input[], int inputLength, char output[], int outputLength);
38 static void hash(const uint8_t input[], int inputLength, uint8_t output[], int outputLength);
39
40 void init();
41 void initHmac(const uint8_t * key, int keyLength);
42
43 void write(const uint8_t);
44 void write(const uint8_t in[], int inLength);
45 void write(const char in[], int inLength) {
46 write((uint8_t *)in, inLength);
47 };
48
49 uint8_t *result();
50 uint8_t *resultHmac();
51
56 void result(uint8_t out[], int outLength);
57 void result(char out[], int outLength) {
58 result((uint8_t *)out, outLength);
59 };
60
61 // Reset to initial state, but preserve key material.
62 void reset();
63
64 private:
65 void hashBlock();
66 void padBlock();
67 void push(uint8_t data);
68
69 uint32_t byteCount;
70
71 uint8_t keyBuffer[BLOCK_LENGTH];
72 uint8_t innerHash[HASH_LENGTH];
73
74 State state;
75 Buffer buffer;
76 uint8_t bufferOffset;
77};
78
79#endif
Definition: sha256.h:24
uint8_t * resultHmac()
Definition: sha256.cpp:153
void write(const uint8_t)
Definition: sha256.cpp:104
void result(char out[], int outLength)
Definition: sha256.h:57
void reset()
Definition: sha256.cpp:166
static void hash(const char input[], int inputLength, char output[], int outputLength)
Definition: sha256.cpp:68
uint8_t * result()
Definition: sha256.cpp:115
void write(const char in[], int inLength)
Definition: sha256.h:45
void initHmac(const uint8_t *key, int keyLength)
Definition: sha256.cpp:89
void init()
Definition: sha256.cpp:79
#define BLOCK_LENGTH
Definition: sha256.h:22
#define HASH_LENGTH
Definition: sha256.h:21