-1

i'm using external DLL in VCL apps to calculate the HMAC sha256 hash,https://www.chilkatsoft.com/delphiDll.asp ,i need a way to do it for Android in Delphi 10, i couldn't find any that works for android in delphi, this the code to do it using 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;

UPDATE

i've found a piece of code to get the HMAC SHA256..but i couldn't understand how it works, like the above code ?

  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;
unknown
  • 65
  • 1
  • 2
  • 9
  • Indy in general works just fine on Android 6. It is only Indy's use of OpenSSL that does not work on Android 6. However, Indy's hash and HMAC classes are not *directly* dependent on OpenSSL specifically, but they can use OpenSSL when it is available. Indy's `IdFIPS` unit has a bunch of function pointers that you can point to any hashing API you want (the `IdSSLOpenSSLHeaders` unit points them to OpenSSL functions) for hashes that Indy does not implement natively (some hashes, like MD5 and SHA1, are implemented natively). – Remy Lebeau Nov 30 '16 at 23:26
  • Looking at the `TIdHMAC` class, it looks like it might skips some of the native hash implementations, relying on external libraries. I'll have to look into that. – Remy Lebeau Nov 30 '16 at 23:27
  • Looks like only `TIdHMACSHA1` was affected. I've updated it to now use Indy's native SHA-1 implementation when an external hashing library is not available. `TIdHMACMD5` was already using Indy's native MD5 implementation. – Remy Lebeau Nov 30 '16 at 23:52
  • can i use it to calculate the HMAC SHA256 ? @RemyLebeau – unknown Dec 01 '16 at 07:39
  • yes, with caveat. As you discovered, Indy has a `TIdHMACSHA256` class, which uses `TIdHashSHA256` internally. But Indy does not currently have its own SHA-256 implementation, so `TIdHashSHA256` requires an external library be assigned to the SHA-256 function pointers in the `IdFIPS` unit. Without that, the hash fails, which is why your output is empty (last night [I made an update](http://indyproject.org/Sockets/Blogs/ChangeLog/20161130.aspx) so `TIdHMACSHA...` classes will raise an exception instead). If you assign a SHA-256 library for Indy to use, then `TIdHMACSHA256` will work. – Remy Lebeau Dec 01 '16 at 16:42

1 Answers1

2

There is a THashSHA2.GetHMAC() method in the System.Hash unit. It has an AHashVersion parameter that you can set to TSHA2Version.SHA256 (which is the default value).

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
EugeneK
  • 2,164
  • 1
  • 16
  • 21