c语言sha256算法代码
时间: 2023-07-29 20:14:57 浏览: 57
以下是一个简单的C语言实现SHA-256算法的代码:
```c
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#define ROTR(x, n) (((x) >> (n)) | ((x) << (32 - (n))))
#define SHR(x, n) ((x) >> (n))
#define Sigma0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
#define Sigma1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
#define sigma0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHR(x, 3))
#define sigma1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHR(x, 10))
const uint32_t K[] = {
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
};
void sha256(const uint8_t *data, uint32_t len, uint8_t *digest)
{
uint32_t H[] = {
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
};
uint32_t W[64];
uint32_t a, b, c, d, e, f, g, h;
uint32_t T1, T2;
uint32_t i, j;
uint32_t padded_len = ((len + 8 + 63) / 64) * 64;
uint8_t *padded_data = (uint8_t *)malloc(padded_len);
memcpy(padded_data, data, len);
padded_data[len] = 0x80;
for (i = len + 1; i < padded_len - 8; i++) {
padded_data[i] = 0x00;
}
padded_data[padded_len - 8] = (len >> 24) & 0xff;
padded_data[padded_len - 7] = (len >> 16) & 0xff;
padded_data[padded_len - 6] = (len >> 8) & 0xff;
padded_data[padded_len - 5] = (len >> 0) & 0xff;
padded_data[padded_len - 4] = 0x00;
padded_data[padded_len - 3] = 0x00;
padded_data[padded_len - 2] = 0x00;
padded_data[padded_len - 1] = 0x00;
for (i = 0; i < padded_len / 64; i++) {
for (j = 0; j < 16; j++) {
W[j] = ((uint32_t)padded_data[i * 64 + j * 4 + 0] << 24) |
((uint32_t)padded_data[i * 64 + j * 4 + 1] << 16) |
((uint32_t)padded_data[i * 64 + j * 4 + 2] << 8) |
((uint32_t)padded_data[i * 64 + j * 4 + 3] << 0);
}
for (j = 16; j < 64; j++) {
W[j] = sigma1(W[j - 2]) + W[j - 7] + sigma0(W[j - 15]) + W[j - 16];
}
a = H[0];
b = H[1];
c = H[2];
d = H[3];
e = H[4];
f = H[5];
g = H[6];
h = H[7];
for (j = 0; j < 64; j++) {
T1 = h + Sigma1(e) + Ch(e, f, g) + K[j] + W[j];
T2 = Sigma0(a) + Maj(a, b, c);
h = g;
g = f;
f = e;
e = d + T1;
d = c;
c = b;
b = a;
a = T1 + T2;
}
H[0] += a;
H[1] += b;
H[2] += c;
H[3] += d;
H[4] += e;
H[5] += f;
H[6] += g;
H[7] += h;
}
for (i = 0; i < 8; i++) {
digest[i * 4 + 0] = (H[i] >> 24) & 0xff;
digest[i * 4 + 1] = (H[i] >> 16) & 0xff;
digest[i * 4 + 2] = (H[i] >> 8) & 0xff;
digest[i * 4 + 3] = (H[i] >> 0) & 0xff;
}
free(padded_data);
}
int main()
{
uint8_t data[] = "hello world";
uint8_t digest[32];
sha256(data, sizeof(data) - 1, digest);
printf("SHA-256 digest: ");
for (int i = 0; i < 32; i++) {
printf("%02x", digest[i]);
}
printf("\n");
return 0;
}
```
这个代码实现了SHA-256算法的核心部分,可用于计算任意长度的数据的哈希值。需要注意的是,这个代码只是一个简单的示例,还有很多细节需要考虑,如填充、字节序等问题。实际使用中,建议使用现成的SHA-256库。
阅读全文