以下是使用C语言实现SHA-256算法的代码,包含init、update和finish三个过程。 ```c #include <stdio.h> #include <stdint.h> #include <string.h> // Roate right function #define ROTR(x, n) (((x) >> (n)) | ((x) << (32 - (n)))) // SHA-256 Constants 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 }; // Initial hash values const uint32_t H[8] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 }; // SHA-256 context structure typedef struct { uint32_t data[16]; // 512-bit block to hold the message uint64_t bitlen; // Total bit length of the message uint32_t state[8]; // Intermediate hash state } sha256_ctx; // Initialize SHA-256 context void init(sha256_ctx *ctx) { memset(ctx, 0, sizeof(sha256_ctx)); memcpy(ctx->state, H, sizeof(H)); } // Update SHA-256 context with input message void update(sha256_ctx *ctx, const uint8_t *message, size_t len) { uint32_t i, j; for (i = 0; i < len; ++i) { ctx->data[ctx->bitlen / 4 % 16] <<= 8; ctx->data[ctx->bitlen / 4 % 16] |= message[i]; if ((ctx->bitlen += 8) % 512 == 0) { for (j = 0; j < 64; ++j) { uint32_t s0 = ROTR(ctx->data[j % 16], 7) ^ ROTR(ctx->data[j % 16], 18) ^ (ctx->data[j % 16] >> 3); uint32_t s1 = ROTR(ctx->data[(j + 14) % 16], 17) ^ ROTR(ctx->data[(j + 14) % 16], 19) ^ (ctx->data[(j + 14) % 16] >> 10); ctx->data[(j + 16) % 16] = ctx->data[j % 16] + s0 + ctx->data[(j + 9) % 16] + s1; } uint32_t a = ctx->state[0]; uint32_t b = ctx->state[1]; uint32_t c = ctx->state[2]; uint32_t d = ctx->state[3]; uint32_t e = ctx->state[4]; uint32_t f = ctx->state[5]; uint32_t g = ctx->state[6]; uint32_t h = ctx->state[7]; for (j = 0; j < 64; ++j) { uint32_t S1 = ROTR(e, 6) ^ ROTR(e, 11) ^ ROTR(e, 25); uint32_t ch = (e & f) ^ (~e & g); uint32_t temp1 = h + S1 + ch + K[j] + ctx->data[j % 16]; uint32_t S0 = ROTR(a, 2) ^ ROTR(a, 13) ^ ROTR(a, 22); uint32_t maj = (a & b) ^ (a & c) ^ (b & c); uint32_t temp2 = S0 + maj; h = g; g = f; f = e; e = d + temp1; d = c; c = b; b = a; a = temp1 + temp2; } ctx->state[0] += a; ctx->state[1] += b; ctx->state[2] += c; ctx->state[3] += d; ctx->state[4] += e; ctx->state[5] += f; ctx->state[6] += g; ctx->state[7] += h; } } } // Finalize SHA-256 context and generate hash value void finish(sha256_ctx *ctx, uint8_t *hash) { uint32_t i; ctx->data[ctx->bitlen / 4 % 16] <<= 8; ctx->data[ctx->bitlen / 4 % 16] |= 0x80; while ((ctx->bitlen + 8) % 512 != 448) { ctx->data[ctx->bitlen / 4 % 16] <<= 8; ctx->bitlen += 8; } ctx->data[14] = (ctx->bitlen >> 32) & 0xFFFFFFFF; ctx->data[15] = ctx->bitlen & 0xFFFFFFFF; for (i = 0; i < 64; ++i) { uint32_t s0 = ROTR(ctx->data[i % 16], 7) ^ ROTR(ctx->data[i % 16], 18) ^ (ctx->data[i % 16] >> 3); uint32_t s1 = ROTR(ctx->data[(i + 14) % 16], 17) ^ ROTR(ctx->data[(i + 14) % 16], 19) ^ (ctx->data[(i + 14) % 16] >> 10); ctx->data[(i + 16) % 16] = ctx->data[i % 16] + s0 + ctx->data[(i + 9) % 16] + s1; } uint32_t a = ctx->state[0]; uint32_t b = ctx->state[1]; uint32_t c = ctx->state[2]; uint32_t d = ctx->state[3]; uint32_t e = ctx->state[4]; uint32_t f = ctx->state[5]; uint32_t g = ctx->state[6]; uint32_t h = ctx->state[7]; for (i = 0; i < 64; ++i) { uint32_t S1 = ROTR(e, 6) ^ ROTR(e, 11) ^ ROTR(e, 25); uint32_t ch = (e & f) ^ (~e & g); uint32_t temp1 = h + S1 + ch + K[i] + ctx->data[i % 16]; uint32_t S0 = ROTR(a, 2) ^ ROTR(a, 13) ^ ROTR(a, 22); uint32_t maj = (a & b) ^ (a & c) ^ (b & c); uint32_t temp2 = S0 + maj; h = g; g = f; f = e; e = d + temp1; d = c; c = b; b = a; a = temp1 + temp2; } ctx->state[0] += a; ctx->state[1] += b; ctx->state[2] += c; ctx->state[3] += d; ctx->state[4] += e; ctx->state[5] += f; ctx->state[6] += g; ctx->state[7] += h; memcpy(hash, ctx->state, sizeof(ctx->state)); } int main() { // Test case const char *message = "Hello, World!"; sha256_ctx ctx; uint8_t hash[32]; init(&ctx); update(&ctx, (const uint8_t *)message, strlen(message)); finish(&ctx, hash); printf("Message: %s\n", message); printf("SHA-256 Hash: "); for (int i = 0; i < 32; ++i) { printf("%02x", hash[i]); } printf("\n"); return 0; } ``` 这是一个完整的SHA-256算法实现,包括了初始化(init)、更新(update)和完成(finish)三个过程。示例代码中还包含了一个简单的测试案例,用于计算字符串"Hello, World!"的SHA-256哈希值。 [2024-11-29 18:24:55 | 酷代码 | 3995字解答]