Dynalib Utils
SHA256.h
Go to the documentation of this file.
1 #ifndef SHA256_H
2 #define SHA256_H
3 #include <string>
4 #include <cstddef>
5 #include "../TypeDefs.h"
6 
7 using namespace std;
8 
9 #define SHA2_SHFR(x, n) ((x) >> (n))
10 #define SHA2_ROTR(x, n) (((x) >> (n)) | ((x) << ((sizeof(x) << 3) - (n))))
11 #define SHA2_ROTL(x, n) (((x) << (n)) | ((x) >> ((sizeof(x) << 3) - (n))))
12 #define SHA2_CH(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
13 #define SHA2_MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
14 #define SHA256_F1(x) (SHA2_ROTR(x, 2) ^ SHA2_ROTR(x, 13) ^ SHA2_ROTR(x, 22))
15 #define SHA256_F2(x) (SHA2_ROTR(x, 6) ^ SHA2_ROTR(x, 11) ^ SHA2_ROTR(x, 25))
16 #define SHA256_F3(x) (SHA2_ROTR(x, 7) ^ SHA2_ROTR(x, 18) ^ SHA2_SHFR(x, 3))
17 #define SHA256_F4(x) (SHA2_ROTR(x, 17) ^ SHA2_ROTR(x, 19) ^ SHA2_SHFR(x, 10))
18 #define SHA2_UNPACK32(x, str) \
19 { \
20  *((str) + 3) = (uint8_t) ((x) ); \
21  *((str) + 2) = (uint8_t) ((x) >> 8); \
22  *((str) + 1) = (uint8_t) ((x) >> 16); \
23  *((str) + 0) = (uint8_t) ((x) >> 24); \
24 }
25 #define SHA2_PACK32(str, x) \
26 { \
27  *(x) = ((uint32_t) *((str) + 3) ) \
28  | ((uint32_t) *((str) + 2) << 8) \
29  | ((uint32_t) *((str) + 1) << 16) \
30  | ((uint32_t) *((str) + 0) << 24); \
31 }
32 
33 class SHA256
34 {
35 protected:
36  typedef unsigned char uint8;
37  typedef unsigned int uint32;
38  typedef unsigned long long uint64;
39 
40  const static uint32_t sha256_k[];
41  static const unsigned int SHA224_256_BLOCK_SIZE = (512 / 8);
42  unsigned int m_tot_len;
43  unsigned int m_len;
44  unsigned char m_block[2 * SHA224_256_BLOCK_SIZE];
45  uint32_t m_h[8];
46 
47  void transform(const unsigned char *message, unsigned int block_nb);
48 
49 public:
50  static const unsigned int DIGEST_SIZE = ( 256 / 8);
51 
52  void init();
53  void update(const unsigned char* message, uint len);
54  void final(uint8_t* digest);
55 };
56 
57 extern uint8_t* sha256(unsigned char* input, uint inputLen, uint8_t* digest);
58 extern uint8_t* sha256(std::string input, uint8_t* digest);
59 
60 #endif
unsigned char uint8
Definition: SHA256.h:36
unsigned int m_tot_len
Definition: SHA256.h:42
Definition: SHA256.h:33
unsigned int uint32
Definition: SHA256.h:37
unsigned int m_len
Definition: SHA256.h:43
uint8_t * sha256(unsigned char *input, uint inputLen, uint8_t *digest)
Definition: SHA256.cpp:102
unsigned long long uint64
Definition: SHA256.h:38