- UID
- 2
注册时间2004-12-1
阅读权限255
最后登录1970-1-1
总坛主
TA的每日心情 | 开心 2024-12-1 11:04 |
---|
签到天数: 12 天 [LV.3]偶尔看看II
|
- {************************************************}
- { }
- { EHash EurekaLog v 7.x }
- { Hash Unit - EHashes }
- { }
- { Copyright (c) 2001 - 2008 by Fabio Dell'Aria }
- { Fixed By PiaoYun/P.Y.G 2013-10-24 }
- { }
- {************************************************}
- unit EHash;
- interface
- uses
- Windows;
- // 获取从指针处给定大小的CRC32校验值,可用来作为判断函数是否被修改 piaoyun 2013-10-24
- function GetCRC32(Buf: Pointer; Size: DWord): DWord; overload;
- // 获取字符串CRC32
- function GetCRC32(const Src: string): DWord; overload;
- // 获取字符串CRC16
- function GetCRC16(const Src: string): Word;
- // 更新CRC32数据
- function UpdateCRC32(const ACRC32: LongWord; const AData: Byte): LongWord;
- // 获取给定位置、大小的MD5值 -- 也可以用来作为判断函数是否被修改
- procedure GetMD5(var Digest; const Buffer; Size: DWord);
- implementation
- const
- Crc16Tab: array[0..255] of Word =
- ($0000, $1021, $2042, $3063, $4084, $50A5, $60C6, $70E7,
- $8108, $9129, $A14A, $B16B, $C18C, $D1AD, $E1CE, $F1EF,
- $1231, $0210, $3273, $2252, $52B5, $4294, $72F7, $62D6,
- $9339, $8318, $B37B, $A35A, $D3BD, $C39C, $F3FF, $E3DE,
- $2462, $3443, $0420, $1401, $64E6, $74C7, $44A4, $5485,
- $A56A, $B54B, $8528, $9509, $E5EE, $F5CF, $C5AC, $D58D,
- $3653, $2672, $1611, $0630, $76D7, $66F6, $5695, $46B4,
- $B75B, $A77A, $9719, $8738, $F7DF, $E7FE, $D79D, $C7BC,
- $48C4, $58E5, $6886, $78A7, $0840, $1861, $2802, $3823,
- $C9CC, $D9ED, $E98E, $F9AF, $8948, $9969, $A90A, $B92B,
- $5AF5, $4AD4, $7AB7, $6A96, $1A71, $0A50, $3A33, $2A12,
- $DBFD, $CBDC, $FBBF, $EB9E, $9B79, $8B58, $BB3B, $AB1A,
- $6CA6, $7C87, $4CE4, $5CC5, $2C22, $3C03, $0C60, $1C41,
- $EDAE, $FD8F, $CDEC, $DDCD, $AD2A, $BD0B, $8D68, $9D49,
- $7E97, $6EB6, $5ED5, $4EF4, $3E13, $2E32, $1E51, $0E70,
- $FF9F, $EFBE, $DFDD, $CFFC, $BF1B, $AF3A, $9F59, $8F78,
- $9188, $81A9, $B1CA, $A1EB, $D10C, $C12D, $F14E, $E16F,
- $1080, $00A1, $30C2, $20E3, $5004, $4025, $7046, $6067,
- $83B9, $9398, $A3FB, $B3DA, $C33D, $D31C, $E37F, $F35E,
- $02B1, $1290, $22F3, $32D2, $4235, $5214, $6277, $7256,
- $B5EA, $A5CB, $95A8, $8589, $F56E, $E54F, $D52C, $C50D,
- $34E2, $24C3, $14A0, $0481, $7466, $6447, $5424, $4405,
- $A7DB, $B7FA, $8799, $97B8, $E75F, $F77E, $C71D, $D73C,
- $26D3, $36F2, $0691, $16B0, $6657, $7676, $4615, $5634,
- $D94C, $C96D, $F90E, $E92F, $99C8, $89E9, $B98A, $A9AB,
- $5844, $4865, $7806, $6827, $18C0, $08E1, $3882, $28A3,
- $CB7D, $DB5C, $EB3F, $FB1E, $8BF9, $9BD8, $ABBB, $BB9A,
- $4A75, $5A54, $6A37, $7A16, $0AF1, $1AD0, $2AB3, $3A92,
- $FD2E, $ED0F, $DD6C, $CD4D, $BDAA, $AD8B, $9DE8, $8DC9,
- $7C26, $6C07, $5C64, $4C45, $3CA2, $2C83, $1CE0, $0CC1,
- $EF1F, $FF3E, $CF5D, $DF7C, $AF9B, $BFBA, $8FD9, $9FF8,
- $6E17, $7E36, $4E55, $5E74, $2E93, $3EB2, $0ED1, $1EF0);
- var
- crc_table: array[0..255] of DWord; // Table of CRCs of all 8-bit messages.
- crc_table_computed: Boolean = False; // Flag: has the table been computed? Initially "false.
- // Make the table for a fast CRC.
- procedure make_crc_table;
- var
- c: DWord;
- n, k: Integer;
- poly: DWord; { polynomial exclusive-or pattern }
- const
- { terms of polynomial defining this crc (except x^32): }
- p: array[0..13] of Byte = (0, 1, 2, 4, 5, 7, 8, 10, 11, 12, 16, 22, 23, 26);
- begin
- { make exclusive-or pattern from polynomial ($EDB88320) }
- poly := 0;
- for n := 0 to ((SizeOf(p) div SizeOf(Byte)) - 1) do
- poly := (poly or (DWord(1) shl (31 - p[n])));
- for n := 0 to 255 do
- begin
- c := DWord(n);
- for k := 0 to 7 do
- begin
- if ((c and 1) <> 0) then
- c := (poly xor (c shr 1))
- else
- c := (c shr 1);
- end;
- crc_table[n] := c;
- end;
- crc_table_computed := True;
- end;
- function GetCRC32(Buf: Pointer; Size: DWord): DWord;
- var
- crc: DWord;
- begin
- if (buf = nil) or (Size = 0) then
- begin
- Result := 0;
- Exit;
- end;
- if (not crc_table_computed) then
- make_crc_table;
- crc := (0 xor uLong($FFFFFFFF));
- repeat
- crc := crc_table[(crc xor PByte(Buf)^) and $FF] xor (crc shr 8);
- Inc(PByte(Buf));
- Dec(Size);
- until (Size = 0);
- Result := (crc xor uLong($FFFFFFFF));
- end;
- function UpdateCRC32(const ACRC32: LongWord; const AData: Byte): LongWord;
- begin
- if (not crc_table_computed) then
- make_crc_table;
- Result := LongWord(crc_table[Byte(ACRC32 xor LongWord(AData))] xor ((ACRC32 shr 8) and LongWord($00FFFFFF)));
- end;
- function GetCRC32(const Src: string): DWord;
- var
- TestData: AnsiString;
- begin
- TestData := UTF8Encode(Src);
- Result := GetCRC32(Pointer(TestData), Length(TestData));
- end;
- function GetCRC16(const Src: string): Word;
- var
- i: Integer;
- TestData: AnsiString;
- begin
- Result := 0;
- TestData := UTF8Encode(Src);
- for i := 1 to Length(TestData) do
- Result := (Crc16Tab[((Result shr 8) xor Ord(TestData[i])) and $FF] xor ((Result shl 8) and $FFFF));
- end;
- procedure GetMD5(var Digest; const Buffer; Size: DWord);
- var
- LenHi, LenLo, Index: DWord;
- CurrentHash: array[0..3] of DWord;
- HashBuffer: array[0..63] of Byte;
- PBuf: ^Byte;
- procedure Compress;
- var
- Data: array[0..15] of DWord;
- A, B, C, D: DWord;
- function LRot32(a, b: DWord): DWord; //{$IFDEF SUPPORTS_INLINE}inline; {$ENDIF}
- begin
- Result := (a shl b) or (a shr (32 - b));
- end;
- begin
- Move(HashBuffer, Data, Sizeof(Data));
- A := CurrentHash[0];
- B := CurrentHash[1];
- C := CurrentHash[2];
- D := CurrentHash[3];
- A := B + LRot32(A + (D xor (B and (C xor D))) + Data[0] + $D76AA478, 7);
- D := A + LRot32(D + (C xor (A and (B xor C))) + Data[1] + $E8C7B756, 12);
- C := D + LRot32(C + (B xor (D and (A xor B))) + Data[2] + $242070DB, 17);
- B := C + LRot32(B + (A xor (C and (D xor A))) + Data[3] + $C1BDCEEE, 22);
- A := B + LRot32(A + (D xor (B and (C xor D))) + Data[4] + $F57C0FAF, 7);
- D := A + LRot32(D + (C xor (A and (B xor C))) + Data[5] + $4787C62A, 12);
- C := D + LRot32(C + (B xor (D and (A xor B))) + Data[6] + $A8304613, 17);
- B := C + LRot32(B + (A xor (C and (D xor A))) + Data[7] + $FD469501, 22);
- A := B + LRot32(A + (D xor (B and (C xor D))) + Data[8] + $698098D8, 7);
- D := A + LRot32(D + (C xor (A and (B xor C))) + Data[9] + $8B44F7AF, 12);
- C := D + LRot32(C + (B xor (D and (A xor B))) + Data[10] + $FFFF5BB1, 17);
- B := C + LRot32(B + (A xor (C and (D xor A))) + Data[11] + $895CD7BE, 22);
- A := B + LRot32(A + (D xor (B and (C xor D))) + Data[12] + $6B901122, 7);
- D := A + LRot32(D + (C xor (A and (B xor C))) + Data[13] + $FD987193, 12);
- C := D + LRot32(C + (B xor (D and (A xor B))) + Data[14] + $A679438E, 17);
- B := C + LRot32(B + (A xor (C and (D xor A))) + Data[15] + $49B40821, 22);
- A := B + LRot32(A + (C xor (D and (B xor C))) + Data[1] + $F61E2562, 5);
- D := A + LRot32(D + (B xor (C and (A xor B))) + Data[6] + $C040B340, 9);
- C := D + LRot32(C + (A xor (B and (D xor A))) + Data[11] + $265E5A51, 14);
- B := C + LRot32(B + (D xor (A and (C xor D))) + Data[0] + $E9B6C7AA, 20);
- A := B + LRot32(A + (C xor (D and (B xor C))) + Data[5] + $D62F105D, 5);
- D := A + LRot32(D + (B xor (C and (A xor B))) + Data[10] + $02441453, 9);
- C := D + LRot32(C + (A xor (B and (D xor A))) + Data[15] + $D8A1E681, 14);
- B := C + LRot32(B + (D xor (A and (C xor D))) + Data[4] + $E7D3FBC8, 20);
- A := B + LRot32(A + (C xor (D and (B xor C))) + Data[9] + $21E1CDE6, 5);
- D := A + LRot32(D + (B xor (C and (A xor B))) + Data[14] + $C33707D6, 9);
- C := D + LRot32(C + (A xor (B and (D xor A))) + Data[3] + $F4D50D87, 14);
- B := C + LRot32(B + (D xor (A and (C xor D))) + Data[8] + $455A14ED, 20);
- A := B + LRot32(A + (C xor (D and (B xor C))) + Data[13] + $A9E3E905, 5);
- D := A + LRot32(D + (B xor (C and (A xor B))) + Data[2] + $FCEFA3F8, 9);
- C := D + LRot32(C + (A xor (B and (D xor A))) + Data[7] + $676F02D9, 14);
- B := C + LRot32(B + (D xor (A and (C xor D))) + Data[12] + $8D2A4C8A, 20);
- A := B + LRot32(A + (B xor C xor D) + Data[5] + $FFFA3942, 4);
- D := A + LRot32(D + (A xor B xor C) + Data[8] + $8771F681, 11);
- C := D + LRot32(C + (D xor A xor B) + Data[11] + $6D9D6122, 16);
- B := C + LRot32(B + (C xor D xor A) + Data[14] + $FDE5380C, 23);
- A := B + LRot32(A + (B xor C xor D) + Data[1] + $A4BEEA44, 4);
- D := A + LRot32(D + (A xor B xor C) + Data[4] + $4BDECFA9, 11);
- C := D + LRot32(C + (D xor A xor B) + Data[7] + $F6BB4B60, 16);
- B := C + LRot32(B + (C xor D xor A) + Data[10] + $BEBFBC70, 23);
- A := B + LRot32(A + (B xor C xor D) + Data[13] + $289B7EC6, 4);
- D := A + LRot32(D + (A xor B xor C) + Data[0] + $EAA127FA, 11);
- C := D + LRot32(C + (D xor A xor B) + Data[3] + $D4EF3085, 16);
- B := C + LRot32(B + (C xor D xor A) + Data[6] + $04881D05, 23);
- A := B + LRot32(A + (B xor C xor D) + Data[9] + $D9D4D039, 4);
- D := A + LRot32(D + (A xor B xor C) + Data[12] + $E6DB99E5, 11);
- C := D + LRot32(C + (D xor A xor B) + Data[15] + $1FA27CF8, 16);
- B := C + LRot32(B + (C xor D xor A) + Data[2] + $C4AC5665, 23);
- A := B + LRot32(A + (C xor (B or (not D))) + Data[0] + $F4292244, 6);
- D := A + LRot32(D + (B xor (A or (not C))) + Data[7] + $432AFF97, 10);
- C := D + LRot32(C + (A xor (D or (not B))) + Data[14] + $AB9423A7, 15);
- B := C + LRot32(B + (D xor (C or (not A))) + Data[5] + $FC93A039, 21);
- A := B + LRot32(A + (C xor (B or (not D))) + Data[12] + $655B59C3, 6);
- D := A + LRot32(D + (B xor (A or (not C))) + Data[3] + $8F0CCC92, 10);
- C := D + LRot32(C + (A xor (D or (not B))) + Data[10] + $FFEFF47D, 15);
- B := C + LRot32(B + (D xor (C or (not A))) + Data[1] + $85845DD1, 21);
- A := B + LRot32(A + (C xor (B or (not D))) + Data[8] + $6FA87E4F, 6);
- D := A + LRot32(D + (B xor (A or (not C))) + Data[15] + $FE2CE6E0, 10);
- C := D + LRot32(C + (A xor (D or (not B))) + Data[6] + $A3014314, 15);
- B := C + LRot32(B + (D xor (C or (not A))) + Data[13] + $4E0811A1, 21);
- A := B + LRot32(A + (C xor (B or (not D))) + Data[4] + $F7537E82, 6);
- D := A + LRot32(D + (B xor (A or (not C))) + Data[11] + $BD3AF235, 10);
- C := D + LRot32(C + (A xor (D or (not B))) + Data[2] + $2AD7D2BB, 15);
- B := C + LRot32(B + (D xor (C or (not A))) + Data[9] + $EB86D391, 21);
- Inc(CurrentHash[0], A);
- Inc(CurrentHash[1], B);
- Inc(CurrentHash[2], C);
- Inc(CurrentHash[3], D);
- Index := 0;
- FillChar(HashBuffer, Sizeof(HashBuffer), 0);
- end;
- begin
- LenHi := 0;
- LenLo := 0;
- Index := 0;
- FillChar(HashBuffer, Sizeof(HashBuffer), 0);
- FillChar(CurrentHash, Sizeof(CurrentHash), 0);
- CurrentHash[0] := $67452301;
- CurrentHash[1] := $EFCDAB89;
- CurrentHash[2] := $98BADCFE;
- CurrentHash[3] := $10325476;
- Inc(LenHi, Size shr 29);
- Inc(LenLo, Size * 8);
- if (LenLo < (Size * 8)) then Inc(LenHi);
- PBuf := @Buffer;
- while (Size > 0) do
- begin
- if (Sizeof(HashBuffer) - Index) <= DWord(Size) then
- begin
- Move(PBuf^, HashBuffer[Index], Sizeof(HashBuffer) - Index);
- Dec(Size, Sizeof(HashBuffer) - Index);
- Inc(PBuf, Sizeof(HashBuffer) - Index);
- Compress;
- end
- else
- begin
- Move(PBuf^, HashBuffer[Index], Size);
- Inc(Index, Size);
- Size := 0;
- end;
- end;
- HashBuffer[Index] := $80;
- if (Index >= 56) then Compress;
- PDWord(@HashBuffer[56])^ := LenLo;
- PDWord(@HashBuffer[60])^ := LenHi;
- Compress;
- Move(CurrentHash, Digest, Sizeof(CurrentHash));
- end;
- end.
复制代码 |
|