CRC算法单元文件

unit Crc32;

interface

uses
 Windows,SysUtils,Classes;

const  //Crc32表
 Table:Array[0..255] of DWORD=
 ( {post.abstract}000000,  073096, $EE0E612C, 0951BA,
   6DC419, 6AF48F, $E963A535, E6495A3,
   {post.abstract}EDB8832, DCB8A4, $E0D5E91E, D2D988,
   B64C2B, EB17CBD, $E7B82D07, BF1D91,
   DB71064, AB020F2, $F3B97148, BE41DE,
   ADAD47D, DDDE4EB, $F4D4B551, D385C7,
   6C9856, 6BA8C0, $FD62F97A, A65C9EC,
   015C4F, 066CD9, $FA0F3D63, D080DF5,
   B6E20C8, C69105E, $D56041E4, $A2677172,
   C03E4D1, B04D447, $D20D85FD, $A50AB56B,
   B5A8FA, B2986C, $DBBBC9D6, $ACBCF940,
   D86CE3, DF5C75, $DCD60DCF, $ABD13D59,
   D930AC, DE003A, $C8D75180, $BFD06116,
   B4F4B5, B3C423, $CFBA9599, $B8BDA50F,
   02B89E, F058808, $C60CD9B2, $B10BE924,
   F6F7C87, 684C11, $C1611DAB, $B6662D3D,
   DC4190, DB7106, D220BC, $EFD5102A,
   B18589, B6B51F, FBFE4A5, $E8B8D433,
   07C9A2, {post.abstract}F00F934, 09A88E, $E10E9818,
   F6A0DBB, 6D3D2D, 646C97, $E6635C01,
   B6B51F4, C6C6162, 6530D8, $F262004E,
   C0695ED, B01A57B, 08F4C1, $F50FC457,
   B0D9C6, B7E950, BBEB8EA, $FCB9887C,
   DD1DDF, DA2D49, CD37CF3, $FBD44C65,
   DB26158, AB551CE, $A3BC0074, $D4BB30E2,
   ADFA541, DD895D7, $A4D1C46D, $D3D6F4FB,
   69E96A, 6ED9FC, $AD678846, $DA60B8D0,
   042D73, 031DE5, $AA0A4C5F, $DD0D7CC9,
   05713C, 0241AA, $BE0B1010, $C90C2086,
   68B525, 6F85B3, $B966D409, $CE61E49F,
   EDEF90E, D9C998, $B0D09822, $C7D7A8B4,
   B33D17, EB40D81, $B7BD5C3B, $C0BA6CAD,
   $EDB88320, ABFB3B6, B6E20C, B1D29A,
   $EAD54739, DD277AF, DB2615, DC1683,
   $E3630B12, 643B84, {post.abstract}D6D6A3E, A6A5AA8,
   $E40ECF0B, 09FF9D, {post.abstract}A00AE27, D079EB1,
   $F00F9344, 08A3D2, E01F268, 06C2FE,
   $F762575D, 6567CB, 6C3671, E6B06E7,
   $FED41B76, D32BE0, DA7A5A, DD4ACC,
   $F9B9DF6F, EBEEFF9, B7BE43, B08ED5,
   $D6D6A3E8, $A1D1937E, D8C2C4, FDFF252,
   $D1BB67F1, $A6BC5767, FB506DD, B2364B,
   $D80D2BDA, $AF0A1B4C, 034AF6, 047A60,
   $DF60EFC3, $A867DF55, 6E8EEF, 69BE79,
   $CB61B38C, $BC66831A, 6FD2A0, 68E236,
   $CC0C7795, $BB0B4703, 0216B9, 05262F,
   $C5BA3BBE, $B2BD0B28, BB45A92, CB36A04,
   $C2D7FFA7, $B5D0CF31, CD99E8B, BDEAE1D,
   B64C2B0, $EC63F226, 6AA39C, 6D930A,
   C0906A9, $EB0E363F, 076785, 005713,
   BF4A82, $E2B87A14, BB12BAE, {post.abstract}CB61B38,
   D28E9B, $E5D5BE0D, CDCEFB7, {post.abstract}BDBDF21,
   D3D2D4, $F1D4E242, DDB3F8, FDA836E,
   BE16CD, $F6B9265B, FB077E1, B74777,
   085AE6, $FF0F6A70, 063BCA, 010B5C,
   F659EFF, $F862AE69, 6BFFD3, 6CCF45,
   $A00AE278, $D70DD2EE, E048354, 03B3C2,
   $A7672661, $D06016F7, 69474D, E6E77DB,
   $AED16A4A, $D9D65ADC, DF0B66, D83BF0,
   $A9BCAE53, $DEBB9EC5, B2CF7F, B5FFE9,
   $BDBDF21C, $CABAC28A, B39330, B4A3A6,
   $BAD03605, $CDD70693, DE5729, D967BF,
   $B3667A2E, $C4614AB8, D681B02, A6F2B94,
   $B40BBE37, $C30C8EA1, A05DF1B, D02EF8D);

  procedure GetCRC32File(FileName:string;var CRC32 WORD);
  function GetCrc32Str(s: string; Seed: LongInt):string;
  
implementation

procedure GetCRC32File(FileName:string;var CRC32 WORD);
var
 F:file;
 BytesRead WORD;
 Buffer:array[1..65521] of Byte;
 i:Word;
begin
 FileMode :=0;
 CRC32 :=$ffffffff;
 {$I-}
 AssignFile(F,FileName);
 Reset(F,1);
 if IoResult = 0 then
   begin
     repeat
     BlockRead(F,Buffer,Sizeof(Buffer),BytesRead);
     for i := 1 to BytesRead do
       CRC32 := (CRC32 shr 8) xor Table[Buffer[i] xor (CRC32 and {post.abstract}0000ff)];
     until BytesRead = 0;
   end;
 CloseFile(F);
 {$I+}
 CRC32 := not CRC32;
end;
   
function GetCrc32Str(s: string; Seed: LongInt):string;
var
 Count: Integer;
 CrcVal: LongInt;
begin
 CrcVal := Seed;
 for Count := 1 to Length(s) do
   CrcVal := Table[Byte(CrcVal xor DWORD(Ord(s[Count])))] xor ((CrcVal shr 8) and {post.abstract}FFFFFF);
 Result := IntToHex(not(CrcVal), 8);
end;


end.

//////////////////////////////////////////////////////
调用:
uses Crc32;

procedure TForm1.Button1Click(Sender: TObject);
begin
 Edit1.Text:=GetCrc32Str('11111111',8);//这里取指定字符串的CRC32校验值;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
 FileStr:String;
 crc: DWORD;  
begin
 FileStr:=Application.ExeName;//这里取指定的文件的crc32校验值;
 GetCRC32File(FileStr,crc);
 if crc<>0 then
   Edit2.Text:=PChar(IntToHex(crc,6));
end;

你可能感兴趣的:(算法,文件,CRC,休闲,单元)