SHA-1(安全哈希算法实现)

如题,不知道sha-1的自己百度吧。

  1 #include <iostream>
  2 #include <vector>   //定义vector数组
  3 #include <string>   //记录消息
  4 using namespace std;
  5 
  6 const int NUM = 8;   //一个字由32比特(或者8个16进制数)
  7 const int BIT = 512;  //消息认证码要以512比特一组
  8 
  9 //字常量
 10 string H0 = "67452301";
 11 string H1 = "EFCDAB89";
 12 string H2 = "98BADCFE";
 13 string H3 = "10325476";
 14 string H4 = "C3D2E1F0";
 15 
 16 //定义SHA1(安全哈希算法)类
 17 class SHA1
 18 {
 19 public:
 20     //将一个字符串形式的字转化为vector数组
 21     vector<int> hex_into_dec(string word);
 22 
 23     //将vector转化为string字符串形式
 24     string num_into_message(vector<int> A);
 25 
 26     //两个字X和Y的逻辑"和"
 27     vector<int> word_AND(vector<int> A,vector<int> B);
 28 
 29     //两个字X和Y的逻辑"或"
 30     vector<int> word_OR(vector<int> A,vector<int> B);
 31 
 32     //两个字X和Y的逻辑"异或"
 33     vector<int> word_XOR(vector<int> A,vector<int> B);
 34 
 35     //两个字X和Y的逻辑"补"
 36     vector<int> word_COMPLEMENT(vector<int> A);
 37 
 38     //两个字X和Y的摸2^32整数加
 39     vector<int> word_ADD(vector<int> A,vector<int> B);
 40 
 41     //将字X循环左移s个位置
 42     vector<int> ROTL(vector<int> A,int s);
 43 
 44     //SHA-1的填充方案,我们设定msg由ASCII码组成
 45     vector<vector<int> > SHA_1_PAD(string msg);
 46 
 47     //将SHA-1压成以字为单位
 48     vector<vector<vector<int> > > compress(vector<vector<int> > result);
 49 
 50     //定义ft函数,每个ft函数都有B,C,D三个字作为输入,并产生一个字作为输出
 51     vector<int> Ft(int t,vector<int> B,vector<int> C,vector<int> D);
 52 
 53     //定义字常数K
 54     vector<int> K(int t);
 55 
 56     //开始进行SHA-1(安全Hash算法)的加密
 57     vector<vector<int> > SHA_1(string msg);
 58 
 59 };
 60 
 61 //将vector转化为string字符串形式
 62 string SHA1::num_into_message(vector<int> A)
 63 {
 64     int i;
 65     string msg = "";
 66     for(i = 0;i < A.size();i++)
 67     {
 68         if(A[i] >= 0 && A[i] <= 9)
 69             msg += '0' + A[i];
 70         else if(A[i] >= 10 && A[i] <= 15)
 71             msg += 'A' + (A[i] - 10);
 72     }
 73     return msg;
 74 }
 75 
 76 //将一个字符串形式的字转化为vector数组
 77 vector<int> SHA1::hex_into_dec(string word)
 78 {
 79     int i;
 80     vector<int> result(NUM,0);
 81     for(i = 0;i < NUM;i++)
 82     {
 83         if(word[i] >= '0' && word[i] <= '9')
 84         {
 85             result[i] = word[i] - '0';
 86         }
 87         else if(word[i] >= 'A' && word[i] <= 'F')
 88         {
 89             result[i] = 10 + word[i] - 'A';
 90         }
 91     }
 92     return result;
 93 }
 94 
 95 //两个字X和Y的逻辑"和"
 96 vector<int> SHA1::word_AND(vector<int> A,vector<int> B)
 97 {
 98     vector<int> result(NUM,0);
 99     int i;
100     for(i = 0;i < NUM;i++)
101     {
102         result[i] = A[i] & B[i];
103     }
104     return result;
105 }
106 
107 //两个字X和Y的逻辑"或"
108 vector<int> SHA1::word_OR(vector<int> A,vector<int> B)
109 {
110     vector<int> result(NUM,0);
111     int i;
112     for(i = 0;i < NUM;i++)
113     {
114         result[i] = A[i] | B[i];
115     }
116     return result;
117 }
118 
119 //两个字X和Y的逻辑"异或"
120 vector<int> SHA1::word_XOR(vector<int> A,vector<int> B)
121 {
122     vector<int> result(NUM,0);
123     int i;
124     for(i = 0;i < NUM;i++)
125     {
126         result[i] = A[i] ^ B[i];
127     }
128     return result;
129 }
130 
131 //两个字X和Y的逻辑"补"
132 vector<int> SHA1::word_COMPLEMENT(vector<int> A)
133 {
134     vector<int> result(NUM,0);
135     int i;
136     for(i = 0;i < NUM;i++)
137     {
138         result[i] = 15 - A[i];
139     }
140     return result;
141 }
142 
143 //两个字X和Y的摸2^32整数加
144 vector<int> SHA1::word_ADD(vector<int> A,vector<int> B)
145 {
146     vector<int> result(NUM,0);
147     int i;
148     for(i = NUM - 1;i >= 0;i--)
149     {
150         result[i] = A[i] + B[i];
151         if(i != 0)
152         {
153             int temp = result[i] / 16;
154             result[i-1] += temp;
155         }
156         result[i] %= 16;
157     }
158     return result;
159 }
160 
161 //将字X循环左移s个位置
162 vector<int> SHA1::ROTL(vector<int> A,int s)
163 {
164     vector<int> result = A;
165     vector<int> temp(NUM,0);
166     int i,j;
167     for(i = 0;i < s;i++)
168     {
169         for(j = NUM - 1;j >= 0;j--)
170         {
171             if(result[j] / 8 >= 1)
172             {
173                 temp[j] = 1;
174                 result[j] <<= 1;
175                 result[j] %= 16;
176                 if(j < NUM - 1)
177                     result[j] += temp[j + 1];
178             }
179             else if(result[j] / 8 == 0)
180             {
181                 temp[j] = 0;
182                 result[j] <<= 1;
183                 result[j] %= 16;
184             }
185         }
186         result[NUM - 1] += temp[0];
187     }
188     return result;
189 }
190 
191 //SHA-1的填充方案,我们设定msg由ASCII码组成
192 vector<vector<int> > SHA1::SHA_1_PAD(string msg)
193 {
194     int len = msg.length();
195     int bit_num = len * 8;
196     int i,j;
197     int num,lest = bit_num % 512;
198     if(lest != 0)   //看消息长度是否超过512字节,我们需要将它补成512的倍数
199         num = bit_num / 512 + 1;
200     else
201         num = bit_num / 512;
202     //首先我们以8位字节为一组保存到vector里面,512比特为一组,即一组里面有64位元素
203     vector<vector<int> > result;
204     result.resize(num);
205     for(i = 0;i < num;i++)
206     {
207         result[i].resize(64);
208     }
209     for(i = 0;i < num;i++)
210     {
211         for(j = 0;j < 64 && i * 64 + j < len;j++)
212         {
213             result[i][j] = msg[i * 64 + j];
214         }
215     }
216     //下面开始为未够512比特的消息分组进行补长度操作
217     if(lest != 0){
218     int x = num - 1,last_len = lest / 8;
219     result[x][last_len] = 128;   //先补一个"1"
220     for(i = last_len + 1;i < 56;i++)
221     {
222         result[x][i] = 0;
223     }
224     int last_l = lest;
225     j = 63;
226     while(j >= 56)
227     {
228         result[x][j] = last_l % 128;
229         last_l /= 128;
230         j--;
231     }
232     }
233     return result;
234 }
235 
236 //将SHA-1压成以字为单位(三维数组有点复杂)
237 vector<vector<vector<int> > > SHA1::compress(vector<vector<int> > result)
238 {
239     vector<vector<int> > rr;
240     rr.resize(result.size());
241     int i,j;
242     for(i = 0;i < rr.size();i++)
243     {
244         rr[i].resize(128);
245     }
246     for(i = 0;i < result.size();i++)
247     {
248         for(j = 0;j < result[i].size();j++)
249         {
250             rr[i][2 * j] = result[i][j] / 16;
251             rr[i][2 * j + 1] = result[i][j] % 16;
252         }
253     }
254     vector<vector<vector<int> > > rrr;
255     rrr.resize(result.size());
256     for(i = 0;i < rrr.size();i++)
257     {
258         rrr[i].resize(16);
259     }
260     for(i = 0;i < rrr.size();i++)
261     {
262         for(j = 0;j < 16;j++)
263         {
264             rrr[i][j].resize(8);
265         }
266     }
267     for(i = 0;i < rr.size();i++)
268     {
269         for(j = 0;j < rr[i].size();j++)
270         {
271             rrr[i][j / 8][j % 8] = rr[i][j];
272         }
273     }
274     return rrr;
275 }
276 
277 //定义ft函数,每个ft函数都有B,C,D三个字作为输入,并产生一个字作为输出
278 vector<int> SHA1::Ft(int t,vector<int> B,vector<int> C,vector<int> D)
279 {
280     vector<int> result;
281     if(t >= 0 && t <= 19)
282     {
283         vector<int> a1 = word_AND(B,C);
284         vector<int> a2 = word_AND(word_COMPLEMENT(B),D);
285         result = word_OR(a1,a2);
286     }
287     else if((t >= 20 && t <= 39) || (t >= 60 && t <= 79))
288     {
289         vector<int> a1 = word_XOR(B,C);
290         result = word_XOR(a1,D);
291     }
292     else if(t >= 40 && t <= 59)
293     {
294         vector<int> a1 = word_AND(B,C);
295         vector<int> a2 = word_AND(B,D);
296         vector<int> a3 = word_AND(C,D);
297         vector<int> a4 = word_OR(a1,a2);
298         result = word_OR(a4,a3);
299     }
300     return result;
301 }
302 
303 //定义字常数K
304 vector<int> SHA1::K(int t)
305 {
306     vector<int> result;
307     if(t >= 0 && t <= 19)
308     {
309         result = hex_into_dec("5A827999");
310     }
311     else if(t >= 20 && t <= 39)
312     {
313         result = hex_into_dec("6ED9EBA1");
314     }
315     else if(t >= 40 && t <= 59)
316     {
317         result = hex_into_dec("8F1BBCDC");
318     }
319     else if(t >= 60 && t <= 79)
320     {
321         result = hex_into_dec("CA62C1D6");
322     }
323     return result;
324 }
325 
326 //开始进行SHA-1(安全Hash算法)的加密
327 vector<vector<int> > SHA1::SHA_1(string msg)
328 {
329     vector<int> h0 = hex_into_dec(H0);
330     vector<int> h1 = hex_into_dec(H1);
331     vector<int> h2 = hex_into_dec(H2);
332     vector<int> h3 = hex_into_dec(H3);
333     vector<int> h4 = hex_into_dec(H4);
334     
335     vector<vector<int> > result1 = SHA_1_PAD(msg);
336     vector<vector<vector<int> > > result2 = compress(result1);
337     int n = result2.size();
338     int i,j;
339     for(i = 0;i < n;i++)
340     {
341         vector<vector<int> > W;
342         W.resize(80);
343         for(j = 0;j < 16;j++)
344         {
345             W[j] = result2[i][j];
346         }
347         for(j = 16;j < 80;j++)
348         {
349             vector<int> a1 = word_XOR(W[j-3],W[j-8]);
350             vector<int> a2 = word_XOR(a1,W[j-14]);
351             vector<int> a3 = word_XOR(a2,W[j-16]);
352             W[j] = ROTL(a3,1);
353         }
354         
355         //将string转化为vector数组
356         vector<int> A = hex_into_dec(H0);
357         vector<int> B = hex_into_dec(H1);
358         vector<int> C = hex_into_dec(H2);
359         vector<int> D = hex_into_dec(H3);
360         vector<int> E = hex_into_dec(H4);
361 
362         for(j = 0;j < 80;j++)
363         {
364             vector<int> a1 = ROTL(A,5);
365             vector<int> a2 = Ft(j,B,C,D);
366             vector<int> a3 = word_ADD(a1,a2);
367             vector<int> a4 = word_ADD(a3,E);
368             vector<int> a5 = word_ADD(a4,W[j]);
369             vector<int> temp = word_ADD(a5,K(j));
370             E = D;
371             D = C;
372             C = ROTL(B,30);
373             B = A;
374             A = temp;
375         }
376 
377         h0 = word_ADD(h0,A);
378         h1 = word_ADD(h1,B);
379         h2 = word_ADD(h2,C);
380         h3 = word_ADD(h3,D);
381         h4 = word_ADD(h4,E);
382     }
383 
384     //返回结果(H0||H1||H2||H3||H4)
385     vector<vector<int> > result;
386     result.push_back(h0);
387     result.push_back(h1);
388     result.push_back(h2);
389     result.push_back(h3);
390     result.push_back(h4);
391 
392     return result;
393 }
394 
395 int main()
396 {
397     SHA1 sha1;   //定义SHA1算法类
398     string message = "cryptographyisthepracticeandstudyoftechniquesforsecurecommunicationinthepresenceofthirdpartiesmoregenerallyitisaboutconstructingandanalyzingprotocolsthatovercometheinfluenceofadversariesandwhicharerelatedtovariousaspectsininformationsecuritysuchasdataconfidentialitydataintegrityauthenticationandnonrepudiationmoderncryptographyintersectsthedisciplinesofmathematicscomputerscienceandelectricalengineeringapplicationsofcryptographyincludeATMcardscomputerpasswordsandelectroniccommerce";
399     vector<vector<int> > result;
400     result = sha1.SHA_1(message);
401     cout << "消息为:" << endl << message << endl;
402     cout << "利用填充方案SHA-1-PAD给出对消息的填充,得出SHA-1(x)得:" << endl;
403     int i;
404     for(i = 0;i < result.size();i++)
405     {
406         cout << sha1.num_into_message(result[i]);
407     }
408     cout << endl;
409     return 0;
410 }

SHA-1(安全哈希算法实现)_第1张图片

就这样,请多多指教!

你可能感兴趣的:(sha)