Delphi 11 HMACSha512 函数

欢迎加入全网最大Delphi 技术交流群 682628230

备用,是否正确待测

其一 需要opensll ,其二不需要

uses   IdHMACSHA1, System.Hash,IdCoderMIME, IdGlobal;
 
function EncryptHMACSha512(Input, AKey: String): String;
  var
    SHA512    : TIdHMACSHA512;
   Encoder: TEncoding;
   IDBytes: TIDBytes;
begin
    SHA512:=TIdHMACSHA512.Create;
    try
       SHA512.Key:=TIdDecoderMIME.DecodeBytes(AKey);
      Result:=TIdEncoderMIME.EncodeBytes(SHA512.HashValue(ToBytes(Input)));
    finally
      SHA512.Free;
    end;
end;
unit EncryptHMACSha512;
 interface
uses   System.SysUtils,System.Hash;
 
function EnHMACSHA512(Input, AKey: String): String;
function HMACSHA512(const aKey,aData:TBytes):TBytes;
  function BytesToHex(Bytes: TBytes): string;
implementation
  function HMACSHA512(const aKey,aData:TBytes):TBytes;
  begin
    Result:= THashSHA2.GetHMACAsBytes(aData,aKey,SHA512);
  end;
 
function BytesToHex(Bytes: TBytes): string;
var
  i: Integer;
  s: TStringBuilder;
begin
  s := TStringBuilder.Create;
  try
    for i := 0 to Length(Bytes) - 1 do
      s.Append(IntToHex(Bytes[i], 2));
    Result := s.ToString;
  finally
    s.Free;
  end;
end;
 
function EnHMACSHA512(Input, AKey: String): String;
  var
   key,data:TBytes;
   HmacResult:TBytes;
   HmacHex:string;
begin
   key:=TEncoding.UTF8.GetBytes(AKey);
   data:=TEncoding.UTF8.GetBytes(Input);
   HmacResult:=HMACSHA512(key,data);
   HmacHex:=BytesToHex(HmacResult);
   Result:=HmacHex;
end;
end.
© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享