#include <stdio.h>
#include <stdint.h>
#include <string.h>
#define ROTR(x, n) (((x) >> (n)) | ((x) << (32 - (n)))) // 定义右旋转函数
// 定义常量K
const uint32_t K[64] = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
};
// 定义初始哈希值
const uint32_t H[8] = {
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
};
// 定义SHA-256算法的核心函数
void sha256_core(uint32_t* W, uint32_t* H)
{
uint32_t a, b, c, d, e, f, g, h, T1, T2;
uint32_t* K_ptr = (uint32_t*)K;
// 初始化工作变量
a = H[0]; b = H[1]; c = H[2]; d = H[3];
e = H[4]; f = H[5]; g = H[6]; h = H[7];
// 进行64轮操作
for (int i = 0; i < 64; i++)
{
T1 = h + ROTR(e, 6) + ROTR(e, 11) + ROTR(e, 25) + ((e & f) ^ (~e & g)) + *K_ptr + W[i];
T2 = ROTR(a, 2) + ((a & b) ^ (a & c) ^ (b & c));
h = g; g = f; f = e; e = d + T1;
d = c; c = b; b = a; a = T1 + T2;
K_ptr++; // 将指向常量K数组的指针向后移动一个位置
}
// 更新哈希值
H[0] += a; H[1] += b; H[2] += c; H[3] += d;
H[4] += e; H[5] += f; H[6] += g; H[7] += h;
}
// 定义SHA-256算法的主函数
void sha256(const uint8_t* message, uint32_t message_len, uint8_t* hash)
{
uint32_t W[64];
uint32_t H_temp[8];
uint32_t num_blocks = message_len / 64;
uint32_t last_block_len = message_len % 64;
uint8_t* message_ptr = (uint8_t*)message;
// 处理每个64字节的块
for (int i = 0; i < num_blocks; i++)
{
// 将64字节的块分成16个32位字
for (int j = 0; j < 16; j++)
{
W[j] = (message_ptr[(i * 64) + (j * 4)] << 24) |
(message_ptr[(i * 64) + (j * 4) + 1] << 16) |
(message_ptr[(i * 64) + (j * 4) + 2] << 8) |
(message_ptr[(i * 64) + (j * 4) + 3]);
}
// 扩展16个字为64个字
for (int j = 16; j < 64; j++)
{
uint32_t s0 = ROTR(W[j-15], 7) ^ ROTR(W[j-15], 18) ^ (W[j-15] >> 3);
uint32_t s1 = ROTR(W[j-2], 17) ^ ROTR(W[j-2], 19) ^ (W[j-2] >> 10);
W[j] = W[j-16] + s0 + W[j-7] + s1;
}
// 备份哈希值
memcpy(H_temp, H, sizeof(H));
// 进行SHA-256算法的核心操作
sha256_core(W, H_temp);
}
// 处理最后一个不足64字节的块
uint8_t last_block[64];
memset(last_block, 0, sizeof(last_block));
memcpy(last_block, message_ptr + (num_blocks * 64), last_block_len);
last_block[last_block_len] = 0x80;
if (last_block_len >= 56)
{
// 如果最后一个块剩余的字节不足以存储消息长度,则需要再添加一个块
memcpy(W, last_block, sizeof(W));
sha256_core(W, H);
memset(W, 0, sizeof(W));
}
*((uint64_t*)(last_block + 56)) = message_len * 8;
for (int i = 0; i < 16; i++)
{
W[i] = (last_block[i*4] << 24) | (last_block[i*4+1] << 16) | (last_block[i*4+2] << 8) | last_block[i*4+3];
}
sha256_core(W, H);
// 将哈希值写入输出缓冲区
for (int i = 0; i < 8; i++)
{
hash[i * 4] = (H[i] >> 24) & 0xFF;
hash[i * 4 + 1] = (H[i] >> 16) & 0xFF;
hash[i * 4 + 2] = (H[i] >> 8) & 0xFF;
hash[i * 4 + 3] = H[i] & 0xFF;
}
}
// 测试SHA-256算法
int main()
{
uint8_t message[] = "Hello, world!";
uint8_t hash[32];
sha256(message, strlen((char*)message), hash);
printf("SHA-256 hash of \"%s\":\n", message);
for (int i = 0; i < 32; i++)
{
printf("%02x", hash[i]);
}
printf("\n");
return 0;
}