Forum > Packages and Libraries

how to using CryptoLib4Pascal and HashLib4Pascal

(1/1)

gfmstudio:
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:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses //... HlpIHashInfo, HlpConverters, HlpHashFactory;.
and the code that generates the HMAC looks something like this:
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button1Click(Sender: TObject);var  LHMAC: IHMAC;  barr: TBytes;  tekst, secret_key, hmac512_value: String;begin  tekst := 'test';  secret_key := '123';  LHMAC := THashFactory.THMAC.CreateHMAC(THashFactory.TCrypto.CreateSHA2_512);  LHMAC.Key := TConverters.ConvertStringToBytes(secret_key, TEncoding.UTF8);  hmac512_value := LHMAC.ComputeString(tekst, TEncoding.UTF8).ToString();  ShowMessage(hmac512_value);end;    

AlexTP:
@paweld,
I added your example to the wiki
https://wiki.freepascal.org/HashLib4Pascal#Example

Thaddy:
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.

paweld:
@AlexTP: Thank you

Navigation

[0] Message Index

Go to full version