重新組織編寫Delphi的MD2、MD4、MD5類

來源:互聯網
上載者:User

     在Delphi中,由於Indy的TIdHashMessageDigest類有BUG(見我的《要謹慎使用Indy的TIdHashMessageDigest類》一文),只好重新借Indy代碼重新組織改寫了一下,經過簡單測試正常,源碼如下,如有錯誤請指正:

unit MessageDigest;</p><p>interface</p><p>uses SysUtils, Classes;</p><p>type<br /> THashValue128 = array[0..15] of Byte;</p><p> TMDRecord128 = packed record<br /> case Integer of<br /> 0: (ByteValue: THashValue128);<br /> 1: (LongValue: array[0..3] of LongWord);<br /> end;</p><p> (*$NODEFINE THashValue128*)<br /> (*$NODEFINE TMDRecord128*)<br /> (*$HPPEMIT '#pragma pack(push, 1)'*)<br /> (*$HPPEMIT 'struct THashValue128'*)<br /> (*$HPPEMIT '{'*)<br /> (*$HPPEMIT ' Byte data[16];'*)<br /> (*$HPPEMIT ' Byte operator[](int Index){ return data[Index]; }'*)<br /> (*$HPPEMIT ' operator Byte* (){ return data; }'*)<br /> (*$HPPEMIT '};'*)<br /> (*$HPPEMIT 'typedef THashValue128 TMDRecord128;'*)<br /> (*$HPPEMIT '#pragma pack(pop)'*)</p><p> TMessageDigest = class<br /> private<br /> FState: TMDRecord128;<br /> FBuffer: array [0..63] of Byte;<br /> FBufSize: Integer;<br /> protected<br /> procedure Init; virtual; abstract;<br /> procedure Final(LastSize: Integer; Count: Int64); virtual; abstract;<br /> procedure Transform; virtual; abstract;<br /> public<br /> class function AsHex(const Value: THashValue128): string;<br />{$IFDEF UNICODE}<br /> function HashValue(const Src: AnsiString): THashValue128; overload;<br />{$ENDIF}<br /> function HashValue(const Src: string): THashValue128; overload;<br /> function HashValue(AStream: TStream;<br /> StartPos: Int64 = 0; Size: Int64 = 0): THashValue128; overload;<br /> end;</p><p> TMessageDigest2 = class(TMessageDigest)<br /> private<br /> FX: array[0..47] of Byte; // 384bit<br /> FCheckSum: THashValue128;<br /> protected<br /> procedure Init; override;<br /> procedure Final(LastSize: Integer; Count: Int64); override;<br /> procedure Transform; override;<br /> end;</p><p> TMessageDigest4 = class(TMessageDigest)<br /> protected<br /> procedure Init; override;<br /> procedure Final(LastSize: Integer; Count: Int64); override;<br /> procedure Transform; override;<br /> end;</p><p> TMessageDigest5 = class(TMessageDigest4)<br /> protected<br /> procedure Transform; override;<br /> public</p><p> end;</p><p>implementation</p><p>{ TMessageDigest }</p><p>class function TMessageDigest.AsHex(const Value: THashValue128): string;<br />const<br /> HexDigits: array[0..15] of char = '0123456789ABCDEF';<br />var<br /> I: Integer;<br />begin<br /> SetLength(Result, 32);<br /> for I := 0 to 15 do<br /> begin<br /> Result[I shl 1 + 1] := HexDigits[Value[I] shr 4];<br /> Result[I shl 1 + 2] := HexDigits[Value[I] and $F];<br /> end;<br />end;</p><p>{$IFDEF UNICODE}<br />function TMessageDigest.HashValue(const Src: AnsiString): THashValue128;<br />{$ELSE}<br />function TMessageDigest.HashValue(const Src: string): THashValue128;<br />{$ENDIF}<br />var<br /> I, Len, Count: Integer;<br />begin<br /> Init;<br /> Count := Length(Src);<br /> Len := Count + 1;<br /> I := 1;<br /> while Len - I >= FBufSize do<br /> begin<br /> Move(Src[I], FBuffer, FBufSize);<br /> Transform;<br /> Inc(I, FBufSize);<br /> end;<br /> Dec(Len, I);<br /> if Len > 0 then<br /> Move(Src[I], FBuffer, Len);<br /> Final(Len, Count);<br /> Result := FState.ByteValue;<br />end;</p><p>{$IFDEF UNICODE}<br />function TMessageDigest.HashValue(const Src: string): THashValue128;<br />begin<br /> Result := HashValue(AnsiString(Src));<br />end;<br />{$ENDIF}</p><p>function TMessageDigest.HashValue(AStream: TStream; StartPos, Size: Int64): THashValue128;<br />var<br /> Count: Int64;<br />begin<br /> if (StartPos >= 0) or (StartPos <= AStream.Size) then<br /> AStream.Position := StartPos;<br /> if (Size <= 0) or (Size > AStream.Size - AStream.Position) then<br /> Size := AStream.Size<br /> else Size := Size + AStream.Position;<br /> Count := Size - AStream.Position;<br /> Init;<br /> while Size - AStream.Position >= FBufSize do<br /> begin<br /> AStream.Read(FBuffer, FBufSize);<br /> Transform;<br /> end;<br /> Size := AStream.Read(FBuffer, Size - AStream.Position);<br /> FInal(Size, Count);<br /> Result := FState.ByteValue;<br />end;</p><p>const<br /> MD2_PI_SUBST : array [0..255] of byte = (<br /> 41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240,<br /> 6, 19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217,<br /> 188, 76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66,<br /> 111, 24, 138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73,<br /> 160, 251, 245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178,<br /> 7, 63, 148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154,<br /> 90, 144, 50, 39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25,<br /> 48, 179, 72, 165, 181, 209, 215, 94, 146, 42, 172, 86, 170, 198,<br /> 79, 184, 56, 210, 150, 164, 125, 182, 118, 252, 107, 226, 156, 116,<br /> 4, 241, 69, 157, 112, 89, 100, 113, 135, 32, 134, 91, 207, 101,<br /> 230, 45, 168, 2, 27, 96, 37, 173, 174, 176, 185, 246, 28, 70,<br /> 97, 105, 52, 64, 126, 15, 85, 71, 163, 35, 221, 81, 175, 58,<br /> 195, 92, 249, 206, 186, 197, 234, 38, 44, 83, 13, 110, 133, 40,<br /> 132, 9, 211, 223, 205, 244, 65, 129, 77, 82, 106, 220, 55, 200,<br /> 108, 193, 171, 250, 36, 225, 123, 8, 12, 189, 177, 74, 120, 136,<br /> 149, 139, 227, 99, 232, 109, 233, 203, 213, 254, 59, 0, 29, 57,<br /> 242, 239, 183, 14, 102, 88, 208, 228, 166, 119, 114, 248, 235, 117,<br /> 75, 10, 49, 68, 80, 180, 143, 237, 31, 26, 219, 153, 141, 51,<br /> 159, 17, 131, 20);</p><p>{ TMessageDigest2 }</p><p>procedure TMessageDigest2.Final(LastSize: Integer; Count: Int64);<br />var<br /> Pad: Byte;<br />begin<br /> Pad := 16 - LastSize;<br /> // Step 1<br /> FillChar(FBuffer[LastSize], Pad, Pad);<br /> Transform;<br /> // Step 2<br /> Move(FCheckSum, FBuffer, 16);<br /> Transform;<br /> Move(FX, FState, 16);<br />end;</p><p>procedure TMessageDigest2.Init;<br />begin<br /> FBufSize := 16;<br /> FillChar(FCheckSum, 16, 0);<br /> FillChar(FBuffer, 16, 0);<br /> FillChar(FX, Sizeof(FX), 0);<br />end;</p><p>procedure TMessageDigest2.Transform;<br />const<br /> NumRounds = 18;<br />var<br /> x: Byte;<br /> i, j: Integer;<br /> T: Word;<br /> LCheckSumScore: Byte;<br />begin<br /> // Move the next 16 bytes into the second 16 bytes of X.<br /> for i := 0 to 15 do<br /> begin<br /> x := FBuffer[i];<br /> FX[i + 16] := x;<br /> FX[i + 32] := x xor FX[i];<br /> end;<br /> { Do 18 rounds. }<br /> T := 0;<br /> for i := 0 to NumRounds - 1 do<br /> begin<br /> for j := 0 to 47 do<br /> begin<br /> T := FX[j] xor MD2_PI_SUBST[T];<br /> FX[j] := T;// and $FF;<br /> end;<br /> T := (T + i) and $FF;<br /> end;</p><p> LCheckSumScore := FChecksum[15];<br /> for i := 0 to 15 do<br /> begin<br /> x := FBuffer[i] xor LCheckSumScore;<br /> LCheckSumScore := FChecksum[i] xor MD2_PI_SUBST[x];<br /> FChecksum[i] := LCheckSumScore;<br /> end;<br />end;</p><p>{ TMessageDigest4 }</p><p>const<br /> MD4_INIT_VALUES: array[0..3] of LongWord = (<br /> $67452301, $EFCDAB89, $98BADCFE, $10325476);</p><p>function ROL(AVal: LongWord; AShift: Byte): LongWord;<br />asm<br /> mov cl, dl<br /> rol eax, cl<br />// Result := (AVal shl AShift) or (AVal shr (32 - AShift));<br />end;</p><p>procedure TMessageDigest4.Final(LastSize: Integer; Count: Int64);<br />var<br /> I: Integer;<br />begin<br /> // Append one bit with value 1<br /> FBuffer[LastSize] := $80;<br /> Inc(LastSize);<br /> // Must have sufficient space to insert the 64-bit size value<br /> if LastSize > 56 then<br /> begin<br /> FillChar(FBuffer[LastSize], 64 - LastSize, 0);<br /> Transform;<br /> LastSize := 0;<br /> end;<br /> // Pad with zeroes. Leave room for the 64 bit size value.<br /> FillChar(FBuffer[LastSize], 56 - LastSize, 0);<br /> // Append the Number of bits processed.<br /> Count := Count shl 3;<br /> for I := 56 to 63 do<br /> begin<br /> FBuffer[I] := Count and $FF;<br /> Count := Count shr 8;<br /> end;<br /> Transform;<br />end;</p><p>procedure TMessageDigest4.Init;<br />begin<br /> FBufSize := 64;<br /> Move(MD4_INIT_VALUES, FState, 16);<br />end;</p><p>procedure TMessageDigest4.Transform;<br />var<br /> A, B, C, D, i : LongWord;<br /> buff : array[0..15] of LongWord; // 64-byte buffer<br />begin<br /> A := FState.LongValue[0];<br /> B := FState.LongValue[1];<br /> C := FState.LongValue[2];<br /> D := FState.LongValue[3];<br /> Move(FBuffer, buff, Sizeof(buff));<br /> // Round 1<br /> for i := 0 to 3 do<br /> begin<br /> A := ROL((((D xor C) and B) xor D) + A + buff[i*4+0], 3);<br /> D := ROL((((C xor B) and A) xor C) + D + buff[i*4+1], 7);<br /> C := ROL((((B xor A) and D) xor B) + C + buff[i*4+2], 11);<br /> B := ROL((((A xor D) and C) xor A) + B + buff[i*4+3], 19);<br /> end;<br /> // Round 2<br /> for i := 0 to 3 do<br /> begin<br /> A := ROL(((B and C) or (D and (B or C))) + A + buff[0*4+i] + $5A827999, 3);<br /> D := ROL(((A and B) or (C and (A or B))) + D + buff[1*4+i] + $5A827999, 5);<br /> C := ROL(((D and A) or (B and (D or A))) + C + buff[2*4+i] + $5A827999, 9);<br /> B := ROL(((C and D) or (A and (C or D))) + B + buff[3*4+i] + $5A827999, 13);<br /> end;<br /> // Round 3<br /> A := ROL((B xor C xor D) + A + buff[ 0] + $6ED9EBA1, 3);<br /> D := ROL((A xor B xor C) + D + buff[ 8] + $6ED9EBA1, 9);<br /> C := ROL((D xor A xor B) + C + buff[ 4] + $6ED9EBA1, 11);<br /> B := ROL((C xor D xor A) + B + buff[12] + $6ED9EBA1, 15);<br /> A := ROL((B xor C xor D) + A + buff[ 2] + $6ED9EBA1, 3);<br /> D := ROL((A xor B xor C) + D + buff[10] + $6ED9EBA1, 9);<br /> C := ROL((D xor A xor B) + C + buff[ 6] + $6ED9EBA1, 11);<br /> B := ROL((C xor D xor A) + B + buff[14] + $6ED9EBA1, 15);<br /> A := ROL((B xor C xor D) + A + buff[ 1] + $6ED9EBA1, 3);<br /> D := ROL((A xor B xor C) + D + buff[ 9] + $6ED9EBA1, 9);<br /> C := ROL((D xor A xor B) + C + buff[ 5] + $6ED9EBA1, 11);<br /> B := ROL((C xor D xor A) + B + buff[13] + $6ED9EBA1, 15);<br /> A := ROL((B xor C xor D) + A + buff[ 3] + $6ED9EBA1, 3);<br /> D := ROL((A xor B xor C) + D + buff[11] + $6ED9EBA1, 9);<br /> C := ROL((D xor A xor B) + C + buff[ 7] + $6ED9EBA1, 11);<br /> B := ROL((C xor D xor A) + B + buff[15] + $6ED9EBA1, 15);</p><p> Inc(FState.LongValue[0], A);<br /> Inc(FState.LongValue[1], B);<br /> Inc(FState.LongValue[2], C);<br /> Inc(FState.LongValue[3], D);<br />end;</p><p>{ TMessageDigest5 }</p><p>const<br /> MD5_SINE : array [1..64] of LongWord = (<br /> { Round 1. }<br /> $d76aa478, $e8c7b756, $242070db, $c1bdceee, $f57c0faf, $4787c62a,<br /> $a8304613, $fd469501, $698098d8, $8b44f7af, $ffff5bb1, $895cd7be,<br /> $6b901122, $fd987193, $a679438e, $49b40821,<br /> { Round 2. }<br /> $f61e2562, $c040b340, $265e5a51, $e9b6c7aa, $d62f105d, $02441453,<br /> $d8a1e681, $e7d3fbc8, $21e1cde6, $c33707d6, $f4d50d87, $455a14ed,<br /> $a9e3e905, $fcefa3f8, $676f02d9, $8d2a4c8a,<br /> { Round 3. }<br /> $fffa3942, $8771f681, $6d9d6122, $fde5380c, $a4beea44, $4bdecfa9,<br /> $f6bb4b60, $bebfbc70, $289b7ec6, $eaa127fa, $d4ef3085, $04881d05,<br /> $d9d4d039, $e6db99e5, $1fa27cf8, $c4ac5665,<br /> { Round 4. }<br /> $f4292244, $432aff97, $ab9423a7, $fc93a039, $655b59c3, $8f0ccc92,<br /> $ffeff47d, $85845dd1, $6fa87e4f, $fe2ce6e0, $a3014314, $4e0811a1,<br /> $f7537e82, $bd3af235, $2ad7d2bb, $eb86d391<br /> );</p><p>procedure TMessageDigest5.Transform;<br />var<br /> A, B, C, D: LongWord;<br /> x : array[0..15] of LongWord; // 64-byte buffer<br />begin<br /> A := FState.LongValue[0];<br /> B := FState.LongValue[1];<br /> C := FState.LongValue[2];<br /> D := FState.LongValue[3];<br /> Move(FBuffer, x, Sizeof(x));<br /> { Round 1 }<br /> A := ROL(A + (((D xor C) and B) xor D) + x[ 0] + MD5_SINE[ 1], 7) + B;<br /> D := ROL(D + (((C xor B) and A) xor C) + x[ 1] + MD5_SINE[ 2], 12) + A;<br /> C := ROL(C + (((B xor A) and D) xor B) + x[ 2] + MD5_SINE[ 3], 17) + D;<br /> B := ROL(B + (((A xor D) and C) xor A) + x[ 3] + MD5_SINE[ 4], 22) + C;<br /> A := ROL(A + (((D xor C) and B) xor D) + x[ 4] + MD5_SINE[ 5], 7) + B;<br /> D := ROL(D + (((C xor B) and A) xor C) + x[ 5] + MD5_SINE[ 6], 12) + A;<br /> C := ROL(C + (((B xor A) and D) xor B) + x[ 6] + MD5_SINE[ 7], 17) + D;<br /> B := ROL(B + (((A xor D) and C) xor A) + x[ 7] + MD5_SINE[ 8], 22) + C;<br /> A := ROL(A + (((D xor C) and B) xor D) + x[ 8] + MD5_SINE[ 9], 7) + B;<br /> D := ROL(D + (((C xor B) and A) xor C) + x[ 9] + MD5_SINE[10], 12) + A;<br /> C := ROL(C + (((B xor A) and D) xor B) + x[10] + MD5_SINE[11], 17) + D;<br /> B := ROL(B + (((A xor D) and C) xor A) + x[11] + MD5_SINE[12], 22) + C;<br /> A := ROL(A + (((D xor C) and B) xor D) + x[12] + MD5_SINE[13], 7) + B;<br /> D := ROL(D + (((C xor B) and A) xor C) + x[13] + MD5_SINE[14], 12) + A;<br /> C := ROL(C + (((B xor A) and D) xor B) + x[14] + MD5_SINE[15], 17) + D;<br /> B := ROL(B + (((A xor D) and C) xor A) + x[15] + MD5_SINE[16], 22) + C;<br /> { Round 2 }<br /> A := ROL(A + (C xor (D and (B xor C))) + x[ 1] + MD5_SINE[17], 5) + B;<br /> D := ROL(D + (B xor (C and (A xor B))) + x[ 6] + MD5_SINE[18], 9) + A;<br /> C := ROL(C + (A xor (B and (D xor A))) + x[11] + MD5_SINE[19], 14) + D;<br /> B := ROL(B + (D xor (A and (C xor D))) + x[ 0] + MD5_SINE[20], 20) + C;<br /> A := ROL(A + (C xor (D and (B xor C))) + x[ 5] + MD5_SINE[21], 5) + B;<br /> D := ROL(D + (B xor (C and (A xor B))) + x[10] + MD5_SINE[22], 9) + A;<br /> C := ROL(C + (A xor (B and (D xor A))) + x[15] + MD5_SINE[23], 14) + D;<br /> B := ROL(B + (D xor (A and (C xor D))) + x[ 4] + MD5_SINE[24], 20) + C;<br /> A := ROL(A + (C xor (D and (B xor C))) + x[ 9] + MD5_SINE[25], 5) + B;<br /> D := ROL(D + (B xor (C and (A xor B))) + x[14] + MD5_SINE[26], 9) + A;<br /> C := ROL(C + (A xor (B and (D xor A))) + x[ 3] + MD5_SINE[27], 14) + D;<br /> B := ROL(B + (D xor (A and (C xor D))) + x[ 8] + MD5_SINE[28], 20) + C;<br /> A := ROL(A + (C xor (D and (B xor C))) + x[13] + MD5_SINE[29], 5) + B;<br /> D := ROL(D + (B xor (C and (A xor B))) + x[ 2] + MD5_SINE[30], 9) + A;<br /> C := ROL(C + (A xor (B and (D xor A))) + x[ 7] + MD5_SINE[31], 14) + D;<br /> B := ROL(B + (D xor (A and (C xor D))) + x[12] + MD5_SINE[32], 20) + C;<br /> { Round 3. }<br /> A := ROL(A + (B xor C xor D) + x[ 5] + MD5_SINE[33], 4) + B;<br /> D := ROL(D + (A xor B xor C) + x[ 8] + MD5_SINE[34], 11) + A;<br /> C := ROL(C + (D xor A xor B) + x[11] + MD5_SINE[35], 16) + D;<br /> B := ROL(B + (C xor D xor A) + x[14] + MD5_SINE[36], 23) + C;<br /> A := ROL(A + (B xor C xor D) + x[ 1] + MD5_SINE[37], 4) + B;<br /> D := ROL(D + (A xor B xor C) + x[ 4] + MD5_SINE[38], 11) + A;<br /> C := ROL(C + (D xor A xor B) + x[ 7] + MD5_SINE[39], 16) + D;<br /> B := ROL(B + (C xor D xor A) + x[10] + MD5_SINE[40], 23) + C;<br /> A := ROL(A + (B xor C xor D) + x[13] + MD5_SINE[41], 4) + B;<br /> D := ROL(D + (A xor B xor C) + x[ 0] + MD5_SINE[42], 11) + A;<br /> C := ROL(C + (D xor A xor B) + x[ 3] + MD5_SINE[43], 16) + D;<br /> B := ROL(B + (C xor D xor A) + x[ 6] + MD5_SINE[44], 23) + C;<br /> A := ROL(A + (B xor C xor D) + x[ 9] + MD5_SINE[45], 4) + B;<br /> D := ROL(D + (A xor B xor C) + x[12] + MD5_SINE[46], 11) + A;<br /> C := ROL(C + (D xor A xor B) + x[15] + MD5_SINE[47], 16) + D;<br /> B := ROL(B + (C xor D xor A) + x[ 2] + MD5_SINE[48], 23) + C;<br /> { Round 4. }<br /> A := ROL(A + ((B or not D) xor C) + x[ 0] + MD5_SINE[49], 6) + B;<br /> D := ROL(D + ((A or not C) xor B) + x[ 7] + MD5_SINE[50], 10) + A;<br /> C := ROL(C + ((D or not B) xor A) + x[14] + MD5_SINE[51], 15) + D;<br /> B := ROL(B + ((C or not A) xor D) + x[ 5] + MD5_SINE[52], 21) + C;<br /> A := ROL(A + ((B or not D) xor C) + x[12] + MD5_SINE[53], 6) + B;<br /> D := ROL(D + ((A or not C) xor B) + x[ 3] + MD5_SINE[54], 10) + A;<br /> C := ROL(C + ((D or not B) xor A) + x[10] + MD5_SINE[55], 15) + D;<br /> B := ROL(B + ((C or not A) xor D) + x[ 1] + MD5_SINE[56], 21) + C;<br /> A := ROL(A + ((B or not D) xor C) + x[ 8] + MD5_SINE[57], 6) + B;<br /> D := ROL(D + ((A or not C) xor B) + x[15] + MD5_SINE[58], 10) + A;<br /> C := ROL(C + ((D or not B) xor A) + x[ 6] + MD5_SINE[59], 15) + D;<br /> B := ROL(B + ((C or not A) xor D) + x[13] + MD5_SINE[60], 21) + C;<br /> A := ROL(A + ((B or not D) xor C) + x[ 4] + MD5_SINE[61], 6) + B;<br /> D := ROL(D + ((A or not C) xor B) + x[11] + MD5_SINE[62], 10) + A;<br /> C := ROL(C + ((D or not B) xor A) + x[ 2] + MD5_SINE[63], 15) + D;<br /> B := ROL(B + ((C or not A) xor D) + x[ 9] + MD5_SINE[64], 21) + C;</p><p> Inc(FState.LongValue[0], A);<br /> Inc(FState.LongValue[1], B);<br /> Inc(FState.LongValue[2], C);<br /> Inc(FState.LongValue[3], D);<br />end;</p><p>end.   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.