首页 > 代码库 > SHA-1算法

SHA-1算法

SHA-1.h
#ifndef _SHA1_H
#define _SHA1_H
#include<iostream>
using namespace std;
//4个函数
#define f1(B,C,D) ((B&C)|((~B)&D))
#define f2(B,C,D) (B^C^D)
#define f3(B,C,D) ((B&C)|(B&D)|(C&D))
#define f4(B,C,D) (B^C^D)
typedef unsigned int UInt32;
//信息摘要结构体
struct Message_Digest{
    UInt32 H[5];
};
//SHA-1类
class SHA1
{
public:
    SHA1(){INIT();};
    ~SHA1(){};
    Message_Digest DEAL(UInt32 W[16]);//以512bit块为单位处理
private:
    void INIT();                //初始杂凑值
    UInt32 ROTL(int k,UInt32 W);//循环左移k比特给定的32比特字
    //信息摘要
    Message_Digest MD;
};

#endif

SHA-1.cpp

#include"SHA-1.h"
//
const UInt32 K[4] = {0x5A827999,0x6ED9EBA1,0x8F1BBCDC,0xCA62C1D6};
//初始化
void SHA1::INIT(){
    MD.H[0] = 0x67452301;
    MD.H[1] = 0xefcdab89;
    MD.H[2] = 0x98badcfe;
    MD.H[3] = 0x10325476;
    MD.H[4] = 0xc3d2e1f0;
}
//
Message_Digest SHA1::DEAL(UInt32 M[16]){
    int i;
    UInt32 temp;
    UInt32 W[80];
    UInt32 A,B,C,D,E;
    INIT();//每次都初始化
    for(i=0;i<16;i++){
        W[i] = M[i];
    }
    for(i=16;i<80;i++){
        W[i] = ROTL(1,(W[i-3]^W[i-8]^W[i-14]^W[i-16]));
    }
    //初始寄存器
    A = MD.H[0];
    B = MD.H[1];
    C = MD.H[2];
    D = MD.H[3];
    E = MD.H[4];
    cout<<"初始:"<<hex<<A<<" "<<B<<" "<<C<<" "<<D<<" "<<E<<endl;
    for(i=0;i<20;i++){
        temp = ROTL(5,A) + f1(B,C,D) + E + W[i] + K[0];
        temp &= 0xFFFFFFFF;
        E = D;
        D = C;
        C = ROTL(30,B);
        B = A;
        A = temp;
        cout<<dec<<i<<":";
        cout<<hex<<A<<" "<<B<<" "<<C<<" "<<D<<" "<<E<<endl;
    }
    for(i=20;i<40;i++){
        temp = ROTL(5,A) + f2(B,C,D) + E + W[i] + K[1];
        temp &= 0xFFFFFFFF;
        E = D;
        D = C;
        C = ROTL(30,B);
        B = A;
        A = temp;
        cout<<dec<<i<<":";
        cout<<hex<<A<<" "<<B<<" "<<C<<" "<<D<<" "<<E<<endl;
    }
    for(i=40;i<60;i++){
        temp = ROTL(5,A) + f3(B,C,D) + E + W[i] + K[2];
        temp &= 0xFFFFFFFF;
        E = D;
        D = C;
        C = ROTL(30,B);
        B = A;
        A = temp;
        cout<<dec<<i<<":";
        cout<<hex<<A<<" "<<B<<" "<<C<<" "<<D<<" "<<E<<endl;
    }
    for(i=60;i<80;i++){
        temp = ROTL(5,A) + f4(B,C,D) + E + W[i] + K[3];
        temp &= 0xFFFFFFFF;
        E = D;
        D = C;
        C = ROTL(30,B);
        B = A;
        A = temp;
        cout<<dec<<i<<":";
        cout<<hex<<A<<" "<<B<<" "<<C<<" "<<D<<" "<<E<<endl;
    }
    MD.H[0] = (MD.H[0]+A) & 0xFFFFFFFF;
    MD.H[1] = (MD.H[1]+B) & 0xFFFFFFFF;
    MD.H[2] = (MD.H[2]+C) & 0xFFFFFFFF;
    MD.H[3] = (MD.H[3]+D) & 0xFFFFFFFF;
    MD.H[4] = (MD.H[4]+E) & 0xFFFFFFFF;

    return MD;
}
//循环左移
UInt32 SHA1::ROTL(int k,UInt32 W){
    return ((W << k) & 0xFFFFFFFF) | (W) >> (32-(k));
}

TEST.cpp

#include<iostream>
#include"SHA-1.h"
using namespace std;
//
typedef unsigned int UInt32;
typedef unsigned __int64 UInt64;
typedef unsigned char UChar;
#define Max 1000//最大字符数
SHA1 sha1=SHA1();
Message_Digest M_D;
UInt32 W[Max/4];//整型
UInt32 M[16];   //存分组信息
//压缩+显示
void compress(){

    M_D = sha1.DEAL(W);
    cout<<"哈希值: ";
    cout<<hex<<M_D.H[0]<<" ";
    cout<<hex<<M_D.H[1]<<" ";
    cout<<hex<<M_D.H[2]<<" ";
    cout<<hex<<M_D.H[3]<<" ";
    cout<<hex<<M_D.H[4];
    cout<<endl;
}
//添加填充位+添加长度
void PAD(UChar Y[Max]){
    //x+1+d+l=|x|
    UInt32 i,j;
    UInt32 T1=0,T2=0,T3=0,T4=0;
    UChar temp[Max]={0};
    UInt64 x = strlen((char *)Y);//数据长度
    UInt32 d = abs(55-x) % 64;   //填充长度
    UInt32 n = (x+8)/64+1; //分组数
    UInt32 m = x%64;       //最后组数据长度
    UInt32 l = 8;      
    cout<<"数据长度x:"<<int(x)<<" ";
    cout<<"填充长度d:"<<d<<" ";
    cout<<"分组数量n:"<<n<<" ";
    cout<<"最后长度m:"<<m<<endl;
    //不填充
    for(i=0;i<x;i++){
        temp[i] = Y[i];
    }
    //填充1次1000 0000
        temp[x] = 0x80;
    //填充d次0000 0000
    for(i=x+1;i<x+d+1;i++){
        temp[i] = 0x00;
    }
    //填充长度的63-0位
    for(i=1;i<=l;i++){
        temp[(n*64)-i] = (UChar)(8*x>>(i-1)*8);
    }
    //无符号字符转换为无符号整型
    for(i=0;i<Max/4;i++){
        for(j=0;j<4;j++){
            if(j==0)
                T1 = temp[4*i+j];
            if(j==1)
                T2 = temp[4*i+j];
            if(j==2)
                T3 = temp[4*i+j];
            if(j==3)
                T4 = temp[4*i+j];
        }
        W[i] = (T1<<24) + (T2<<16) + (T3<<8) +T4;
    }
    //显示16进制数据
    cout<<"16进制数据:";
    for(i=0;i<n*16;i++){
        cout<<hex<<" "<<W[i];
    }
    cout<<endl;
    //分组处理
    for(i=0;i<n;i++){
        cout<<"分组处理:"<<i+1<<endl;
        for(j=0;j<16;j++){
            M[j] = W[(i*16)+j];
        }
        compress();//sha-1压缩
    }
}
//主函数
int main(){
    UChar Y[Max];
    cout<<"请输入要加密的字符串(最大"<<Max<<"个):"<<endl;
    cin>>Y;
    PAD(Y);

    system("pause");
    return 0;
}

 

SHA-1算法