]> git.piffa.net Git - arduino/blob - books/pdummies/Libraries/Sha/sha256.h
first commit
[arduino] / books / pdummies / Libraries / Sha / sha256.h
1 #ifndef Sha256_h
2 #define Sha256_h
3
4 #include <inttypes.h>
5 #include "Print.h"
6
7 #define HASH_LENGTH 32
8 #define BLOCK_LENGTH 64
9
10 union _buffer {
11   uint8_t b[BLOCK_LENGTH];
12   uint32_t w[BLOCK_LENGTH/4];
13 };
14 union _state {
15   uint8_t b[HASH_LENGTH];
16   uint32_t w[HASH_LENGTH/4];
17 };
18
19 class Sha256Class : public Print
20 {
21   public:
22     void init(void);
23     void initHmac(const uint8_t* secret, int secretLength);
24     uint8_t* result(void);
25     uint8_t* resultHmac(void);
26     virtual size_t write(uint8_t);
27     using Print::write;
28   private:
29     void pad();
30     void addUncounted(uint8_t data);
31     void hashBlock();
32     uint32_t ror32(uint32_t number, uint8_t bits);
33     _buffer buffer;
34     uint8_t bufferOffset;
35     _state state;
36     uint32_t byteCount;
37     uint8_t keyBuffer[BLOCK_LENGTH];
38     uint8_t innerHash[HASH_LENGTH];
39 };
40 extern Sha256Class Sha256;
41
42 #endif