SHA-.h
#ifndef _SHA_256_H
#define _SHA_256_H
#include<iostream>
using namespace std;
typedef unsigned int UInt32;
//六个逻辑函数
#define Conditional(x,y,z) ((x&y)^((~x)&z))
#define Majority(x,y,z) ((x&y)^(x&z)^(y&z))
#define LSigma_0(x) (ROTL(x,30)^ROTL(x,19)^ROTL(x,10))
#define LSigma_1(x) (ROTL(x,26)^ROTL(x,21)^ROTL(x,7))
#define SSigma_0(x) (ROTL(x,25)^ROTL(x,14)^SHR(x,3))
#define SSigma_1(x) (ROTL(x,15)^ROTL(x,13)^SHR(x,10))
//信息摘要结构
struct Message_Digest{
UInt32 H[];
};
//SHA256类
class SHA256
{
public:
SHA256(){INIT();};
~SHA256(){};
Message_Digest DEAL(UInt32 W[]);//处理512比特数据,返回信息摘要
private:
void INIT(); //初始杂凑值
UInt32 ROTR(UInt32 W,int n);//右旋转
UInt32 ROTL(UInt32 W,int n);//左旋转
UInt32 SHR(UInt32 W,int n); //右移位
private:
//信息摘要
Message_Digest MD;
}; #endif
SHA-256.cpp
#include"SHA-256.h"
//64个32比特字的常数(前64个素数的立方根小数前32位)
const UInt32 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,
};
//初始化杂凑值(前8个素数的平方根小数前32位)
void SHA256::INIT(){
MD.H[] = 0x6a09e667;
MD.H[] = 0xbb67ae85;
MD.H[] = 0x3c6ef372;
MD.H[] = 0xa54ff53a;
MD.H[] = 0x510e527f;
MD.H[] = 0x9b05688c;
MD.H[] = 0x1f83d9ab;
MD.H[] = 0x5be0cd19;
}
//处理512比特数据,返回信息摘要
Message_Digest SHA256::DEAL(UInt32 M[]){
int i;
UInt32 T1=,T2=;
UInt32 W[]={};
UInt32 A=,B=,C=,D=,E=,F=,G=,H=;
for(i=;i<;i++){
W[i] = M[i];
}
for(i=;i<;i++){
W[i] = SSigma_1(W[i-])+W[i-]+SSigma_0(W[i-])+W[i-];
}
A = MD.H[];
B = MD.H[];
C = MD.H[];
D = MD.H[];
E = MD.H[];
F = MD.H[];
G = MD.H[];
H = MD.H[];
cout<<"初始:";
cout<<hex<<A<<" "<<B<<" "<<C<<" "<<D<<" "<<E<<" "<<F<<" "<<G<<" "<<H<<endl;
for(i=;i<;i++){
T1 = H + LSigma_1(E) + Conditional(E, F, G) + K[i] + W[i];
T2 = LSigma_0(A) + Majority(A, B, C);
H = G;
G = F;
F = E;
E = D + T1;
D = C;
C = B;
B = A;
A = T1 + T2;
cout<<dec<<i<<":";
cout<<hex<<A<<" "<<B<<" "<<C<<" "<<D<<" "<<E<<" "<<F<<" "<<G<<" "<<H<<endl;
}
MD.H[]=(MD.H[]+A) & 0xFFFFFFFF;
MD.H[]=(MD.H[]+B) & 0xFFFFFFFF;
MD.H[]=(MD.H[]+C) & 0xFFFFFFFF;
MD.H[]=(MD.H[]+D) & 0xFFFFFFFF;
MD.H[]=(MD.H[]+E) & 0xFFFFFFFF;
MD.H[]=(MD.H[]+F) & 0xFFFFFFFF;
MD.H[]=(MD.H[]+G) & 0xFFFFFFFF;
MD.H[]=(MD.H[]+H) & 0xFFFFFFFF; return MD;
}
//右旋转
UInt32 SHA256::ROTR(UInt32 W,int n){
return ((W >> n) & 0xFFFFFFFF) | (W) << (-(n));
}
//左旋转
UInt32 SHA256::ROTL(UInt32 W,int n){
return ((W << n) & 0xFFFFFFFF) | (W) >> (-(n));
}
//右移位
UInt32 SHA256::SHR(UInt32 W,int n){
return ((W >> n) & 0xFFFFFFFF);
}
TEST.cpp
#include<iostream>
#include"SHA-256.h"
using namespace std; typedef unsigned int UInt32;
typedef unsigned __int64 UInt64;
typedef unsigned char UChar;
#define Max 1000//最大字符数
SHA256 sha256=SHA256();
Message_Digest M_D;
UInt32 W[Max/];//整型
UInt32 M[]; //存分组信息
//压缩+显示
void compress(){
int i;
M_D = sha256.DEAL(M);
cout<<"哈希值: ";
for(i=;i<;i++){
cout<<hex<<M_D.H[i]<<" ";
}
cout<<endl;
}
//添加填充位+添加长度
void PAD(UChar Y[Max]){
//x+1+d+l=|x|
UInt32 i,j;
UInt32 T1=,T2=,T3=,T4=;
UChar temp[Max]={};
UInt64 x = strlen((char *)Y);//数据长度
UInt32 d = abs(-x) % ; //填充长度
UInt32 n = (x+)/+; //分组数
UInt32 m = x%; //最后组数据长度
UInt32 l = ;
cout<<"数据长度x:"<<int(x)<<" ";
cout<<"填充长度d:"<<d<<" ";
cout<<"分组数量n:"<<n<<" ";
cout<<"最后长度m:"<<m<<endl;
//不填充
for(i=;i<x;i++){
temp[i] = Y[i];
}
//填充1次1000 0000
temp[x] = 0x80;
//填充d次0000 0000
for(i=x+;i<x+d+;i++){
temp[i] = 0x00;
}
//填充长度的63-0位
for(i=;i<=l;i++){
temp[(n*)-i] = (UChar)(*x>>(i-)*);
}
//无符号字符转换为无符号整型
for(i=;i<Max/;i++){
for(j=;j<;j++){
if(j==)
T1 = temp[*i+j];
if(j==)
T2 = temp[*i+j];
if(j==)
T3 = temp[*i+j];
if(j==)
T4 = temp[*i+j];
}
W[i] = (T1<<) + (T2<<) + (T3<<) +T4;
}
//显示16进制数据
cout<<"16进制数据:";
for(i=;i<n*;i++){
cout<<hex<<" "<<W[i];
}
cout<<endl;
//分组处理
for(i=;i<n;i++){
cout<<"分组处理:"<<i+<<endl;
for(j=;j<;j++){
M[j] = W[(i*)+j];
}
compress();//sha-256压缩
}
}
//主函数
int main(){
UChar Y[Max];
cout<<"请输入要加密的字符串(最大"<<Max<<"个):"<<endl;
cin>>Y;
PAD(Y); system("pause");
return ;
}