中国开发网: 论坛: Delphi/BCB: 贴子 494059
python: file 计算hash值参考
var
Hash: TDCP_hash;
Source: TFileStream;
Digest: array[0..31] of byte;
i, j, Found: integer;
s: string;
SRec: TSearchRec;
begin
if ParamCount= 0 then
begin
Writeln('HashFile [-h<hashname>] <filename1> [<filename2> ... ]');
Exit;
end;
if Copy(ParamStr(1),1,2)= '-h' then { Select the type of hash we want to use }
begin
i:= 2;
Hash:= DCPhashfromname(Copy(ParamStr(1),3,Length(ParamStr(1))-2),nil);
end
else
begin
i:= 1;
Hash:= TDCP_md5.Create(nil);
end;
if Hash= nil then
begin
Writeln('Hash algorithm not implemented');
Exit;
end;
for i:= i to ParamCount do begin
Found:= FindFirst(ParamStr(i),faReadOnly or faSysFile,SRec); { Use FindFirst so we can specify wild cards in the filename }
if Found<> 0 then
Writeln('File not found - ',ParamStr(i));
while Found= 0 do
begin
Hash.Init; { Initialize the hash }
try
Source:= TFileStream.Create(SRec.Name,fmOpenRead); { Open the file for reading }
Hash.UpdateStream(Source,Source.Size); { Hash the file }
Hash.Final(Digest); { Produce the digest }
s:= SRec.Name+' ';
while Length(s)< 30 do
s:= s+' ';
for j:= 0 to ((Hash.HashSize shr 3) - 1) do
s:= s + IntToHex(Digest[j],2);
Writeln(s);
Source.Free;
Source:= nil;
except
Writeln('Unable to access file - ',SRec.Name); { Oh no! }
Hash.Burn;
Source.Free;
end;
Found:= FindNext(SRec); { Find the next file }
end;
FindClose(SRec);
end;
Hash.Free;

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录