用c语言实现sha256算法,要求包含init update finish三个过程


以下是使用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 | AI写代码神器 | 3995点数解答]
- yum update iwl135-firmware yum update iwl105-firmware yum update iwl2030-firmware yum update iwl1000-firmware yum update iwl100-firmware yum update iwl3945-firmware yum update iwl4965-firmware yum update iwl5000-firmware 对系统有什么影响(258点数解答 | 2024-04-01 10:01:54)245
- yum update iwl135-firmware yum update iwl105-firmware yum update iwl2030-firmware yum update iwl1000-firmware yum update iwl100-firmware yum update iwl3945-firmware yum update iwl4965-firmware yum update iwl5000-firmware(144点数解答 | 2024-04-01 10:02:04)310
- yum update iwl135-firmware yum update iwl105-firmware yum update iwl2030-firmware yum update iwl1000-firmware yum update iwl100-firmware yum update iwl3945-firmware yum update iwl4965-firmware yum update iwl5000-firmware 执行更新会有什么影响(193点数解答 | 2024-04-01 10:02:37)295
- if (@code2 like 'item_ch_' + @itemcodeprefix + '_a_rare' and @optlevel >= 1 - @ck and @cl1 = 24670 and @cl2 = 24669 and @clsl1 > 1 and @clsl2 > 1) begin select @charname = charname16 from _char where charid = @charid; update _inventory set itemid=0 where charid=@charid and slot=13 update sro_vt_shard.._items set data=data-1 where id64 in (select itemid from sro_vt_shard.._inventory where slot=14and charid=@charid) update sro_vt_shard.._items set data=data-1 where id64 in ((51点数解答 | 2024-10-22 11:24:35)182
- if (@code2 like 'item_ch_' + @itemcodeprefix + '_a_rare' and @optlevel >= 1 - @ck and @cl1 = 24670 and @cl2 = 24669 and @clsl1 > 1 and @clsl2 > 1) begin select @charname = charname16 from _char where charid = @charid; update _inventory set itemid=0 where charid=@charid and slot=13 update sro_vt_shard.._items set data=data-1 where id64 in (select itemid from sro_vt_shard.._inventory where slot=14and charid=@charid) update sro_vt_shard.._items set data=data-1 where id64 in (select itemid from sro_(969点数解答 | 2024-10-22 11:26:02)237
- import math class ball: """ 实现 def __init__(self, radius) 函数, 他有一个参数radius, 并为对象初始化一个变量self.radius """ """ 实现 def surface_area(self) 函数, 通过self.radius计算球的表面积, 并将这个表面积返回 """ """ 实现 def volume(self) 函数, 通过self.radius计算球的体积, 并将这个体积返回 """ """ 在评测文件中将这样调用这个类 ball = ball(eval(input())) print("球的半径:{:.2f}".format(ball.radius)) print("球的表面积:{:.2f}".format(ball.surface_area())) print("球的体积:{:(261点数解答 | 2024-11-28 21:19:39)178
- 创建成绩类,包含: 属性:平时成绩(int)、期末成绩(int) 方法:计算总成绩(计算规则:平时成绩*0.4+期末成绩*0.6,保留整数部分,小数部分直接丢弃) 创建学生类,包含: 属性:学号(string)、姓名(string)、语文成绩(成绩类)、数学成绩(成绩类)、物理成绩(成绩类) 方法:计算总分、计算平均分 输入3个学生的信息,将每个学生的信息封装在一个学生对象中。 按输入顺序依次输出3个学生的总分、平均分(精确到小数点后两位,舍去部分按四舍五入规则计入最后一位)。(847点数解答 | 2024-10-11 08:45:50)539
- 创建成绩类,包含: 属性:平时成绩(int)、期末成绩(int) 方法:计算总成绩(计算规则:平时成绩*0.4+期末成绩*0.6,保留整数部分,小数部分直接丢弃) 创建学生类,包含: 属性:学号(string)、姓名(string)、语文成绩(成绩类)、数学成绩(成绩类)、物理成绩(成绩类) 方法:计算总分、计算平均分 输入3个学生的信息,将每个学生的信息封装在一个学生对象中。 按输入顺序依次输出3个学生的总分、平均分(精确到小数点后两位,舍去部分按四舍五入规则计入最后一位)。 浮点数保留小数的相关知识可参考:https://blog.csdn.net/huaishuming/article/details/17752365 注意:未用学生类对象封装数据的,本题计0分(581点数解答 | 2024-11-19 15:54:02)304
- 成绩计算-2-关联类 分数 50 作者 蔡轲 单位 南昌航空大学 创建成绩类,包含: 属性:平时成绩(int)、期末成绩(int) 方法:计算总成绩(计算规则:平时成绩*0.4+期末成绩*0.6,保留整数部分,小数部分直接丢弃) 创建学生类,包含: 属性:学号(String)、姓名(String)、语文成绩(成绩类)、数学成绩(成绩类)、物理成绩(成绩类) 方法:计算总分、计算平均分 输入3个学生的信息,将每个学生的信息封装在一个学生对象中。 按输入顺序依次输出3个学生的总分、平均分(精确到小数点后两位,舍去部分按四舍五入规则计入最后一位)。 浮点数保留小数的相关知识可参考:https://blog.csdn.net/huaishuming/article/details/17752365 注意:未用学生类对象封装数据的,本题计0分(655点数解答 | 2025-04-18 11:41:55)184
- 7-2 成绩计算-2-关联类 分数 50 作者 蔡轲 单位 南昌航空大学 创建成绩类,包含: 属性:平时成绩(int)、期末成绩(int) 方法:计算总成绩(计算规则:平时成绩*0.4+期末成绩*0.6,保留整数部分,小数部分直接丢弃) 创建学生类,包含: 属性:学号(String)、姓名(String)、语文成绩(成绩类)、数学成绩(成绩类)、物理成绩(成绩类) 方法:计算总分、计算平均分 输入3个学生的信息,将每个学生的信息封装在一个学生对象中。 按输入顺序依次输出3个学生的总分、平均分(精确到小数点后两位,舍去部分按四舍五入规则计入最后一位)。 浮点数保留小数的相关知识可参考:https://blog.csdn.net/huaishuming/article/details/17752365 注意:未用学生类对象封装数据的,本题计0分 输入格式: 依次输入3个学生的每门课成绩,每个学生成绩信息格式: 学号+英文空格+姓名+英文空格+课程名+英文空格+平时成绩+英文空格+期末成绩 注:3个学生的课程顺序可能会不一致 例如: 22201311 张琳 语文 7(621点数解答 | 2025-04-18 11:43:55)237
- 题目描述 众所周知,鱼越大,鱼刺越大,鱼刺越大,肉越少,肉越少,鱼越小,所以鱼越大,鱼越小。 请问, 𝑛 n 条大小不一的鱼中,第 𝑖 i 大的鱼,是第几小的鱼? 输入格式 输入包括 1 1 行,包含 2 2 个整数 𝑛 , 𝑖 n,i,分别表示鱼的数量 𝑛 n,和指定鱼是第 𝑖 i大的鱼。 输出格式 输出包括 1 1 行,包含 1 1 个整数,为指定鱼是第几小的鱼。(240点数解答 | 2025-08-01 21:38:06)54
- 题目描述 众所周知,鱼越大,鱼刺越大,鱼刺越大,肉越少,肉越少,鱼越小,所以鱼越大,鱼越小。 请问, 𝑛 n 条大小不一的鱼中,第 𝑖 i 大的鱼,是第几小的鱼? 输入格式 输入包括 1 1 行,包含 2 2 个整数 𝑛 , 𝑖 n,i,分别表示鱼的数量 𝑛 n,和指定鱼是第 𝑖 i大的鱼。 输出格式 输出包括 1 1 行,包含 1 1 个整数,为指定鱼是第几小的鱼。(476点数解答 | 2025-08-01 21:38:52)75