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