Recent

Author Topic: how to using CryptoLib4Pascal and HashLib4Pascal  (Read 1621 times)

gfmstudio

  • Newbie
  • Posts: 1
how to using CryptoLib4Pascal and HashLib4Pascal
« on: April 16, 2024, 06:29:30 am »
Sorry in advance, I'm a beginner programmer, I want to ask how to install cryptolib / hashlib in my form project. even though I have installed the packages via the Online Package Manager and it has been installed but I don't know how to use it, this happens to be a project that requires HMAC SHA-512.

paweld

  • Hero Member
  • *****
  • Posts: 1367
Re: how to using CryptoLib4Pascal and HashLib4Pascal
« Reply #1 on: April 16, 2024, 07:18:35 am »
HashLib4Pascal is not a visual component, so you won't click it off the component list.
To add an installed non-visual component to your project, click on menu "Project > Project inspector ...". (1)
In the newly opened window, add a new dependency: "Add > New requirement" (2) and you select "HashLib4PascalPackage" and click "OK" (3) - now you can close the "Project inspector" window.
Then in the uses section you need to add the modules needed to generate HMAC:
Code: Pascal  [Select][+][-]
  1. uses
  2.  //...
  3.  HlpIHashInfo, HlpConverters, HlpHashFactory;
.
and the code that generates the HMAC looks something like this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   LHMAC: IHMAC;
  4.   barr: TBytes;
  5.   tekst, secret_key, hmac512_value: String;
  6. begin
  7.   tekst := 'test';
  8.   secret_key := '123';
  9.   LHMAC := THashFactory.THMAC.CreateHMAC(THashFactory.TCrypto.CreateSHA2_512);
  10.   LHMAC.Key := TConverters.ConvertStringToBytes(secret_key, TEncoding.UTF8);
  11.   hmac512_value := LHMAC.ComputeString(tekst, TEncoding.UTF8).ToString();
  12.   ShowMessage(hmac512_value);
  13. end;    
Best regards / Pozdrawiam
paweld

AlexTP

  • Hero Member
  • *****
  • Posts: 2561
    • UVviewsoft
Re: how to using CryptoLib4Pascal and HashLib4Pascal
« Reply #2 on: April 16, 2024, 07:28:04 am »
@paweld,
I added your example to the wiki
https://wiki.freepascal.org/HashLib4Pascal#Example

Thaddy

  • Hero Member
  • *****
  • Posts: 16939
  • Ceterum censeo Trump esse delendam
Re: how to using CryptoLib4Pascal and HashLib4Pascal
« Reply #3 on: April 16, 2024, 07:51:45 am »
There is also a very recently updated HMAC 512 in fcl-hash (trunk/main)that I can recommend since it is in the standard distribution. That said, our friend Xor-El's code is more comprehensive. Just to let you know there are multiple choices. That code is by Michael I believe.
I actually prefer the latter for ease of use for HMAC 256/386/512 since it has a nicer interface based on class methods. Both libraries are standards compliant, so pick and choose.
Comes with good example for HMAC 256, but the others are plug and play with the same example.
« Last Edit: April 16, 2024, 08:04:14 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

paweld

  • Hero Member
  • *****
  • Posts: 1367
Re: how to using CryptoLib4Pascal and HashLib4Pascal
« Reply #4 on: April 16, 2024, 07:59:04 am »
@AlexTP: Thank you
Best regards / Pozdrawiam
paweld

 

TinyPortal © 2005-2018