sha512.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #ifndef SHA512_H
  2. #define SHA512_H
  3. #include <string>
  4. class SHA512
  5. {
  6. protected:
  7. typedef unsigned char uint8;
  8. typedef unsigned int uint32;
  9. typedef unsigned long long uint64;
  10. const static uint64 sha512_k[];
  11. static const unsigned int SHA384_512_BLOCK_SIZE = (1024/8);
  12. public:
  13. void init();
  14. void update(const unsigned char *message, unsigned int len);
  15. void final(unsigned char *digest);
  16. static const unsigned int DIGEST_SIZE = ( 512 / 8);
  17. protected:
  18. void transform(const unsigned char *message, unsigned int block_nb);
  19. unsigned int m_tot_len;
  20. unsigned int m_len;
  21. unsigned char m_block[2 * SHA384_512_BLOCK_SIZE];
  22. uint64 m_h[8];
  23. };
  24. std::string sha512(std::string input);
  25. #define SHA2_SHFR(x, n) (x >> n)
  26. #define SHA2_ROTR(x, n) ((x >> n) | (x << ((sizeof(x) << 3) - n)))
  27. #define SHA2_ROTL(x, n) ((x << n) | (x >> ((sizeof(x) << 3) - n)))
  28. #define SHA2_CH(x, y, z) ((x & y) ^ (~x & z))
  29. #define SHA2_MAJ(x, y, z) ((x & y) ^ (x & z) ^ (y & z))
  30. #define SHA512_F1(x) (SHA2_ROTR(x, 28) ^ SHA2_ROTR(x, 34) ^ SHA2_ROTR(x, 39))
  31. #define SHA512_F2(x) (SHA2_ROTR(x, 14) ^ SHA2_ROTR(x, 18) ^ SHA2_ROTR(x, 41))
  32. #define SHA512_F3(x) (SHA2_ROTR(x, 1) ^ SHA2_ROTR(x, 8) ^ SHA2_SHFR(x, 7))
  33. #define SHA512_F4(x) (SHA2_ROTR(x, 19) ^ SHA2_ROTR(x, 61) ^ SHA2_SHFR(x, 6))
  34. #define SHA2_UNPACK32(x, str) \
  35. { \
  36. *((str) + 3) = (uint8) ((x) ); \
  37. *((str) + 2) = (uint8) ((x) >> 8); \
  38. *((str) + 1) = (uint8) ((x) >> 16); \
  39. *((str) + 0) = (uint8) ((x) >> 24); \
  40. }
  41. #define SHA2_UNPACK64(x, str) \
  42. { \
  43. *((str) + 7) = (uint8) ((x) ); \
  44. *((str) + 6) = (uint8) ((x) >> 8); \
  45. *((str) + 5) = (uint8) ((x) >> 16); \
  46. *((str) + 4) = (uint8) ((x) >> 24); \
  47. *((str) + 3) = (uint8) ((x) >> 32); \
  48. *((str) + 2) = (uint8) ((x) >> 40); \
  49. *((str) + 1) = (uint8) ((x) >> 48); \
  50. *((str) + 0) = (uint8) ((x) >> 56); \
  51. }
  52. #define SHA2_PACK64(str, x) \
  53. { \
  54. *(x) = ((uint64) *((str) + 7) ) \
  55. | ((uint64) *((str) + 6) << 8) \
  56. | ((uint64) *((str) + 5) << 16) \
  57. | ((uint64) *((str) + 4) << 24) \
  58. | ((uint64) *((str) + 3) << 32) \
  59. | ((uint64) *((str) + 2) << 40) \
  60. | ((uint64) *((str) + 1) << 48) \
  61. | ((uint64) *((str) + 0) << 56); \
  62. }
  63. #endif