簡體   English   中英

SHA1 Hash Delphi與Python

[英]SHA1 Hash Delphi vs Python

我嘗試在delphi和python中哈希相同的字符串,但得到不同的哈希值?

蟒蛇:

d = bytes('pr', 'utf-8')
print((sha1(d).digest()).hex())

output: 5498d9b96ed2832e04a90c4ac2ab71f869b2bfdc

德爾福XE7:

...
bytes := TEncoding.UTF8.GetBytes('pr');
BinToHex(sha1Digest2(bytes);
...
output: 1ca1a00b40b8350d15cdce2935965d88b7798719


Function TForm1.sha1Digest2(buffer: TBytes): TBytes;
Var
  HashSHA1: THashSHA1;
Begin
  HashSHA1 := THashSHA1.Create;
  HashSHA1.Update(buffer, SizeOf(buffer));
  Result := HashSHA1.HashAsBytes;
End;

Function TForm1.binToHex(Const bin: Array Of Byte): String;
Const
  HexSymbols = '0123456789ABCDEF';
Var
  I: Integer;
Begin
  SetLength(Result, 2 * Length(bin));
  For I := 0 To Length(bin) - 1 Do
  Begin
    Result[1 + 2 * I + 0] := HexSymbols[1 + bin[I] Shr 4];
    Result[1 + 2 * I + 1] := HexSymbols[1 + bin[I] And $0F];
  End;
End;

我做錯了什么? 提前致謝!

您正在使用Sizeof(buffer)而不是Length(buffer) 因此,您要散列4個字節而不是2個Sizeof(<TBytes>)始終為4,因為在內部它是一個指針。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM