sha256.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // sha256.c
  2. #pragma once
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdint.h>
  6. #include <string>
  7. #include <ostream>
  8. #include <sstream>
  9. using namespace std;
  10. static void __sha256(const unsigned char *data, size_t len, unsigned char *out);
  11. static string sha256(string str) {
  12. unsigned char buff[32];
  13. ::memset(buff, 0, 32);
  14. const char *c_str = str.c_str();
  15. __sha256((unsigned char *)c_str, ::strlen(c_str), buff);
  16. ostringstream oss;
  17. for (int i = 0; i < 32; i++) {
  18. oss << hex << setiosflags(ios::right) << setw(2) << setfill('0') << (int)(buff[i]);
  19. }
  20. return oss.str();
  21. }
  22. #define rightrotate(w, n) ((w >> n) | (w) << (32-(n)))
  23. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  24. #define copy_uint32(p, val) *((uint32_t *)p) = __builtin_bswap32((val))//gcc 内建函数__builtin_bswap32,
  25. #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  26. #define copy_uint32(p, val) *((uint32_t *)p) = (val)
  27. #else
  28. #error "Unsupported target architecture endianess!"
  29. #endif
  30. static const uint32_t k[64] = {
  31. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  32. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  33. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  34. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  35. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  36. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  37. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  38. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  39. };
  40. static void __sha256(const unsigned char *data, size_t len, unsigned char *out) {
  41. uint32_t h0 = 0x6a09e667;
  42. uint32_t h1 = 0xbb67ae85;
  43. uint32_t h2 = 0x3c6ef372;
  44. uint32_t h3 = 0xa54ff53a;
  45. uint32_t h4 = 0x510e527f;
  46. uint32_t h5 = 0x9b05688c;
  47. uint32_t h6 = 0x1f83d9ab;
  48. uint32_t h7 = 0x5be0cd19;
  49. int r = (int)(len * 8 % 512);
  50. int append = ((r < 448) ? (448 - r) : (448 + 512 - r)) / 8;
  51. size_t new_len = len + append + 8;
  52. unsigned char buf[new_len];
  53. bzero(buf + len, append);
  54. if (len > 0) {
  55. memcpy(buf, data, len);
  56. }
  57. buf[len] = (unsigned char)0x80;
  58. uint64_t bits_len = len * 8;
  59. for (int i = 0; i < 8; i++) {
  60. buf[len + append + i] = (bits_len >> ((7 - i) * 8)) & 0xff;
  61. }
  62. uint32_t w[64];
  63. bzero(w, 64);
  64. size_t chunk_len = new_len / 64;
  65. for (int idx = 0; idx < chunk_len; idx++) {
  66. uint32_t val = 0;
  67. for (int i = 0; i < 64; i++) {
  68. val = val | (*(buf + idx * 64 + i) << (8 * (3 - i)));
  69. if (i % 4 == 3) {
  70. w[i / 4] = val;
  71. val = 0;
  72. }
  73. }
  74. for (int i = 16; i < 64; i++) {
  75. uint32_t s0 = rightrotate(w[i - 15], 7) ^ rightrotate(w[i - 15], 18) ^ (w[i - 15] >> 3);
  76. uint32_t s1 = rightrotate(w[i - 2], 17) ^ rightrotate(w[i - 2], 19) ^ (w[i - 2] >> 10);
  77. w[i] = w[i - 16] + s0 + w[i - 7] + s1;
  78. }
  79. uint32_t a = h0;
  80. uint32_t b = h1;
  81. uint32_t c = h2;
  82. uint32_t d = h3;
  83. uint32_t e = h4;
  84. uint32_t f = h5;
  85. uint32_t g = h6;
  86. uint32_t h = h7;
  87. for (int i = 0; i < 64; i++) {
  88. uint32_t s_1 = rightrotate(e, 6) ^ rightrotate(e, 11) ^ rightrotate(e, 25);
  89. uint32_t ch = (e & f) ^ (~e & g);
  90. uint32_t temp1 = h + s_1 + ch + k[i] + w[i];
  91. uint32_t s_0 = rightrotate(a, 2) ^ rightrotate(a, 13) ^ rightrotate(a, 22);
  92. uint32_t maj = (a & b) ^ (a & c) ^ (b & c);
  93. uint32_t temp2 = s_0 + maj;
  94. h = g;
  95. g = f;
  96. f = e;
  97. e = d + temp1;
  98. d = c;
  99. c = b;
  100. b = a;
  101. a = temp1 + temp2;
  102. }
  103. h0 += a;
  104. h1 += b;
  105. h2 += c;
  106. h3 += d;
  107. h4 += e;
  108. h5 += f;
  109. h6 += g;
  110. h7 += h;
  111. }
  112. copy_uint32(out, h0);
  113. copy_uint32(out + 1, h1);
  114. copy_uint32(out + 2, h2);
  115. copy_uint32(out + 3, h3);
  116. copy_uint32(out + 4, h4);
  117. copy_uint32(out + 5, h5);
  118. copy_uint32(out + 6, h6);
  119. copy_uint32(out + 7, h7);
  120. }