delphi 11 HMACSha512
备用,是否正确待测
其一 需要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; 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.

浙公网安备 33010602011771号