線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:3651
推到 Plurk!
推到 Facebook!

MD5算法研究

 
axsoft
版主


發表:681
回覆:1056
積分:969
註冊:2002-03-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2002-12-13 11:11:48 IP:61.218.xxx.xxx 未訂閱
MD5算法研究
[ 作者: 王可 ] 綜述 MD5的全稱是Message-Digest Algorithm 5(信息-摘要算法),在90年代初由MIT Laboratory for Computer Science和RSA Data Security Inc的Ronald L. Rivest開發出來,經MD2、MD3和MD4發展而來。它的作用是讓大容量信息在用數字簽名軟件簽署私人密匙前被"壓縮"成一種保密的格式(就是把一個任意長度的字節串變換成一定長的大整數)。不管是MD2、MD4還是MD5,它們都需要獲得一個隨機長度的信息並產生一個128位的信息摘要。雖然這些算法的結構或多或少有些相似,但MD2的設計與MD4和MD5完全不同,那是因為MD2是為8位機器做過設計優化的,而MD4和MD5卻是面向32位的電腦。這三個算法的描述和C語言源代碼在Internet RFCs 1321中有詳細的描述(http://www.ietf.org/rfc/rfc1321.txt),這是一份最權威的文檔,由Ronald L. Rivest在1992年8月向IEFT提交。 Rivest在1989年開發出MD2算法。在這個算法中,首先對信息進行數據補位,使信息的字節長度是16的倍數。然後,以一個16位的檢驗和追加到信息末尾。並且根據這個新產生的信息計算出散列值。後來,Rogier和Chauvaud發現如果忽略了檢驗和將產生MD2沖突。MD2算法的加密後結果是唯一的--既沒有重復。 為了加強算法的安全性,Rivest在1990年又開發出MD4算法。MD4算法同樣需要填補信息以確保信息的字節長度加上448後能被512整除(信息字節長度mod 512 = 448)。然後,一個以64位二進制表示的信息的最初長度被添加進來。信息被處理成512位Damg?rd/Merkle迭代結構的區塊,而且每個區塊要通過三個不同步驟的處理。Den Boer和Bosselaers以及其他人很快的發現了攻擊MD4版本中第一步和第三步的漏洞。Dobbertin向大家演示了如何利用一部普通的個人電腦在幾分鐘內找到MD4完整版本中的沖突(這個沖突實際上是一種漏洞,它將導致對不同的內容進行加密卻可能得到相同的加密後結果)。毫無疑問,MD4就此被淘汰掉了。 盡管MD4算法在安全上有個這麼大的漏洞,但它對在其後才被開發出來的好幾種信息安全加密算法的出現卻有?不可忽視的引導作用。除了MD5以外,其中比較有名的還有SHA-1、RIPE-MD以及HAVAL等。 一年以後,即1991年,Rivest開發出技術上更為趨近成熟的MD5算法。它在MD4的基礎上增加了"安全-帶子"(Safety-Belts)的概念。雖然MD5比MD4稍微慢一些,但卻更為安全。這個算法很明顯的由四個和MD4設計有少許不同的步驟組成。在MD5算法中,信息-摘要的大小和填充的必要條件與MD4完全相同。Den Boer和Bosselaers曾發現MD5算法中的假沖突(Pseudo-Collisions),但除此之外就沒有其他被發現的加密後結果了。 Van Oorschot和Wiener曾經考慮過一個在散列中暴力搜尋沖突的函數(Brute-Force Hash Function),而且他們猜測一個被設計專門用來搜索MD5沖突的機器(這台機器在1994年的制造成本大約是一百萬美元)可以平均每24天就找到一個沖突。但單從1991年到2001年這10年間,竟沒有出現替代MD5算法的MD6或被叫做其他什麼名字的新算法這一點,我們就可以看出這個瑕疵並沒有太多的影響MD5的安全性。上面所有這些都不足以成為MD5的在實際應用中的問題。並且,由於MD5算法的使用不需要支付任何版權費用的,所以在一般的情況下(非絕密應用領域。但即便是應用在絕密領域內,MD5也不失為一種非常優秀的中間技術),MD5怎麼都應該算得上是非常安全的了。 算法的應用 MD5的典型應用是對一段信息(Message)產生信息摘要(Message-Digest),以防止被篡改。比如,在UNIX下有很多軟件在下載的時候都有一個文件名相同,文件擴展名為.md5的文件,在這個文件中通常隻有一行文本,大致結構如: MD5 (tanajiya.tar.gz) = 0ca175b9c0f726a831d895e269332461 這就是tanajiya.tar.gz文件的數字簽名。MD5將整個文件當作一個大文本信息,通過其不可逆的字符串變換算法,產生了這個唯一的MD5信息摘要。如果在以後傳播這個文件的過程中,無論文件的內容發生了任何形式的改變(包括人為修改或者下載過程中線路不穩定引起的傳輸錯誤等),隻要你對這個文件重新計算MD5時就會發現信息摘要不相同,由此可以確定你得到的隻是一個不正確的文件。如果再有一個第三方的認証機構,用MD5還可以防止文件作者的"抵賴",這就是所謂的數字簽名應用。 MD5還廣泛用於加密和解密技術上。比如在UNIX系統中用戶的密碼就是以MD5(或其它類似的算法)經加密後存儲在文件系統中。當用戶登錄的時候,系統把用戶輸入的密碼計算成MD5值,然後再去和保存在文件系統中的MD5值進行比較,進而確定輸入的密碼是否正確。通過這樣的步驟,系統在並不知道用戶密碼的明碼的情況下就可以確定用戶登錄系統的合法性。這不但可以避免用戶的密碼被具有系統管理員權限的用戶知道,而且還在一定程度上增加了密碼被破解的難度。 正是因為這個原因,現在被黑客使用最多的一種破譯密碼的方法就是一種被稱為"跑字典"的方法。有兩種方法得到字典,一種是日常搜集的用做密碼的字符串表,另一種是用排列組合方法生成的,先用MD5程序計算出這些字典項的MD5值,然後再用目標的MD5值在這個字典中檢索。我們假設密碼的最大長度為8位字節(8 Bytes),同時密碼旬酮O字母和數字,共26 26 10=62個字符,排列組合出的字典的項數則是P(62,1) P(62,2)…. P(62,8),那也已經是一個很天文的數字了,存儲這個字典就需要TB級的磁盤陣列,而且這種方法還有一個前提,就是能獲得目標賬戶的密碼MD5值的情況下才可以。這種加密技術被廣泛的應用於UNIX系統中,這也是為什麼UNIX系統比一般操作系統更為堅固一個重要原因。 算法描述 對MD5算法簡要的敘述可以為:MD5以512位分組來處理輸入的信息,且每一分組又被劃分為16個32位子分組,經過了一系列的處理後,算法的輸出由四個32位分組組成,將這四個32位分組級聯後將生成一個128位散列值。 在MD5算法中,首先需要對信息進行填充,使其字節長度對512求余的結果等於448。因此,信息的字節長度(Bits Length)將被擴展至N*512 448,即N*64 56個字節(Bytes),N為一個正整數。填充的方法如下,在信息的後面填充一個1和無數個0,直到滿足上面的條件時才停止用0對信息的填充。然後,在在這個結果後面附加一個以64位二進制表示的填充前信息長度。經過這兩步的處理,現在的信息字節長度=N*512 448 64=(N 1)*512,即長度恰好是512的整數倍。這樣做的原因是為滿足後面處理中對信息長度的要求。 MD5中有四個32位被稱作鏈接變量(Chaining Variable)的整數參數,他們分別為:A=0x01234567,B=0x89abcdef,C=0xfedcba98,D=0x76543210。 當設置好這四個鏈接變量後,就開始進入算法的四輪循環運算。循環的次數是信息中512位信息分組的數目。 將上面四個鏈接變量復制到另外四個變量中:A到a,B到b,C到c,D到d。 主循環有四輪(MD4隻有三輪),每輪循環都很相似。第一輪進行16次操作。每次操作對a、b、c和d中的其中三個作一次非線性函數運算,然後將所得結果加上第四個變量,文本的一個子分組和一個常數。再將所得結果向右環移一個不定的數,並加上a、b、c或d中之一。最後用該結果取代a、b、c或d中之一。 以一下是每次操作中用到的四個非線性函數(每輪一個)。 F(X,Y,Z) =(X&Y)|((~X)&Z) G(X,Y,Z) =(X&Z)|(Y&(~Z)) H(X,Y,Z) =X^Y^Z I(X,Y,Z)=Y^(X|(~Z)) (&是與,|是或,~是非,^是異或) 這四個函數的說明:如果X、Y和Z的對應位是獨立和均勻的,那麼結果的每一位也應是獨立和均勻的。 F是一個逐位運算的函數。即,如果X,那麼Y,否則Z。函數H是逐位奇偶操作符。
      假設Mj表示消息的第j個子分組(從0到15),<< 
   FF(a,b,c,d,Mj,s,ti)表示a=b ((a (F(b,c,d) Mj ti)<<    GG(a,b,c,d,Mj,s,ti)表示a=b ((a (G(b,c,d) Mj ti)<<    HH(a,b,c,d,Mj,s,ti)表示a=b ((a (H(b,c,d) Mj ti)<<    II(a,b,c,d,Mj,s,ti)表示a=b ((a (I(b,c,d) Mj ti)<< 
  這四輪(64步)是:      第一輪       FF(a,b,c,d,M0,7,0xd76aa478)
   FF(d,a,b,c,M1,12,0xe8c7b756)
   FF(c,d,a,b,M2,17,0x242070db) 
      FF(b,c,d,a,M3,22,0xc1bdceee)
   FF(a,b,c,d,M4,7,0xf57c0faf)
   FF(d,a,b,c,M5,12,0x4787c62a)
   FF(c,d,a,b,M6,17,0xa8304613)
   FF(b,c,d,a,M7,22,0xfd469501)
   FF(a,b,c,d,M8,7,0x698098d8)
   FF(d,a,b,c,M9,12,0x8b44f7af)
   FF(c,d,a,b,M10,17,0xffff5bb1)
   FF(b,c,d,a,M11,22,0x895cd7be)
   FF(a,b,c,d,M12,7,0x6b901122)
   FF(d,a,b,c,M13,12,0xfd987193)
   FF(c,d,a,b,M14,17,0xa679438e)
   FF(b,c,d,a,M15,22,0x49b40821)      第二輪       GG(a,b,c,d,M1,5,0xf61e2562)
   GG(d,a,b,c,M6,9,0xc040b340)
   GG(c,d,a,b,M11,14,0x265e5a51)
   GG(b,c,d,a,M0,20,0xe9b6c7aa)
   GG(a,b,c,d,M5,5,0xd62f105d)
   GG(d,a,b,c,M10,9,0x02441453)
   GG(c,d,a,b,M15,14,0xd8a1e681)
   GG(b,c,d,a,M4,20,0xe7d3fbc8)
   GG(a,b,c,d,M9,5,0x21e1cde6)
   GG(d,a,b,c,M14,9,0xc33707d6)
   GG(c,d,a,b,M3,14,0xf4d50d87)
   GG(b,c,d,a,M8,20,0x455a14ed)
   GG(a,b,c,d,M13,5,0xa9e3e905)
   GG(d,a,b,c,M2,9,0xfcefa3f8)
   GG(c,d,a,b,M7,14,0x676f02d9)
   GG(b,c,d,a,M12,20,0x8d2a4c8a)      第三輪       HH(a,b,c,d,M5,4,0xfffa3942)
   HH(d,a,b,c,M8,11,0x8771f681)
   HH(c,d,a,b,M11,16,0x6d9d6122)
   HH(b,c,d,a,M14,23,0xfde5380c)
   HH(a,b,c,d,M1,4,0xa4beea44)
   HH(d,a,b,c,M4,11,0x4bdecfa9)
   HH(c,d,a,b,M7,16,0xf6bb4b60)
   HH(b,c,d,a,M10,23,0xbebfbc70)
   HH(a,b,c,d,M13,4,0x289b7ec6)
   HH(d,a,b,c,M0,11,0xeaa127fa)
   HH(c,d,a,b,M3,16,0xd4ef3085)
   HH(b,c,d,a,M6,23,0x04881d05)
   HH(a,b,c,d,M9,4,0xd9d4d039)
   HH(d,a,b,c,M12,11,0xe6db99e5)
   HH(c,d,a,b,M15,16,0x1fa27cf8)
   HH(b,c,d,a,M2,23,0xc4ac5665)      第四輪       II(a,b,c,d,M0,6,0xf4292244)
   II(d,a,b,c,M7,10,0x432aff97)
   II(c,d,a,b,M14,15,0xab9423a7)
   II(b,c,d,a,M5,21,0xfc93a039)
   II(a,b,c,d,M12,6,0x655b59c3)
   II(d,a,b,c,M3,10,0x8f0ccc92)
   II(c,d,a,b,M10,15,0xffeff47d)
   II(b,c,d,a,M1,21,0x85845dd1)
   II(a,b,c,d,M8,6,0x6fa87e4f)
   II(d,a,b,c,M15,10,0xfe2ce6e0)
   II(c,d,a,b,M6,15,0xa3014314)
   II(b,c,d,a,M13,21,0x4e0811a1)
   II(a,b,c,d,M4,6,0xf7537e82)
   II(d,a,b,c,M11,10,0xbd3af235)
   II(c,d,a,b,M2,15,0x2ad7d2bb)
   II(b,c,d,a,M9,21,0xeb86d391)      常數ti可以如下選擇:      在第i步中,ti是4294967296*abs(sin(i))的整數部分,i的單位是弧度。(4294967296等於2的32次方)
所有這些完成之後,將A、B、C、D分別加上a、b、c、d。然後用下一分組數據繼續運行算法,最後的輸出是A、B、C和D的級聯。      當你按照我上面所說的方法實現MD5算法以後,你可以用以下幾個信息對你做出來的程序作一個簡單的測試,看看程序有沒有錯誤。       MD5 ("") = d41d8cd98f00b204e9800998ecf8427e
   MD5 ("a") = 0cc175b9c0f1b6a831c399e269772661
   MD5 ("abc") = 900150983cd24fb0d6963f7d28e17f72
   MD5 ("message digest") = f96b697d7cb7938d525a2f31aaf161d0
   MD5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b
   MD5 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") =
d174ab98d277d9f5a5611c2c9f419d9f
   MD5 ("123456789012345678901234567890123456789012345678901234567890123456789
01234567890") = 57edf4a22be3c955ac49da2e2107b67a      如果你用上面的信息分別對你做的MD5算法實例做測試,最後得出的結論和標準答案完全一樣,那我就要在這裡象你道一聲祝賀了。要知道,我的程序在第一次編譯成功的時候是沒有得出和上面相同的結果的。      MD5的安全性      MD5相對MD4所作的改進:       1. 增加了第四輪;       2. 每一步均有唯一的加法常數;       3. 為減弱第二輪中函數G的對稱性從(X&Y)|(X&Z)|(Y&Z)變為(X&Z)|(Y&(~Z));       4. 第一步加上了上一步的結果,這將引起更快的雪崩效應;       5. 改變了第二輪和第三輪中訪問消息子分組的次序,使其更不相似;       6. 近似優化了每一輪中的循環左移位移量以實現更快的雪崩效應。各輪的位移量互不相同。     
聯盟----Visita網站http://www.vista.org.tw ---[ 發問前請先找找舊文章 ]--- 發表人 - axsoft 於 2002/12/13 11:17:41
axsoft
版主


發表:681
回覆:1056
積分:969
註冊:2002-03-13

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-02-25 19:03:42 IP:61.218.xxx.xxx 未訂閱
/******************************************************************************
*  Copyright (C) 2000 by Robert Hubley.                                      *
*  All rights reserved.                                                      *
*                                                                            *
*  This software is provided ``AS IS'' and any express or implied            *
*  warranties, including, but not limited to, the implied warranties of      *
*  merchantability and fitness for a particular purpose, are disclaimed.     *
*  In no event shall the authors be liable for any direct, indirect,         *
*  incidental, special, exemplary, or consequential damages (including, but  *
*  not limited to, procurement of substitute goods or services; loss of use, *
*  data, or profits; or business interruption) however caused and on any     *
*  theory of liability, whether in contract, strict liability, or tort       *
*  (including negligence or otherwise) arising in any way out of the use of  *
*  this software, even if advised of the possibility of such damage.         *
*                                                                            *
******************************************************************************    MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm    Port to Win32 DLL by Robert Hubley 1/5/2000    Original Copyright:    Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
rights reserved.    License to copy and use this software is granted provided that it
is identified as the "RSA Data Security, Inc. MD5 Message-Digest
Algorithm" in all material mentioning or referencing this software
or this function.
 
License is also granted to make and use derivative works provided
that such works are identified as "derived from the RSA Data
Security, Inc. MD5 Message-Digest Algorithm" in all material
mentioning or referencing the derived work.    RSA Data Security, Inc. makes no representations concerning either
the merchantability of this software or the suitability of this
software for any particular purpose. It is provided "as is"
without express or implied warranty of any kind.
 
These notices must be retained in any copies of any part of this
documentation and/or software.
*/    #include "md5.h"    /* Constants for MD5Transform routine.
*/
#define S11 7
#define S12 12
#define S13 17
#define S14 22
#define S21 5
#define S22 9
#define S23 14
#define S24 20
#define S31 4
#define S32 11
#define S33 16
#define S34 23
#define S41 6
#define S42 10
#define S43 15
#define S44 21    /* F, G, H and I are basic MD5 functions.
*/
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
#define H(x, y, z) ((x) ^ (y) ^ (z))
#define I(x, y, z) ((y) ^ ((x) | (~z)))    /* ROTATE_LEFT rotates x left n bits.
*/
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))    /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
Rotation is separate from addition to prevent recomputation.
*/
#define FF(a, b, c, d, x, s, ac) { (a)  = F ((b), (c), (d))   (x)   (unsigned long int)(ac); (a) = ROTATE_LEFT ((a), (s)); (a)  = (b); }
#define GG(a, b, c, d, x, s, ac) { (a)  = G ((b), (c), (d))   (x)   (unsigned long int)(ac); (a) = ROTATE_LEFT ((a), (s)); (a)  = (b); }
#define HH(a, b, c, d, x, s, ac) { (a)  = H ((b), (c), (d))   (x)   (unsigned long int)(ac); (a) = ROTATE_LEFT ((a), (s)); (a)  = (b); }
#define II(a, b, c, d, x, s, ac) { (a)  = I ((b), (c), (d))   (x)   (unsigned long int)(ac); (a) = ROTATE_LEFT ((a), (s)); (a)  = (b); }    /* MD5 initialization. Begins an MD5 operation, writing a new context.
*/    MD5_CTX::MD5_CTX()
{
MD5Init ();
}    MD5_CTX::~MD5_CTX()
{
}    void MD5_CTX::MD5Init ()
{
this->count[0] = this->count[1] = 0;
/* Load magic initialization constants.*/
this->state[0] = 0x67452301;
this->state[1] = 0xefcdab89;
this->state[2] = 0x98badcfe;
this->state[3] = 0x10325476;
/* Add by Liguangyi */
MD5_memset(PADDING, 0, sizeof(PADDING));
*PADDING=0x80;
//PADDING = {
//0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
//0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
//0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
}    /* MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
void MD5_CTX::MD5Update (unsigned char *input,unsigned int inputLen)
{
unsigned int i, index, partLen;    /* Compute number of bytes mod 64 */
index = (unsigned int)((this->count[0] >> 3) & 0x3F);    /* Update number of bits */
if ((this->count[0]  = ((unsigned long int)inputLen << 3))
< ((unsigned long int)inputLen << 3))
this->count[1]  ;
this->count[1]  = ((unsigned long int)inputLen >> 29);    partLen = 64 - index;    /* Transform as many times as possible.
*/
if (inputLen >= partLen) {
MD5_memcpy((unsigned char*)&this->buffer[index], 
(unsigned char*)input, partLen);
MD5Transform (this->state, this->buffer);    for (i = partLen; i   63 < inputLen; i  = 64)
MD5Transform (this->state, &input[i]);    index = 0;
}
else
i = 0;    /* Buffer remaining input */
MD5_memcpy ((unsigned char*)&this->buffer[index], (unsigned char*)&input[i], inputLen-i);
}    /* MD5 finalization. Ends an MD5 message-digest operation, writing the
the message digest and zeroizing the context.
*/
void MD5_CTX::MD5Final (unsigned char digest[16])
{
unsigned char bits[8];
unsigned int index, padLen;    /* Save number of bits */
Encode (bits, this->count, 8);    /* Pad out to 56 mod 64.
*/
index = (unsigned int)((this->count[0] >> 3) & 0x3f);
padLen = (index < 56) ? (56 - index) : (120 - index);
MD5Update ( PADDING, padLen);    /* Append length (before padding) */
MD5Update (bits, 8);
/* Store state in digest */
Encode (digest, this->state, 16);    /* Zeroize sensitive information.
*/
MD5_memset ((unsigned char*)this, 0, sizeof (*this));
this->MD5Init();
}    /* MD5 basic transformation. Transforms state based on block.
*/
void MD5_CTX::MD5Transform (unsigned long int state[4], unsigned char block[64])
{
unsigned long int a = state[0], b = state[1], c = state[2], d = state[3], x[16];    Decode (x, block, 64);    /* Round 1 */
FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */    /* Round 2 */
GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
GG (d, a, b, c, x[10], S22,  0x2441453); /* 22 */
GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */    /* Round 3 */
HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
HH (b, c, d, a, x[ 6], S34,  0x4881d05); /* 44 */
HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */    /* Round 4 */
II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */    state[0]  = a;
state[1]  = b;
state[2]  = c;
state[3]  = d;    /* Zeroize sensitive information.
*/
MD5_memset ((unsigned char*)x, 0, sizeof (x));
}    /* Encodes input (unsigned long int) into output (unsigned char). Assumes len is
a multiple of 4.
*/
void MD5_CTX::Encode (unsigned char *output, unsigned long int *input,unsigned int len)
{
unsigned int i, j;    for (i = 0, j = 0; j < len; i  , j  = 4) {
output[j] = (unsigned char)(input[i] & 0xff);
output[j 1] = (unsigned char)((input[i] >> 8) & 0xff);
output[j 2] = (unsigned char)((input[i] >> 16) & 0xff);
output[j 3] = (unsigned char)((input[i] >> 24) & 0xff);
}
}    /* Decodes input (unsigned char) into output (unsigned long int). Assumes len is
a multiple of 4.
*/
void MD5_CTX::Decode (unsigned long int *output, unsigned char *input, unsigned int len)
{
unsigned int i, j;    for (i = 0, j = 0; j < len; i  , j  = 4)
output[i] = ((unsigned long int)input[j]) | (((unsigned long int)input[j 1]) << 8) |
(((unsigned long int)input[j 2]) << 16) | (((unsigned long int)input[j 3]) << 24);
}    /* Note: Replace "for loop" with standard memcpy if possible.
*/    void MD5_CTX::MD5_memcpy (unsigned char* output, unsigned char* input,unsigned int len)
{
unsigned int i;    for (i = 0; i < len; i  )
output[i] = input[i];
}    /* Note: Replace "for loop" with standard memset if possible.
*/
void MD5_CTX::MD5_memset (unsigned char* output,int value,unsigned int len)
{
unsigned int i;    for (i = 0; i < len; i  )
((char *)output)[i] = (char)value;
}        /******************************************************************************
*  Copyright (C) 2000 by Robert Hubley.                                      *
*  All rights reserved.                                                      *
*                                                                            *
*  This software is provided ``AS IS'' and any express or implied            *
*  warranties, including, but not limited to, the implied warranties of      *
*  merchantability and fitness for a particular purpose, are disclaimed.     *
*  In no event shall the authors be liable for any direct, indirect,         *
*  incidental, special, exemplary, or consequential damages (including, but  *
*  not limited to, procurement of substitute goods or services; loss of use, *
*  data, or profits; or business interruption) however caused and on any     *
*  theory of liability, whether in contract, strict liability, or tort       *
*  (including negligence or otherwise) arising in any way out of the use of  *
*  this software, even if advised of the possibility of such damage.         *
*                                                                            *
******************************************************************************
MD5.H - header file for MD5C.C      Port to Win32 DLL by Robert Hubley 1/5/2000      Original Copyright:    Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
rights reserved.    License to copy and use this software is granted provided that it
is identified as the "RSA Data Security, Inc. MD5 Message-Digest
Algorithm" in all material mentioning or referencing this software
or this function.    License is also granted to make and use derivative works provided
that such works are identified as "derived from the RSA Data
Security, Inc. MD5 Message-Digest Algorithm" in all material
mentioning or referencing the derived work.    RSA Data Security, Inc. makes no representations concerning either
the merchantability of this software or the suitability of this
software for any particular purpose. It is provided "as is"
without express or implied warranty of any kind.    These notices must be retained in any copies of any part of this
documentation and/or software.
*/
/******************************************************************************
*  Copyright (C) 2000 by Robert Hubley.                                      *
*  All rights reserved.                                                      *
*                                                                            *
*  This software is provided ``AS IS'' and any express or implied            *
*  warranties, including, but not limited to, the implied warranties of      *
*  merchantability and fitness for a particular purpose, are disclaimed.     *
*  In no event shall the authors be liable for any direct, indirect,         *
*  incidental, special, exemplary, or consequential damages (including, but  *
*  not limited to, procurement of substitute goods or services; loss of use, *
*  data, or profits; or business interruption) however caused and on any     *
*  theory of liability, whether in contract, strict liability, or tort       *
*  (including negligence or otherwise) arising in any way out of the use of  *
*  this software, even if advised of the possibility of such damage.         *
*                                                                            *
******************************************************************************
*/    /******************************************************************************
*   2002-4-18 Modified by Liguangyi.      *
*   struct MD5_CTX ==> class MD5_CTX.          *
*   Take off the Globals Functions                                           *
******************************************************************************
*/    #ifndef _LGY_MD5_H
#define _LGY_MD5_H    /* MD5 Class. */
class MD5_CTX {
public:
MD5_CTX();
virtual ~MD5_CTX();
void MD5Update ( unsigned char *input, unsigned int inputLen);
void MD5Final (unsigned char digest[16]);    private:
unsigned long int state[4];/* state (ABCD) */
unsigned long int count[2];/* number of bits, modulo 2^64 (lsb first) */
unsigned char buffer[64];       /* input buffer */
unsigned char PADDING[64];/* What? */    private:
void MD5Init ();
void MD5Transform (unsigned long int state[4], unsigned char block[64]);
void MD5_memcpy (unsigned char* output, unsigned char* input,unsigned int len);
void Encode (unsigned char *output, unsigned long int *input,unsigned int len);
void Decode (unsigned long int *output, unsigned char *input, unsigned int len);
void MD5_memset (unsigned char* output,int value,unsigned int len);
};    #endif 
聯盟----Visita網站http://www.vista.org.tw ---[ 發問前請先找找舊文章 ]---
bruce0211
版主


發表:157
回覆:668
積分:279
註冊:2002-06-13

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-08-08 17:57:10 IP:211.21.xxx.xxx 未訂閱
【文章】取得檔案 MD5 值副函式 (有 bcb & delphi 版) 【作者】源碼任務 bruce0211@yahoo.com.tw 2005/08/08 【內文】心得分享系列 ■前言 目前因工作專案, 急須一個取得檔案 MD5 值的副函式 , 由於環境限制無法外掛 DLL , 結果找遍所有資料 , 不是不支援 BCB , 就是號稱支援 BCB 的 DELPHI 元件或單元 , 有語法上的問題(不知該元件作者到底有無在 BCB 上測過 ??) , 根本無法轉給 BCB 用 INDY 內有個單元叫 IdHashMessageDigest.pas , 有支援取得檔案 MD5 值的函式 , 但不是每個 INDY 版本都有附這個單元 , 且在 BCB 上仍有問題 , 所以火大 , 參照 INDY 原始碼 , 直接寫成 BCB 副函式 , 以後就不用再靠那些裝不起來的元件或是 內部黑箱作業的 DLL ■原文請見 http://delphi.ktop.com.tw/topic.php?TOPIC_ID=18760
系統時間:2024-05-06 15:12:32
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!