#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<stack>
using namespace std;
#define NUM 8 //一个字等价于8个16进制数
#define HEX 16 //16进制
#define BIT 512 //消息认证码512 bite一组
class SHA_1{
public:
//辅助函数
char hexConvert(int a, int b, int c, int d); //把一个四位二进制串转为一个十六进制字符
string binaryConvert(char a); //把一个十六进制字符转为四位二进制串
vector<int> sysConvert(const unsigned long long &a, int sys, int length); //把整数a转换为长度为length的sys进制向量形式
vector<int> char_to_hex(const string &word); //十六进制符号串(字)转十六进制整数串
string hex_to_char(const vector<int> &a); //十六进制整数串转十六进制符号串
string char_to_binary(const char a); //字符转八位二进制串
//运算函数
string w_AND(const string &a, const string &b); //字,与操作
string w_OR(const string &a, const string &b); //字,或操作
string w_XOR(const string &a, const string &b); //字,异或
string w_CPL(const string &a); //字,补
string w_ADD(const string &a, const string &b); //字,模2^32整数加
string ROTL(const string &a, int s); //a循环左移s个位置(0<= s <=31),a不改变
string Ft(int t, const string &B, const string &C, const string &D);
string K(int t); //字常数K
//主要操作函数
vector<vector<int> > SHA_1_PAD(const string &b_msg); //填充函数,输入为消息的二进制串
vector<vector<string> > divide(const vector<vector<int> > &result); //将填充后的消息分成以字(八个十六进制数)为单位的串联
string result_SHA_1(const string &msg); //获得消息摘要
private:
string H0 = "67452301"; //调用result_SHA_1函数后这些值会改变
string H1 = "EFCDAB89";
string H2 = "98BADCFE";
string H3 = "10325476";
string H4 = "C3D2E1F0";
};
//辅助函数
char SHA_1::hexConvert(int a, int b, int c, int d){ //把一个四位二进制串转为一个十六进制字符
if(a == 0 && b == 0 && c == 0 && d == 0)
return '0';
else if(a == 0 && b == 0 && c == 0 && d == 1)
return '1';
else if(a == 0 && b == 0 && c == 1 && d == 0)
return '2';
else if(a == 0 && b == 0 && c == 1 && d == 1)
return '3';
else if(a == 0 && b == 1 && c == 0 && d == 0)
return <
安全hash算法,sha-1的C++实现
最新推荐文章于 2024-08-14 17:17:55 发布