以下是一个简单的 C 语言实现 SHA-256 算法的示例代码: ```c #include <stdio.h> #include <string.h> // 常量定义 #define SHA256_BLOCK_SIZE 64 #define SHA256_HASH_SIZE 32 // 各种常量 unsigned int 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 }; // 循环右移函数 unsigned int rotateRight(unsigned int value, int shift) { return (value >> shift) | (value << (32 - shift)); } // 计算 Sigma0 函数 unsigned int sigma0(unsigned int x) { return rotateRight(x, 7) ^ rotateRight(x, 18) ^ (x >> 3); } // 计算 Sigma1 函数 unsigned int sigma1(unsigned int x) { return rotateRight(x, 17) ^ rotateRight(x, 19) ^ (x >> 10); } // 计算 Ch 函数 unsigned int ch(unsigned int x, unsigned int y, unsigned int z) { return (x & y) ^ ((~x) & z); } // 计算 Maj 函数 unsigned int maj(unsigned int x, unsigned int y, unsigned int z) { return (x & y) ^ (x & z) ^ (y & z); } // 初始化哈希值 void initHash(unsigned int hash[SHA256_HASH_SIZE / 4]) { hash[0] = 0x6a09e667; hash[1] = 0xbb67ae85; hash[2] = 0x3c6ef372; hash[3] = 0xa54ff53a; hash[4] = 0x510e527f; hash[5] = 0x9b05688c; hash[6] = 0x1f83d9ab; hash[7] = 0x5be0cd19; } // 处理消息扩展 void expandMessage(unsigned int message[SHA256_BLOCK_SIZE / 4], unsigned int W[64]) { for (int t = 0; t < 16; t++) { W[t] = message[t]; } for (int t = 16; t < 64; t++) { unsigned int s0 = sigma0(W[t - 15]); unsigned int s1 = sigma1(W[t - 2]); W[t] = W[t - 16] + s0 + W[t - 7] + s1; } } // 压缩函数 void compress(unsigned int hash[SHA256_HASH_SIZE / 4], unsigned int W[64]) { unsigned int a = hash[0]; unsigned int b = hash[1]; unsigned int c = hash[2]; unsigned int d = hash[3]; unsigned int e = hash[4]; unsigned int f = hash[5]; unsigned int g = hash[6]; unsigned int h = hash[7]; for (int t = 0; t < 64; t++) { unsigned int T1 = h + ch(e, f, g) + K[t] + W[t]; unsigned int T2 = sigma1(e) + maj(e, f, g); h = g; g = f; f = e; e = d + T1; d = c; c = b; b = a; a = T1 + T2; } hash[0] += a; hash[1] += b; hash[2] += c; hash[3] += d; hash[4] += e; hash[5] += f; hash[6] += g; hash[7] += h; } // 计算 SHA-256 哈希值 void sha256(unsigned char *message, size_t messageLength, unsigned char hash[SHA256_HASH_SIZE]) { // 计算填充后消息的长度 size_t paddedMessageLength = ((messageLength + 8 + SHA256_BLOCK_SIZE - 1) / SHA256_BLOCK_SIZE) * SHA256_BLOCK_SIZE; // 分配填充后的消息缓冲区 unsigned char *paddedMessage = (unsigned char *)malloc(paddedMessageLength); // 复制原始消息 memcpy(paddedMessage, message, messageLength); // 添加 1 比特 paddedMessage[messageLength] = 0x80; // 填充长度 long long bitLength = messageLength * 8; for (int i = 0; i < 8; i++) { paddedMessage[paddedMessageLength - 8 + i] = (bitLength >> (56 - 8 * i)) & 0xff; } // 初始化哈希值 unsigned int hashValue[SHA256_HASH_SIZE / 4]; initHash(hashValue); // 处理消息块 for (size_t i = 0; i < paddedMessageLength; i += SHA256_BLOCK_SIZE) { unsigned int messageBlock[SHA256_BLOCK_SIZE / 4]; for (int j = 0; j < SHA256_BLOCK_SIZE / 4; j++) { messageBlock[j] = (paddedMessage[i + 4 * j] << 24) | (paddedMessage[i + 4 * j + 1] << 16) | (paddedMessage[i + 4 * j + 2] << 8) | paddedMessage[i + 4 * j + 3]; } unsigned int W[64]; expandMessage(messageBlock, W); compress(hashValue, W); } // 将哈希值转换为字节数组 for (int i = 0; i < SHA256_HASH_SIZE / 4; i++) { hash[4 * i] = (hashValue[i] >> 24) & 0xff; hash[4 * i + 1] = (hashValue[i] >> 16) & 0xff; hash[4 * i + 2] = (hashValue[i] >> 8) & 0xff; hash[4 * i + 3] = hashValue[i] & 0xff; } free(paddedMessage); } int main() { unsigned char message[] = "Hello, World!"; unsigned char hash[SHA256_HASH_SIZE]; sha256(message, strlen((char *)message), hash); printf("SHA-256 Hash: "); for (int i = 0; i < SHA256_HASH_SIZE; i++) { printf("%02x", hash[i]); } printf("\n"); return 0; } ``` 这只是一个简单的示例实现,可能不满足所有的安全性和性能要求。在实际应用中,建议使用经过严格测试和优化的成熟库来进行 SHA-256 哈希计算。 [2024-10-29 10:05:13 | AI写代码神器 | 2394点数解答]