sha2.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #ifndef SHA2_H
  2. #define SHA2_H
  3. #include <string>
  4. class SHA2
  5. {
  6. public:
  7. virtual void init() = 0;
  8. virtual void update(const unsigned char *message, unsigned int len) = 0;
  9. virtual void final(unsigned char *digest) = 0;
  10. protected:
  11. typedef unsigned char uint8;
  12. typedef unsigned int uint32;
  13. typedef unsigned long long uint64;
  14. };
  15. class SHA256 : public SHA2
  16. {
  17. protected:
  18. const static uint32 sha256_k[];
  19. static const unsigned int SHA224_256_BLOCK_SIZE = (512/8);
  20. public:
  21. void init();
  22. void update(const unsigned char *message, unsigned int len);
  23. void final(unsigned char *digest);
  24. static const unsigned int DIGEST_SIZE = ( 256 / 8);
  25. protected:
  26. void transform(const unsigned char *message, unsigned int block_nb);
  27. unsigned int m_tot_len;
  28. unsigned int m_len;
  29. unsigned char m_block[2*SHA224_256_BLOCK_SIZE];
  30. uint32 m_h[8];
  31. };
  32. class SHA224 : public SHA256
  33. {
  34. public:
  35. void init();
  36. void update(const unsigned char *message, unsigned int len);
  37. void final(unsigned char *digest);
  38. static const unsigned int DIGEST_SIZE = ( 224 / 8);
  39. };
  40. class SHA512 : public SHA2
  41. {
  42. protected:
  43. const static uint64 sha512_k[];
  44. static const unsigned int SHA384_512_BLOCK_SIZE = (1024/8);
  45. public:
  46. void init();
  47. void update(const unsigned char *message, unsigned int len);
  48. void final(unsigned char *digest);
  49. static const unsigned int DIGEST_SIZE = ( 512 / 8);
  50. protected:
  51. void transform(const unsigned char *message, unsigned int block_nb);
  52. unsigned int m_tot_len;
  53. unsigned int m_len;
  54. unsigned char m_block[2 * SHA384_512_BLOCK_SIZE];
  55. uint64 m_h[8];
  56. };
  57. class SHA384 : public SHA512
  58. {
  59. public:
  60. void init();
  61. void update(const unsigned char *message, unsigned int len);
  62. void final(unsigned char *digest);
  63. static const unsigned int DIGEST_SIZE = ( 384 / 8);
  64. };
  65. std::string sha224(std::string input);
  66. std::string sha256(std::string input);
  67. std::string sha384(std::string input);
  68. std::string sha512(std::string input);
  69. #define SHA2_SHFR(x, n) (x >> n)
  70. #define SHA2_ROTR(x, n) ((x >> n) | (x << ((sizeof(x) << 3) - n)))
  71. #define SHA2_ROTL(x, n) ((x << n) | (x >> ((sizeof(x) << 3) - n)))
  72. #define SHA2_CH(x, y, z) ((x & y) ^ (~x & z))
  73. #define SHA2_MAJ(x, y, z) ((x & y) ^ (x & z) ^ (y & z))
  74. #define SHA256_F1(x) (SHA2_ROTR(x, 2) ^ SHA2_ROTR(x, 13) ^ SHA2_ROTR(x, 22))
  75. #define SHA256_F2(x) (SHA2_ROTR(x, 6) ^ SHA2_ROTR(x, 11) ^ SHA2_ROTR(x, 25))
  76. #define SHA256_F3(x) (SHA2_ROTR(x, 7) ^ SHA2_ROTR(x, 18) ^ SHA2_SHFR(x, 3))
  77. #define SHA256_F4(x) (SHA2_ROTR(x, 17) ^ SHA2_ROTR(x, 19) ^ SHA2_SHFR(x, 10))
  78. #define SHA512_F1(x) (SHA2_ROTR(x, 28) ^ SHA2_ROTR(x, 34) ^ SHA2_ROTR(x, 39))
  79. #define SHA512_F2(x) (SHA2_ROTR(x, 14) ^ SHA2_ROTR(x, 18) ^ SHA2_ROTR(x, 41))
  80. #define SHA512_F3(x) (SHA2_ROTR(x, 1) ^ SHA2_ROTR(x, 8) ^ SHA2_SHFR(x, 7))
  81. #define SHA512_F4(x) (SHA2_ROTR(x, 19) ^ SHA2_ROTR(x, 61) ^ SHA2_SHFR(x, 6))
  82. #define SHA2_UNPACK32(x, str) \
  83. { \
  84. *((str) + 3) = (uint8) ((x) ); \
  85. *((str) + 2) = (uint8) ((x) >> 8); \
  86. *((str) + 1) = (uint8) ((x) >> 16); \
  87. *((str) + 0) = (uint8) ((x) >> 24); \
  88. }
  89. #define SHA2_PACK32(str, x) \
  90. { \
  91. *(x) = ((uint32) *((str) + 3) ) \
  92. | ((uint32) *((str) + 2) << 8) \
  93. | ((uint32) *((str) + 1) << 16) \
  94. | ((uint32) *((str) + 0) << 24); \
  95. }
  96. #define SHA2_UNPACK64(x, str) \
  97. { \
  98. *((str) + 7) = (uint8) ((x) ); \
  99. *((str) + 6) = (uint8) ((x) >> 8); \
  100. *((str) + 5) = (uint8) ((x) >> 16); \
  101. *((str) + 4) = (uint8) ((x) >> 24); \
  102. *((str) + 3) = (uint8) ((x) >> 32); \
  103. *((str) + 2) = (uint8) ((x) >> 40); \
  104. *((str) + 1) = (uint8) ((x) >> 48); \
  105. *((str) + 0) = (uint8) ((x) >> 56); \
  106. }
  107. #define SHA2_PACK64(str, x) \
  108. { \
  109. *(x) = ((uint64) *((str) + 7) ) \
  110. | ((uint64) *((str) + 6) << 8) \
  111. | ((uint64) *((str) + 5) << 16) \
  112. | ((uint64) *((str) + 4) << 24) \
  113. | ((uint64) *((str) + 3) << 32) \
  114. | ((uint64) *((str) + 2) << 40) \
  115. | ((uint64) *((str) + 1) << 48) \
  116. | ((uint64) *((str) + 0) << 56); \
  117. }
  118. #endif