当前位置:   article > 正文

SHA-256算法的原理与C/C++实现

sha-256

一、原理

SHA-256 是一种加密哈希函数,旨在将任意大小的数据映射到一个固定大小的哈希值,通常是 256 位(32 字节)。它属于 SHA-2(安全哈希算法 2)家族,旨在提供更高的安全性。

SHA-256 的设计原则包括以下关键步骤:

  • 消息填充:输入消息的位数必须是 512 的倍数。因此,第一步是对输入消息进行填充。填充包括在消息末尾附加一个 '1',然后追加足够的零,使消息长度对 512 取模后余 448。
  • 追加消息长度:填充后,将原始的 64 位消息长度追加到消息的末尾。这确保了哈希值受到消息长度的影响,增强了安全性。
  • 哈希值的初始化:SHA-256 使用 8 个 32 位字作为初始哈希值。这些值是使用前 8 个素数的平方根的小数部分来设置的。
  • 消息分块:对填充后的消息分为 512 位的块,每个块包含 16 个 32 位字。
  • 消息调度:依次处理每个块,生成 64 个扩展的 32 位字。这些字大部分基于之前的字和哈希值,通过一系列逻辑函数和位运算计算得出。
  • 压缩函数:SHA-256 使用一种包含 64 轮的压缩函数,每一轮应用不同的逻辑函数和常数。每一轮都会修改哈希值的不同部分,引入新的数据。
  • 最终哈希值:处理完所有块后,最终的 8 个 32 位字被连接起来形成 256 位的哈希值。

二、C/C++实现

SHA-256.h

  1. #pragma once
  2. #ifndef SHA_256_H
  3. #define SHA_256_H
  4. #include <stdlib.h>
  5. #include <stdint.h>
  6. typedef struct hash_context {
  7. uint8_t buffer[64];
  8. uint32_t state[8];
  9. uint32_t total[2];
  10. } hash_context;
  11. void hash_start(hash_context* ctx);
  12. void hash_update(hash_context* ctx, uint8_t* input, size_t ilen);
  13. void hash_finish(hash_context* ctx, uint8_t* output);
  14. static void sha256_transform(hash_context* ctx, const uint8_t data[]);
  15. #endif // SHA_256_H

SHA-256.cpp

  1. #include <cstdint>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <vector>
  5. #include <time.h>
  6. #include <cstdlib>
  7. #include "SHA-256.h"
  8. using namespace std;
  9. typedef unsigned int uint32_t;
  10. typedef unsigned char uint8_t;
  11. //typedef struct hash_context {
  12. // uint8_t buffer[64];
  13. // uint32_t state[8];
  14. // uint32_t total[2];
  15. //} hash_context;
  16. // SHA-256 算法的宏定义
  17. #define ROTRIGHT(word, bits) (((word) >> (bits)) | ((word) << (32 - (bits))))
  18. #define CH(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
  19. #define MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
  20. #define EP0(x) (ROTRIGHT(x, 2) ^ ROTRIGHT(x, 13) ^ ROTRIGHT(x, 22))
  21. #define EP1(x) (ROTRIGHT(x, 6) ^ ROTRIGHT(x, 11) ^ ROTRIGHT(x, 25))
  22. #define SIG0(x) (ROTRIGHT(x, 7) ^ ROTRIGHT(x, 18) ^ ((x) >> 3))
  23. #define SIG1(x) (ROTRIGHT(x, 17) ^ ROTRIGHT(x, 19) ^ ((x) >> 10))
  24. // SHA-256 常量
  25. static const uint32_t K[64] = {
  26. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  27. 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  28. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  29. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  30. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  31. 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  32. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  33. 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  34. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  35. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  36. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  37. 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  38. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  39. 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  40. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  41. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  42. };
  43. 函数声明
  44. //void hash_start(hash_context* ctx);
  45. //void hash_update(hash_context* ctx, uint8_t* input, size_t ilen);
  46. //void hash_finish(hash_context* ctx, uint8_t* output);
  47. //static void sha256_transform(hash_context* ctx, const uint8_t data[]);
  48. static void sha256_transform(hash_context* ctx, const uint8_t data[]) {
  49. uint32_t a, b, c, d, e, f, g, h, i, j, t1, t2, m[64];
  50. for (i = 0, j = 0; i < 16; ++i, j += 4)
  51. m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]);
  52. for (; i < 64; ++i)
  53. m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16];
  54. a = ctx->state[0];
  55. b = ctx->state[1];
  56. c = ctx->state[2];
  57. d = ctx->state[3];
  58. e = ctx->state[4];
  59. f = ctx->state[5];
  60. g = ctx->state[6];
  61. h = ctx->state[7];
  62. for (i = 0; i < 64; ++i) {
  63. t1 = h + EP1(e) + CH(e, f, g) + K[i] + m[i];
  64. t2 = EP0(a) + MAJ(a, b, c);
  65. h = g;
  66. g = f;
  67. f = e;
  68. e = d + t1;
  69. d = c;
  70. c = b;
  71. b = a;
  72. a = t1 + t2;
  73. }
  74. ctx->state[0] += a;
  75. ctx->state[1] += b;
  76. ctx->state[2] += c;
  77. ctx->state[3] += d;
  78. ctx->state[4] += e;
  79. ctx->state[5] += f;
  80. ctx->state[6] += g;
  81. ctx->state[7] += h;
  82. }
  83. void hash_start(hash_context* ctx) {
  84. ctx->state[0] = 0x6a09e667;
  85. ctx->state[1] = 0xbb67ae85;
  86. ctx->state[2] = 0x3c6ef372;
  87. ctx->state[3] = 0xa54ff53a;
  88. ctx->state[4] = 0x510e527f;
  89. ctx->state[5] = 0x9b05688c;
  90. ctx->state[6] = 0x1f83d9ab;
  91. ctx->state[7] = 0x5be0cd19;
  92. ctx->total[0] = 0;
  93. ctx->total[1] = 0;
  94. }
  95. void hash_update(hash_context* ctx, uint8_t* input, size_t ilen) {
  96. size_t fill;
  97. uint32_t left;
  98. if (ilen == 0)
  99. return;
  100. left = ctx->total[0] & 0x3F;
  101. fill = 64 - left;
  102. ctx->total[0] += (uint32_t)ilen;
  103. ctx->total[0] &= 0xFFFFFFFF;
  104. if (ctx->total[0] < (uint32_t)ilen)
  105. ctx->total[1]++;
  106. if (left && ilen >= fill) {
  107. memcpy((void*)(ctx->buffer + left), input, fill);
  108. sha256_transform(ctx, ctx->buffer);
  109. input += fill;
  110. ilen -= fill;
  111. left = 0;
  112. }
  113. while (ilen >= 64) {
  114. sha256_transform(ctx, input);
  115. input += 64;
  116. ilen -= 64;
  117. }
  118. if (ilen > 0) {
  119. memcpy((void*)(ctx->buffer + left), input, ilen);
  120. }
  121. }
  122. void hash_finish(hash_context* ctx, uint8_t* output) {
  123. uint32_t last, padn;
  124. uint32_t high, low;
  125. uint8_t msglen[8];
  126. high = (ctx->total[0] >> 29) | (ctx->total[1] << 3);
  127. low = (ctx->total[0] << 3);
  128. msglen[0] = (uint8_t)(high >> 24);
  129. msglen[1] = (uint8_t)(high >> 16);
  130. msglen[2] = (uint8_t)(high >> 8);
  131. msglen[3] = (uint8_t)(high);
  132. msglen[4] = (uint8_t)(low >> 24);
  133. msglen[5] = (uint8_t)(low >> 16);
  134. msglen[6] = (uint8_t)(low >> 8);
  135. msglen[7] = (uint8_t)(low);
  136. last = ctx->total[0] & 0x3F;
  137. padn = (last < 56) ? (56 - last) : (120 - last);
  138. hash_update(ctx, (uint8_t*)"\x80", 1);
  139. hash_update(ctx, (uint8_t*)(msglen + 1), padn - 1);
  140. hash_update(ctx, msglen, 8);
  141. for (int i = 0; i < 8; i++) {
  142. output[i * 4] = (uint8_t)(ctx->state[i] >> 24);
  143. output[i * 4 + 1] = (uint8_t)(ctx->state[i] >> 16);
  144. output[i * 4 + 2] = (uint8_t)(ctx->state[i] >> 8);
  145. output[i * 4 + 3] = (uint8_t)(ctx->state[i]);
  146. }
  147. }
  148. /************************** 宏定义 **************************/
  149. //#define DATA_SIZE 1073741824
  150. #define DATA_SIZE 1073741824ULL // 将数据大小定义为 unsigned long long
  151. #define ROUNDS 1ULL // 将循环次数定义为 unsigned long long
  152. int main()
  153. {
  154. hash_context ctx;
  155. uint8_t hash[32]; // SHA-256 输出长度为 32 字节
  156. clock_t start, end;
  157. // 初始化随机数生成器
  158. srand(static_cast<unsigned int>(time(NULL)));
  159. uint8_t* data = (uint8_t*)malloc(DATA_SIZE); // 分配数据缓冲区
  160. // 生成随机数据填充到 data 数组中
  161. for (unsigned long long i = 0; i < DATA_SIZE; i++) // 使用 unsigned long long
  162. {
  163. data[i] = rand() & 0xFF;
  164. }
  165. // 开始计时
  166. start = clock();
  167. for (unsigned long long i = 0; i < ROUNDS; i++) // 使用 unsigned long long
  168. {
  169. hash_start(&ctx);
  170. hash_update(&ctx, data, DATA_SIZE);
  171. hash_finish(&ctx, hash);
  172. }
  173. // 结束计时
  174. end = clock();
  175. // 释放分配的内存
  176. free(data);
  177. // 计算总运行时间和每秒处理的数据量
  178. double time = (double)(end - start) / CLOCKS_PER_SEC;
  179. double computing_speed = (DATA_SIZE * ROUNDS * (unsigned long long)8 / 1000 / 1000) / time;
  180. printf("运行时间: %f seconds\n", time);
  181. printf("运算速度: %f Mbps\n", computing_speed);
  182. return 0;
  183. }

三、运行结果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/1014090
推荐阅读
相关标签
  

闽ICP备14008679号