一、原理
SHA-256 是一種加密哈希函數(shù),旨在將任意大小的數(shù)據(jù)映射到一個固定大小的哈希值,通常是 256 位(32 字節(jié))。它屬于 SHA-2(安全哈希算法 2)家族,旨在提供更高的安全性。
SHA-256 的設(shè)計原則包括以下關(guān)鍵步驟:文章來源:http://www.zghlxwxcb.cn/news/detail-854603.html
- 消息填充:輸入消息的位數(shù)必須是 512 的倍數(shù)。因此,第一步是對輸入消息進行填充。填充包括在消息末尾附加一個 '1',然后追加足夠的零,使消息長度對 512 取模后余 448。
- 追加消息長度:填充后,將原始的 64 位消息長度追加到消息的末尾。這確保了哈希值受到消息長度的影響,增強了安全性。
- 哈希值的初始化:SHA-256 使用 8 個 32 位字作為初始哈希值。這些值是使用前 8 個素數(shù)的平方根的小數(shù)部分來設(shè)置的。
- 消息分塊:對填充后的消息分為 512 位的塊,每個塊包含 16 個 32 位字。
- 消息調(diào)度:依次處理每個塊,生成 64 個擴展的 32 位字。這些字大部分基于之前的字和哈希值,通過一系列邏輯函數(shù)和位運算計算得出。
- 壓縮函數(shù):SHA-256 使用一種包含 64 輪的壓縮函數(shù),每一輪應(yīng)用不同的邏輯函數(shù)和常數(shù)。每一輪都會修改哈希值的不同部分,引入新的數(shù)據(jù)。
- 最終哈希值:處理完所有塊后,最終的 8 個 32 位字被連接起來形成 256 位的哈希值。
二、C/C++實現(xiàn)
SHA-256.h
#pragma once
#ifndef SHA_256_H
#define SHA_256_H
#include <stdlib.h>
#include <stdint.h>
typedef struct hash_context {
uint8_t buffer[64];
uint32_t state[8];
uint32_t total[2];
} hash_context;
void hash_start(hash_context* ctx);
void hash_update(hash_context* ctx, uint8_t* input, size_t ilen);
void hash_finish(hash_context* ctx, uint8_t* output);
static void sha256_transform(hash_context* ctx, const uint8_t data[]);
#endif // SHA_256_H
SHA-256.cpp
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <time.h>
#include <cstdlib>
#include "SHA-256.h"
using namespace std;
typedef unsigned int uint32_t;
typedef unsigned char uint8_t;
//typedef struct hash_context {
// uint8_t buffer[64];
// uint32_t state[8];
// uint32_t total[2];
//} hash_context;
// SHA-256 算法的宏定義
#define ROTRIGHT(word, bits) (((word) >> (bits)) | ((word) << (32 - (bits))))
#define CH(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
#define MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
#define EP0(x) (ROTRIGHT(x, 2) ^ ROTRIGHT(x, 13) ^ ROTRIGHT(x, 22))
#define EP1(x) (ROTRIGHT(x, 6) ^ ROTRIGHT(x, 11) ^ ROTRIGHT(x, 25))
#define SIG0(x) (ROTRIGHT(x, 7) ^ ROTRIGHT(x, 18) ^ ((x) >> 3))
#define SIG1(x) (ROTRIGHT(x, 17) ^ ROTRIGHT(x, 19) ^ ((x) >> 10))
// SHA-256 常量
static 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
};
函數(shù)聲明
//void hash_start(hash_context* ctx);
//void hash_update(hash_context* ctx, uint8_t* input, size_t ilen);
//void hash_finish(hash_context* ctx, uint8_t* output);
//static void sha256_transform(hash_context* ctx, const uint8_t data[]);
static void sha256_transform(hash_context* ctx, const uint8_t data[]) {
uint32_t a, b, c, d, e, f, g, h, i, j, t1, t2, m[64];
for (i = 0, j = 0; i < 16; ++i, j += 4)
m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]);
for (; i < 64; ++i)
m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16];
a = ctx->state[0];
b = ctx->state[1];
c = ctx->state[2];
d = ctx->state[3];
e = ctx->state[4];
f = ctx->state[5];
g = ctx->state[6];
h = ctx->state[7];
for (i = 0; i < 64; ++i) {
t1 = h + EP1(e) + CH(e, f, g) + K[i] + m[i];
t2 = EP0(a) + MAJ(a, b, c);
h = g;
g = f;
f = e;
e = d + t1;
d = c;
c = b;
b = a;
a = t1 + t2;
}
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;
}
void hash_start(hash_context* ctx) {
ctx->state[0] = 0x6a09e667;
ctx->state[1] = 0xbb67ae85;
ctx->state[2] = 0x3c6ef372;
ctx->state[3] = 0xa54ff53a;
ctx->state[4] = 0x510e527f;
ctx->state[5] = 0x9b05688c;
ctx->state[6] = 0x1f83d9ab;
ctx->state[7] = 0x5be0cd19;
ctx->total[0] = 0;
ctx->total[1] = 0;
}
void hash_update(hash_context* ctx, uint8_t* input, size_t ilen) {
size_t fill;
uint32_t left;
if (ilen == 0)
return;
left = ctx->total[0] & 0x3F;
fill = 64 - left;
ctx->total[0] += (uint32_t)ilen;
ctx->total[0] &= 0xFFFFFFFF;
if (ctx->total[0] < (uint32_t)ilen)
ctx->total[1]++;
if (left && ilen >= fill) {
memcpy((void*)(ctx->buffer + left), input, fill);
sha256_transform(ctx, ctx->buffer);
input += fill;
ilen -= fill;
left = 0;
}
while (ilen >= 64) {
sha256_transform(ctx, input);
input += 64;
ilen -= 64;
}
if (ilen > 0) {
memcpy((void*)(ctx->buffer + left), input, ilen);
}
}
void hash_finish(hash_context* ctx, uint8_t* output) {
uint32_t last, padn;
uint32_t high, low;
uint8_t msglen[8];
high = (ctx->total[0] >> 29) | (ctx->total[1] << 3);
low = (ctx->total[0] << 3);
msglen[0] = (uint8_t)(high >> 24);
msglen[1] = (uint8_t)(high >> 16);
msglen[2] = (uint8_t)(high >> 8);
msglen[3] = (uint8_t)(high);
msglen[4] = (uint8_t)(low >> 24);
msglen[5] = (uint8_t)(low >> 16);
msglen[6] = (uint8_t)(low >> 8);
msglen[7] = (uint8_t)(low);
last = ctx->total[0] & 0x3F;
padn = (last < 56) ? (56 - last) : (120 - last);
hash_update(ctx, (uint8_t*)"\x80", 1);
hash_update(ctx, (uint8_t*)(msglen + 1), padn - 1);
hash_update(ctx, msglen, 8);
for (int i = 0; i < 8; i++) {
output[i * 4] = (uint8_t)(ctx->state[i] >> 24);
output[i * 4 + 1] = (uint8_t)(ctx->state[i] >> 16);
output[i * 4 + 2] = (uint8_t)(ctx->state[i] >> 8);
output[i * 4 + 3] = (uint8_t)(ctx->state[i]);
}
}
/************************** 宏定義 **************************/
//#define DATA_SIZE 1073741824
#define DATA_SIZE 1073741824ULL // 將數(shù)據(jù)大小定義為 unsigned long long
#define ROUNDS 1ULL // 將循環(huán)次數(shù)定義為 unsigned long long
int main()
{
hash_context ctx;
uint8_t hash[32]; // SHA-256 輸出長度為 32 字節(jié)
clock_t start, end;
// 初始化隨機數(shù)生成器
srand(static_cast<unsigned int>(time(NULL)));
uint8_t* data = (uint8_t*)malloc(DATA_SIZE); // 分配數(shù)據(jù)緩沖區(qū)
// 生成隨機數(shù)據(jù)填充到 data 數(shù)組中
for (unsigned long long i = 0; i < DATA_SIZE; i++) // 使用 unsigned long long
{
data[i] = rand() & 0xFF;
}
// 開始計時
start = clock();
for (unsigned long long i = 0; i < ROUNDS; i++) // 使用 unsigned long long
{
hash_start(&ctx);
hash_update(&ctx, data, DATA_SIZE);
hash_finish(&ctx, hash);
}
// 結(jié)束計時
end = clock();
// 釋放分配的內(nèi)存
free(data);
// 計算總運行時間和每秒處理的數(shù)據(jù)量
double time = (double)(end - start) / CLOCKS_PER_SEC;
double computing_speed = (DATA_SIZE * ROUNDS * (unsigned long long)8 / 1000 / 1000) / time;
printf("運行時間: %f seconds\n", time);
printf("運算速度: %f Mbps\n", computing_speed);
return 0;
}
三、運行結(jié)果
文章來源地址http://www.zghlxwxcb.cn/news/detail-854603.html
到了這里,關(guān)于SHA-256算法的原理與C/C++實現(xiàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!