飘云 发表于 2014-2-5 00:37:55

EHash.pas--移植自EurekaLog -- by 飘云

{************************************************}
{                                                }
{            EHash EurekaLog v 7.x             }
{               Hash Unit - EHashes            }
{                                                }
{Copyright (c) 2001 - 2008 by Fabio Dell'Aria}
{Fixed By PiaoYun/P.Y.G2013-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 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 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 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)));

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 := 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 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)) and $FF] xor ((Result shl 8) and $FFFF));
end;

procedure GetMD5(var Digest; const Buffer; Size: DWord);
var
LenHi, LenLo, Index: DWord;
CurrentHash: array of DWord;
HashBuffer: array of Byte;
PBuf: ^Byte;

procedure Compress;
var
    Data: array 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;
    B := CurrentHash;
    C := CurrentHash;
    D := CurrentHash;

    A := B + LRot32(A + (D xor (B and (C xor D))) + Data + $D76AA478, 7);
    D := A + LRot32(D + (C xor (A and (B xor C))) + Data + $E8C7B756, 12);
    C := D + LRot32(C + (B xor (D and (A xor B))) + Data + $242070DB, 17);
    B := C + LRot32(B + (A xor (C and (D xor A))) + Data + $C1BDCEEE, 22);
    A := B + LRot32(A + (D xor (B and (C xor D))) + Data + $F57C0FAF, 7);
    D := A + LRot32(D + (C xor (A and (B xor C))) + Data + $4787C62A, 12);
    C := D + LRot32(C + (B xor (D and (A xor B))) + Data + $A8304613, 17);
    B := C + LRot32(B + (A xor (C and (D xor A))) + Data + $FD469501, 22);
    A := B + LRot32(A + (D xor (B and (C xor D))) + Data + $698098D8, 7);
    D := A + LRot32(D + (C xor (A and (B xor C))) + Data + $8B44F7AF, 12);
    C := D + LRot32(C + (B xor (D and (A xor B))) + Data + $FFFF5BB1, 17);
    B := C + LRot32(B + (A xor (C and (D xor A))) + Data + $895CD7BE, 22);
    A := B + LRot32(A + (D xor (B and (C xor D))) + Data + $6B901122, 7);
    D := A + LRot32(D + (C xor (A and (B xor C))) + Data + $FD987193, 12);
    C := D + LRot32(C + (B xor (D and (A xor B))) + Data + $A679438E, 17);
    B := C + LRot32(B + (A xor (C and (D xor A))) + Data + $49B40821, 22);

    A := B + LRot32(A + (C xor (D and (B xor C))) + Data + $F61E2562, 5);
    D := A + LRot32(D + (B xor (C and (A xor B))) + Data + $C040B340, 9);
    C := D + LRot32(C + (A xor (B and (D xor A))) + Data + $265E5A51, 14);
    B := C + LRot32(B + (D xor (A and (C xor D))) + Data + $E9B6C7AA, 20);
    A := B + LRot32(A + (C xor (D and (B xor C))) + Data + $D62F105D, 5);
    D := A + LRot32(D + (B xor (C and (A xor B))) + Data + $02441453, 9);
    C := D + LRot32(C + (A xor (B and (D xor A))) + Data + $D8A1E681, 14);
    B := C + LRot32(B + (D xor (A and (C xor D))) + Data + $E7D3FBC8, 20);
    A := B + LRot32(A + (C xor (D and (B xor C))) + Data + $21E1CDE6, 5);
    D := A + LRot32(D + (B xor (C and (A xor B))) + Data + $C33707D6, 9);
    C := D + LRot32(C + (A xor (B and (D xor A))) + Data + $F4D50D87, 14);
    B := C + LRot32(B + (D xor (A and (C xor D))) + Data + $455A14ED, 20);
    A := B + LRot32(A + (C xor (D and (B xor C))) + Data + $A9E3E905, 5);
    D := A + LRot32(D + (B xor (C and (A xor B))) + Data + $FCEFA3F8, 9);
    C := D + LRot32(C + (A xor (B and (D xor A))) + Data + $676F02D9, 14);
    B := C + LRot32(B + (D xor (A and (C xor D))) + Data + $8D2A4C8A, 20);

    A := B + LRot32(A + (B xor C xor D) + Data + $FFFA3942, 4);
    D := A + LRot32(D + (A xor B xor C) + Data + $8771F681, 11);
    C := D + LRot32(C + (D xor A xor B) + Data + $6D9D6122, 16);
    B := C + LRot32(B + (C xor D xor A) + Data + $FDE5380C, 23);
    A := B + LRot32(A + (B xor C xor D) + Data + $A4BEEA44, 4);
    D := A + LRot32(D + (A xor B xor C) + Data + $4BDECFA9, 11);
    C := D + LRot32(C + (D xor A xor B) + Data + $F6BB4B60, 16);
    B := C + LRot32(B + (C xor D xor A) + Data + $BEBFBC70, 23);
    A := B + LRot32(A + (B xor C xor D) + Data + $289B7EC6, 4);
    D := A + LRot32(D + (A xor B xor C) + Data + $EAA127FA, 11);
    C := D + LRot32(C + (D xor A xor B) + Data + $D4EF3085, 16);
    B := C + LRot32(B + (C xor D xor A) + Data + $04881D05, 23);
    A := B + LRot32(A + (B xor C xor D) + Data + $D9D4D039, 4);
    D := A + LRot32(D + (A xor B xor C) + Data + $E6DB99E5, 11);
    C := D + LRot32(C + (D xor A xor B) + Data + $1FA27CF8, 16);
    B := C + LRot32(B + (C xor D xor A) + Data + $C4AC5665, 23);

    A := B + LRot32(A + (C xor (B or (not D))) + Data + $F4292244, 6);
    D := A + LRot32(D + (B xor (A or (not C))) + Data + $432AFF97, 10);
    C := D + LRot32(C + (A xor (D or (not B))) + Data + $AB9423A7, 15);
    B := C + LRot32(B + (D xor (C or (not A))) + Data + $FC93A039, 21);
    A := B + LRot32(A + (C xor (B or (not D))) + Data + $655B59C3, 6);
    D := A + LRot32(D + (B xor (A or (not C))) + Data + $8F0CCC92, 10);
    C := D + LRot32(C + (A xor (D or (not B))) + Data + $FFEFF47D, 15);
    B := C + LRot32(B + (D xor (C or (not A))) + Data + $85845DD1, 21);
    A := B + LRot32(A + (C xor (B or (not D))) + Data + $6FA87E4F, 6);
    D := A + LRot32(D + (B xor (A or (not C))) + Data + $FE2CE6E0, 10);
    C := D + LRot32(C + (A xor (D or (not B))) + Data + $A3014314, 15);
    B := C + LRot32(B + (D xor (C or (not A))) + Data + $4E0811A1, 21);
    A := B + LRot32(A + (C xor (B or (not D))) + Data + $F7537E82, 6);
    D := A + LRot32(D + (B xor (A or (not C))) + Data + $BD3AF235, 10);
    C := D + LRot32(C + (A xor (D or (not B))) + Data + $2AD7D2BB, 15);
    B := C + LRot32(B + (D xor (C or (not A))) + Data + $EB86D391, 21);

    Inc(CurrentHash, A);
    Inc(CurrentHash, B);
    Inc(CurrentHash, C);
    Inc(CurrentHash, 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 := $67452301;
CurrentHash := $EFCDAB89;
CurrentHash := $98BADCFE;
CurrentHash := $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, Sizeof(HashBuffer) - Index);
      Dec(Size, Sizeof(HashBuffer) - Index);
      Inc(PBuf, Sizeof(HashBuffer) - Index);
      Compress;
    end
    else
    begin
      Move(PBuf^, HashBuffer, Size);
      Inc(Index, Size);
      Size := 0;
    end;
end;
HashBuffer := $80;
if (Index >= 56) then Compress;
PDWord(@HashBuffer)^ := LenLo;
PDWord(@HashBuffer)^ := LenHi;
Compress;
Move(CurrentHash, Digest, Sizeof(CurrentHash));
end;

end.

xingbing 发表于 2014-2-7 10:48:54

群主编程功底很深,膜拜。
页: [1]
查看完整版本: EHash.pas--移植自EurekaLog -- by 飘云