版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、SHA1算法源代碼2009-10-16 17:51sha1.h=#ifndef _SHA1_H_#define _SHA1_H_/*/* sha1.h */#ifndef _SHA_enum_#define _SHA_enum_enum. shaSuccess = 0, shaNull, /*/* Null pointer parameter */ shaI
2、nputTooLong, /*/* input data too long */ shaStateError /*/* called Input after Result */;#endif#define SHA1HashSize 20typedef struct SHA1Context. DWORD Intermediate_HashSHA1HashSize/4; / Message Digest
3、60; DWORD Length_Low; / Message length in bits DWORD Length_High; / Message length in bits int Message_Block_Index;
4、; / Index into message block array unsigned char Message_Block64; / 512-bit message blocks int Computed; / Is the digest computed? i
5、nt Corrupted; / Is the message digest corrupted? SHA1Context;/ Function Prototypes CString GetSHA1String(CString sSource);int SHA1Reset(SHA1Context *);int SHA1Input(SHA1Context *, const unsigned char *, unsigne
6、d int);int SHA1Result(SHA1Context *, unsigned char Message_DigestSHA1HashSize);CString Sha1toBase32(const unsigned char *);#endif=#include "sha1.h"/ Define the SHA1 circular left shift macro #define SHA1CircularShift(bits, word) (word) << (bits) | (word) >> (32-(bits)/ Local Fu
7、nction Prototyptes */void SHA1PadMessage(SHA1Context *);void SHA1ProcessMessageBlock(SHA1Context *);int SHA1Reset(SHA1Context *c). if (!c) return shaNull; c->Length_Low
8、60; = 0; c->Length_High = 0; c->Message_Block_Index = 0; c->Intermediate_Hash0 = 0x67452301; c->Interm
9、ediate_Hash1 = 0xEFCDAB89; c->Intermediate_Hash2 = 0x98BADCFE; c->Intermediate_Hash3 = 0x10325476; c->Intermediate_Hash4 = 0xC3D2E1F0; c->Computed = 0;
10、; c->Corrupted = 0; return shaSuccess;int SHA1Result( SHA1Context *c, unsigned char Message_DigestSHA1HashSize). int i; if (!c | !Message_Digest) return shaNull; if (c->
11、;Corrupted) return c->Corrupted; if (!c->Computed) . SHA1PadMessage(c); for(i = 0; i<64; c->Message_Block+i = 0)
12、160; ; c->Length_Low = 0; /*/* and clear length */ c->Length_High = 0; c->Computed = 1;
13、 for(i = 0; i < SHA1HashSize; +i) Message_Digesti = c->Intermediate_Hashi>>2 >> 8 * (3 - (i & 0x03); return shaSuccess;int SHA1Input(SHA1Context *context, const unsigned char *message_array, un
14、signed length). if (!length) return shaSuccess; if (!context | !message_array) return shaNull; if (context->Computed) .
15、0; context->Corrupted = shaStateError; return shaStateError; if (context->Corrupted) return context->Corrupted; while(length- &&am
16、p; !context->Corrupted) . context->Message_Blockcontext->Message_Block_Index+ = (*message_array & 0xFF); context->Length_Low += 8; if (conte
17、xt->Length_Low = 0) . context->Length_High+; if (context->Length_High = 0) &
18、#160; . /*/* Message is too long */ context->Corrupted = 1;
19、0; if (context->Message_Block_Index = 64) SHA1ProcessMessageBlock(context); mes
20、sage_array+; return shaSuccess;void SHA1ProcessMessageBlock(SHA1Context *context). const DWORD K = . 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6 ; int t; DWORD temp;
21、; DWORD W80; DWORD A, B, C, D, E; /*/* * Initialize the first 16 words in the array W */ for(t = 0; t < 16; t+) . Wt = context->Message_Bl
22、ockt * 4 << 24; Wt |= context->Message_Blockt * 4 + 1 << 16; Wt |= context->Message_Blockt * 4 + 2 << 8; Wt |= context->Message_Blockt * 4 + 3;
23、0; for(t = 16; t < 80; t+) Wt = SHA1CircularShift(1,Wt-3 Wt-8 Wt-14 Wt-16); A = context->Intermediate_Hash0; B = context->Intermediate_Hash1; C = context->Interm
24、ediate_Hash2; D = context->Intermediate_Hash3; E = context->Intermediate_Hash4; for(t = 0; t < 20; t+) . temp = SHA1CircularShift(5,A) +
25、; (B & C) | (B) & D) + E + Wt + K0; E = D; D = C; C = SHA1CircularShift(30,B); B = A;
26、60; A = temp; for(t = 20; t < 40; t+) . temp = SHA1CircularShift(5,A) + (B C D) + E + Wt + K1; E = D;
27、60; D = C; C = SHA1CircularShift(30,B); B = A; A = temp; for(t = 40; t < 60; t+) .
28、0; temp = SHA1CircularShift(5,A) + (B & C) | (B & D) | (C & D) + E + Wt + K2; E = D; D = C;
29、; C = SHA1CircularShift(30,B); B = A; A = temp; for(t = 60; t < 80; t+) . temp = SHA1CircularShift(5,A) + (B C
30、 D) + E + Wt + K3; E = D; D = C; C = SHA1CircularShift(30,B); B = A; A = temp;
31、 context->Intermediate_Hash0 += A; context->Intermediate_Hash1 += B; context->Intermediate_Hash2 += C; context->Intermediate_Hash3 += D; context->Intermediate_Hash4 += E;
32、 context->Message_Block_Index = 0;void SHA1PadMessage(SHA1Context *context). if (context->Message_Block_Index > 55) . context->Message_Blockcontext->Message_Block_Index+ = 0x80;
33、 while(context->Message_Block_Index < 64) context->Message_Blockcontext->Message_Block_Index+ = 0; SHA1ProcessMessageBlock(context);
34、 while(context->Message_Block_Index < 56) context->Message_Blockcontext->Message_Block_Index+ = 0; else . context->M
35、essage_Blockcontext->Message_Block_Index+ = 0x80; while(context->Message_Block_Index < 56) context->Message_Blockcontext->Message_Block_Index+ = 0;
36、; /*/* * Store the message length as the last 8 octets */ context->Message_Block56 = context->Length_High >> 24; context->Message_Block57 = context->Length_High >> 16; context->
37、Message_Block58 = context->Length_High >> 8; context->Message_Block59 = context->Length_High; context->Message_Block60 = context->Length_Low >> 24; context->Message_Block61 = context->Length_Low >> 16;
38、 context->Message_Block62 = context->Length_Low >> 8; context->Message_Block63 = context->Length_Low; SHA1ProcessMessageBlock(context);/ Convert 5 Bytes to 8 Bytes Base32void _Sha1toBase32(unsigned char *out, const unsigned char *in).
39、 const char *Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" out0 = Table(in0 >> 3) ) & 0x1F; out1 = Table(in0 << 2) | (in1 >> 6) &
40、 0x1F; out2 = Table(in1 >> 1) ) & 0x1F; out3 = Table(in1 << 4) | (in2 >> 4) & 0x1F; out4 = Table(in2 << 1) | (in3 >> 7) & 0x1F; out5 = Table(in3 >> 2) ) &a
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 外貿(mào)出口購銷合同范例
- 房屋裝修責(zé)任合同范例
- 拆遷活動板房合同模板
- 房子裝修安全合同模板
- 境外股權(quán)收購合同模板簡易
- 承包外包服務(wù)合同范例
- 學(xué)校建筑樓房合同范例
- 加工項目轉(zhuǎn)讓合同范例
- 房產(chǎn)代理合同范例
- 房屋補貼合同范例
- 重度子癇前期、胎盤早剝急救演練
- 老年社區(qū)獲得性肺炎的幾個熱點問題專家講座
- 建筑消防工程施工操作規(guī)程
- 大學(xué)生職業(yè)規(guī)劃4篇匯編
- GB/T 42461-2023信息安全技術(shù)網(wǎng)絡(luò)安全服務(wù)成本度量指南
- (完整word版)扣字詞匯124
- 2023屆廣東省廣州市高三一模語文現(xiàn)代文閱讀小說《給我一枝槍》講評課件
- 中職世界歷史全一冊教案
- 毛栗煤礦 礦業(yè)權(quán)價款計算結(jié)果的報告
- Q-CR 783.1-2021 鐵路通信網(wǎng)絡(luò)安全技術(shù)要求 第1部分:總體技術(shù)要求
- 2023年黑龍江建筑職業(yè)技術(shù)學(xué)院高職單招(數(shù)學(xué))試題庫含答案解析
評論
0/150
提交評論