使用 Delphi 计算 Android 中的 HMAC 哈希值 10

Calculating HMAC hash in Android using Delphi 10

我在 VCL 应用程序中使用外部 DLL 来计算 HMAC sha256 哈希,https://www.chilkatsoft.com/delphiDll.asp,我需要一种方法来为 Delphi 中的 Android 10,我不能'在 delphi 中找到适用于 android 的任何内容, 这是使用 DLL

执行此操作的代码
function TForm2.HMAC(const s: pwidechar): string;
var
crypt: HCkCrypt2;
success: Boolean;
mac: PWideChar;

begin
crypt := CkCrypt2_Create();

//  Any string argument automatically begins the 30-day trial.
success := CkCrypt2_UnlockComponent(crypt,'Anything for 30-day trial.');
if (success <> True) then
  begin
    Exit;
  end;

//  The output will be Hex, so set the EncodingMode:
CkCrypt2_putEncodingMode(crypt,'hex');

//  Set the hash algorithm:
//  Choices are: md5, sha-1, sha256, sha384, sha512, md2, haval
CkCrypt2_putHashAlgorithm(crypt,'sha256');

//  Set the HMAC key:
CkCrypt2_SetHmacKeyEncoded(crypt,'my key','ascii');
mac := CkCrypt2__hmacStringENC(crypt,s);
Result := (mac);

CkCrypt2_Dispose(crypt);

end;

更新

我找到了一段代码来获取 HMAC SHA256..但我无法理解 它是如何工作的,就像上面的代码一样?

  var
    Hash: TIdHMACSHA256 ;
    HashValue: TBytes;
  begin
    SetCurrentDir(ExtractFilePath(ParamStr(0)));
    Hash := TIdHMACSHA256 .Create;
    try
      Hash.Key := TEncoding.ASCII.GetBytes('devaee2');
      HashValue := Hash.HashValue(TFile.ReadAllBytes('menu.xml'));
      // HashValue is an empty array, why?
      Tag := Length(HashValue);
      TFile.WriteAllBytes('menu.xml.hash', HashValue);

    finally
      FreeAndNil(Hash);
    end;
  end;

有一个THashSHA2.GetHMAC() method in the System.Hash单位。它有一个 AHashVersion 参数,您可以将其设置为 TSHA2Version.SHA256(默认值)。