Delphi XE中的SHA1散列

前端之家收集整理的这篇文章主要介绍了Delphi XE中的SHA1散列前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在实施XML数字签名.我从小步骤开始,所以现在我想解决SHA-1哈希的问题.

在SO中有很多关于此的问题:

> Digitially Sign Key with Lockbox
> Encryption library for Delphi
> Convert this php digital signing to Delphi
> Delphi: is there a version of LockBox for Delphi-XE
> Delphi 2010 Cryptography libraries

……可能还有更多.但是,我正在使用Delphi XE.到目前为止,我已经尝试过LockBox 2(Songbeamer和Sourceforge版本),Lock Box 3,DCPCrypto2和其他一些(Hashes是一个易于使用的单元,它使用Windows加密功能)

我准备了一个小型试验台,它给了我以下内容

LockBox2

Failed: 1 ('abc') 
       Got: '9f04f41a848514162050e3d68c1a7abb441dc2b5'
  Expected: 'a9993e364706816aba3e25717850c26c9cd0d89d'
Failed: 2 ('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq') 
       Got: '51d7d8769ac72c409c5b0e3f69c60adc9a039014'
  Expected: '84983e441c3bd26ebaae4aa1f95129e5e54670f1'

LockBox3

Failed: 1 ('abc') 
       Got: '9f04f41a848514162050e3d68c1a7abb441dc2b5'
  Expected: 'a9993e364706816aba3e25717850c26c9cd0d89d'
Failed: 2 ('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq') 
       Got: '51d7d8769ac72c409c5b0e3f69c60adc9a039014'
  Expected: '84983e441c3bd26ebaae4aa1f95129e5e54670f1'

DCPCrypto2

Failed: 1 ('abc') 
       Got: '9f04f41a848514162050e3d68c1a7abb441dc2b5'
  Expected: 'a9993e364706816aba3e25717850c26c9cd0d89d'
Failed: 2 ('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq') 
       Got: '51d7d8769ac72c409c5b0e3f69c60adc9a039014'
  Expected: '84983e441c3bd26ebaae4aa1f95129e5e54670f1'

哈希

Test 1 passes
Test 2 passes

您是否已成功在Delphi XE下编译提到的库并使它们提供适当的值?我对DCPCrypt2 SelfTest程序特别感兴趣.

编辑:我添加this answer的固定源代码.谢谢大家的帮助,非常感谢.

解决方法

莱昂纳多,我认为当你使用函数来散列传递字节数组(缓冲区)的字符串时,你的问题是UNICODE.所以当您在Delphi XE中传递abc字符串时,您正在散列一个像这样的缓冲区61 00 62 00 63 00(十六进制表示)

检查此示例应用程序,该应用程序使用Jwscl库中的Windows加密函数(JEDI Windows安全代码库)

program Jwscl_TestHash;

{$APPTYPE CONSOLE}

uses
  JwsclTypes,JwsclCryptProvider,Classes,SysUtils;

function GetHashString(Algorithm: TJwHashAlgorithm; Buffer : Pointer;Size:Integer) : AnsiString;
var
  Hash: TJwHash;
  HashSize: Cardinal;
  HashData: Pointer;
  i       : Integer;
begin
  Hash := TJwHash.Create(Algorithm);
  try
    Hash.HashData(Buffer,Size);
    HashData := Hash.RetrieveHash(HashSize);
    try
        SetLength(Result,HashSize*2);
        BinToHex(PAnsiChar(HashData),PAnsiChar(Result),HashSize);
    finally
      TJwHash.FreeBuffer(HashData);
    end;
  finally
    Hash.Free;
  end;
end;


function GetHashSHA(FBuffer : AnsiString): AnsiString;
begin
   Result:=GetHashString(haSHA,@FBuffer[1],Length(FBuffer));
end;

function GetHashSHA_Unicode(FBuffer : String): String;
begin
   Result:=GetHashString(haSHA,Length(FBuffer)*SizeOf(Char));
end;

begin
 try
     Writeln(GetHashSHA('abc'));
     Writeln(GetHashSHA('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'));
     Writeln(GetHashSHA_Unicode('abc'));
     Writeln(GetHashSHA_Unicode('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'));
     Readln;
 except
    on E:Exception do
    begin
        Writeln(E.Classname,':',E.Message);
        Readln;
    end;
 end;

end.

这回归

abc AnsiString

A9993E364706816ABA3E25717850C26C9CD0D89D

abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq AnsiString

84983E441C3BD26EBAAE4AA1F95129E5E54670F1 for

abc unicode

9F04F41A848514162050E3D68C1A7ABB441DC2B5

abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq Unicode

51D7D8769AC72C409C5B0E3F69C60ADC9A039014

猜你在找的Delphi相关文章