文件CRC和MD5校验
CRC和MD5用于文件和数据的传输校验,以确认是否接收成功。
unit CRCMD5;interface
{ 获取文件CRC校验码 }
function GetFileCRC(const iFileName: string): String;
{ 获取字符串CRC校验码 }
function GetStringCRC(const Str: string): Cardinal;
{ 取文件MD5码 }
function GetFileMD5(const iFileName: string): String;implementationuses Classes, IdHashMessageDigest, IdHashCRC;{ 获取文件CRC校验码 }
function GetFileCRC(const iFileName: string): String;
varMemSteam: TMemoryStream;MyCRC : TIdHashCRC32;
beginMemSteam := TMemoryStream.Create;MemSteam.LoadFromFile(iFileName);MyCRC := TIdHashCRC32.Create;Result := MyCRC.HashStreamAsHex(MemSteam);MyCRC.Free;MemSteam.Free;
end;
{ 获取字符串CRC校验码 }
function GetStringCRC(const Str: string): Cardinal;
varMyCRC: TIdHashCRC32;
beginMyCRC := TIdHashCRC32.Create;Result := MyCRC.HashValue(Str);MyCRC.Free;
end;
{ 取文件MD5码 }
function GetFileMD5(const iFileName: string): String;
varMemSteam: TMemoryStream;MyMD5 : TIdHashMessageDigest5;
beginMemSteam := TMemoryStream.Create;MemSteam.LoadFromFile(iFileName);MyMD5 := TIdHashMessageDigest5.Create;Result := MyMD5.HashStreamAsHex(MemSteam);MyMD5.Free;MemSteam.Free;
end;
end.