sha256.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SHA-256 实现(见 include/portcv/sha256.hpp)。
  2. #include "portcv/sha256.hpp"
  3. #include <cstdint>
  4. #include <cstdio>
  5. #include <string>
  6. #include <vector>
  7. namespace portcv {
  8. namespace {
  9. inline uint32_t rotr(uint32_t x, int n) { return (x >> n) | (x << (32 - n)); }
  10. /// 前 64 个素数立方根小数部分前 32 位(FIPS 180-4 §4.2.2)。
  11. const uint32_t kRound[64] = {
  12. 0x428a2f98u, 0x71374491u, 0xb5c0fbcfu, 0xe9b5dba5u, 0x3956c25bu, 0x59f111f1u, 0x923f82a4u,
  13. 0xab1c5ed5u, 0xd807aa98u, 0x12835b01u, 0x243185beu, 0x550c7dc3u, 0x72be5d74u, 0x80deb1feu,
  14. 0x9bdc06a7u, 0xc19bf174u, 0xe49b69c1u, 0xefbe4786u, 0x0fc19dc6u, 0x240ca1ccu, 0x2de92c6fu,
  15. 0x4a7484aau, 0x5cb0a9dcu, 0x76f988dau, 0x983e5152u, 0xa831c66du, 0xb00327c8u, 0xbf597fc7u,
  16. 0xc6e00bf3u, 0xd5a79147u, 0x06ca6351u, 0x14292967u, 0x27b70a85u, 0x2e1b2138u, 0x4d2c6dfcu,
  17. 0x53380d13u, 0x650a7354u, 0x766a0abbu, 0x81c2c92eu, 0x92722c85u, 0xa2bfe8a1u, 0xa81a664bu,
  18. 0xc24b8b70u, 0xc76c51a3u, 0xd192e819u, 0xd6990624u, 0xf40e3585u, 0x106aa070u, 0x19a4c116u,
  19. 0x1e376c08u, 0x2748774cu, 0x34b0bcb5u, 0x391c0cb3u, 0x4ed8aa4au, 0x5b9cca4fu, 0x682e6ff3u,
  20. 0x748f82eeu, 0x78a5636fu, 0x84c87814u, 0x8cc70208u, 0x90befffau, 0xa4506cebu, 0xbef9a3f7u,
  21. 0xc67178f2u};
  22. /// 处理一个 512 bit 分组。
  23. void process_block(const uint8_t* block, uint32_t state[8]) {
  24. uint32_t w[64];
  25. for (int i = 0; i < 16; ++i) {
  26. w[i] = (static_cast<uint32_t>(block[i * 4]) << 24) |
  27. (static_cast<uint32_t>(block[i * 4 + 1]) << 16) |
  28. (static_cast<uint32_t>(block[i * 4 + 2]) << 8) |
  29. static_cast<uint32_t>(block[i * 4 + 3]);
  30. }
  31. for (int i = 16; i < 64; ++i) {
  32. const uint32_t s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >> 3);
  33. const uint32_t s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >> 10);
  34. w[i] = w[i - 16] + s0 + w[i - 7] + s1;
  35. }
  36. uint32_t a = state[0];
  37. uint32_t b = state[1];
  38. uint32_t c = state[2];
  39. uint32_t d = state[3];
  40. uint32_t e = state[4];
  41. uint32_t f = state[5];
  42. uint32_t g = state[6];
  43. uint32_t h = state[7];
  44. for (int i = 0; i < 64; ++i) {
  45. const uint32_t s1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
  46. const uint32_t ch = (e & f) ^ ((~e) & g);
  47. const uint32_t temp1 = h + s1 + ch + kRound[i] + w[i];
  48. const uint32_t s0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);
  49. const uint32_t maj = (a & b) ^ (a & c) ^ (b & c);
  50. const uint32_t temp2 = s0 + maj;
  51. h = g;
  52. g = f;
  53. f = e;
  54. e = d + temp1;
  55. d = c;
  56. c = b;
  57. b = a;
  58. a = temp1 + temp2;
  59. }
  60. state[0] += a;
  61. state[1] += b;
  62. state[2] += c;
  63. state[3] += d;
  64. state[4] += e;
  65. state[5] += f;
  66. state[6] += g;
  67. state[7] += h;
  68. }
  69. } // namespace
  70. std::string sha256_hex(const std::string& data) {
  71. uint32_t state[8] = {0x6a09e667u, 0xbb67ae85u, 0x3c6ef372u, 0xa54ff53au,
  72. 0x510e527fu, 0x9b05688cu, 0x1f83d9abu, 0x5be0cd19u};
  73. std::vector<uint8_t> msg(data.begin(), data.end());
  74. const uint64_t bit_len = static_cast<uint64_t>(data.size()) * 8u;
  75. msg.push_back(0x80u); // 追加 '1' 位
  76. while (msg.size() % 64u != 56u) { msg.push_back(0x00u); }
  77. for (int i = 7; i >= 0; --i) { // 64 bit 大端长度
  78. msg.push_back(static_cast<uint8_t>((bit_len >> (8 * i)) & 0xFFu));
  79. }
  80. for (std::size_t offset = 0; offset < msg.size(); offset += 64) {
  81. process_block(&msg[offset], state);
  82. }
  83. char out[65];
  84. for (int i = 0; i < 8; ++i) {
  85. std::snprintf(out + i * 8, 9, "%08x", static_cast<unsigned>(state[i]));
  86. }
  87. out[64] = '\0';
  88. return std::string(out);
  89. }
  90. } // namespace portcv