我有以下用delphi编写的代码。
with TIdHashMessageDigest5.Create do begin
st2.Position := 0;
Digest := HashValue( st2 );
SetLength( Hash, 16 );
Move( Digest, Hash[1], 16);
Free;
end;
我需要将其转换为使用SHA1哈希。我在库中找不到SHA1类型。有人可以帮忙吗?我在互联网上寻求帮助但找不到任何帮助。
答案 0 :(得分:4)
见这里:
https://sergworks.wordpress.com/2014/10/25/high-performance-hash-library/
https://sourceforge.net/projects/sha1implementat/
http://www.colorfultyping.com/generating-a-sha-1-checksum-for-a-given-class-type/
顺便说一下,你没有提到你的Delphi版本。如果您使用的是现代版本(XE以上版本),我认为其标准库应该支持SHA-1,MD5等。你可以这样做:
uses IdHashSHA;
function SHA1FromString(const AString: string): string;
var
SHA1: TIdHashSHA1;
begin
SHA1 := TIdHashSHA1.Create;
try
Result := SHA1.HashStringAsHex(AString);
finally
SHA1.Free;
end;
end;
答案 1 :(得分:0)
您似乎正在使用Indy 9,它不支持SHA1。在Indy 10中添加了SHA1(以及一些其他哈希,包括其他一些SHA)。TIdHash
的接口也在Indy 10中重写。在其他更改中,HashValue()
方法被替换为新的Hash...()
和Hash...AsHex()
方法(HashString(AsHex)
,HashStream(AsHex)
,HashBytes(AsHex)
),例如:
uses
..., IdHash, IdHashMessageDigest;
var
Hash: TIdBytes;
begin
with TIdHashMessageDigest5.Create do
try
st2.Position := 0;
Hash := HashStream( st2 );
finally
Free;
end;
// use Hash as needed...
end;
uses
..., IdHash, IdHashSHA;
var
Hash: TIdBytes;
begin
with TIdHashSHA1.Create do
try
st2.Position := 0;
Hash := HashStream( st2 );
finally
Free;
end;
// use Hash as needed...
end;
答案 2 :(得分:0)
还有两个选择:
unit Spring.Cryptography.SHA;
TSHA1 = class(THashAlgorithmBase, ISHA1)
http://lockbox.seanbdurkin.id.au/HomePage
unit LbProc;
procedure StreamHashSHA1(var Digest : TSHA1Digest; AStream : TStream);
procedure FileHashSHA1(var Digest : TSHA1Digest; const AFileName : string);