Recent

Author Topic: How to use CalcHMAC function?  (Read 5692 times)

Jonvy

  • Jr. Member
  • **
  • Posts: 90
How to use CalcHMAC function?
« on: June 15, 2018, 01:38:17 pm »
Hi all,
I want to use CalcHMAC to calculate a signature use SHA512.

Function from http://keit.co/p/dcpcrypt-hmac-rfc2104/, but I don't know for CalcHMAC(message, key: string; hash: TDCP_hashclass), which parameter should I input as hash?

is hash:TDC_hashclass defined in some where? Where I can tell the function that I use SHA512?

Can someone tell me?

Thanks!

Xor-el

  • Sr. Member
  • ****
  • Posts: 404
Re: How to use CalcHMAC function?
« Reply #1 on: June 15, 2018, 03:35:46 pm »
TDC_hashclasses are defined in DCPcrypt library.

another alternative is this

If you use Lazarus 1.6 and above / FPC 3.0.0 and above
then download this library at

https://github.com/Xor-el/HashLib4Pascal

locate "HashLib4PascalPackage.lpk" in the "Packages" folder of the Library.
Open it and add it to your project.

below is a simple console program calculating the HMAC specified in your post using the library above.


Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. uses
  4.   SysUtils,
  5.   HlpIHashInfo,
  6.   HlpConverters,
  7.   HlpHashFactory;
  8.  
  9. var
  10.   StringToSign, MyKey, MyResult: String;
  11.   LHMAC: IHMAC;
  12.  
  13. begin
  14.   StringToSign := 'The quick brown fox jumps over the lazy dog';
  15.   MyKey := 'key';
  16.   LHMAC := THashFactory.THMAC.CreateHMAC(THashFactory.TCrypto.CreateSHA2_512);
  17.  
  18.   LHMAC.Key := TConverters.ConvertStringToBytes(MyKey, TEncoding.UTF8);
  19.   MyResult := LHMAC.ComputeString(StringToSign, TEncoding.UTF8).ToString();
  20.   WriteLn(MyResult);
  21.   ReadLn();
  22.  
  23. end.
  24.  

Enjoy.  :)
« Last Edit: June 15, 2018, 03:38:12 pm by Xor-el »

Gammatester

  • Jr. Member
  • **
  • Posts: 69
Re: How to use CalcHMAC function?
« Reply #2 on: June 15, 2018, 03:41:21 pm »
If you use the version from https://sourceforge.net/projects/lazarus-ccr/files/DCPcrypt/ you find the definition
Code: Pascal  [Select][+][-]
  1.   TDCP_hashclass= class of TDCP_hash;
  2.  
and therefore your procedure call should be something like this
Code: Pascal  [Select][+][-]
  1. CalcHMAC(message,key, TDCP_sha512)
  2.  
But be aware that the code from your link is buggy, because the hash blocklength is hard-coded as 64, while the SHA-512 blocklength is 128!

For working Pascal HMAC code see http://www.wolfgang-ehrhardt.de/crchash_en.html
« Last Edit: June 15, 2018, 05:15:10 pm by Gammatester »

Jonvy

  • Jr. Member
  • **
  • Posts: 90
Re: How to use CalcHMAC function?
« Reply #3 on: June 16, 2018, 05:19:48 am »
Thanks Xor-el, according to your code,I use HashLib4Pascal calculate the HMAC value.

But for the result HMAC value,it's all characters in it are capital.
My result:
0F231777BD4525B250CFAD3B1A9FF539B07C3B1A4690C0EB236E73FB72D42D10166BEAF5C00E34E0A1C6B6D52CBDBAD1BA1B1760DB4E5E2677B7B09CEDBDA981
When I compare the value on line(https://www.freeformatter.com/hmac-generator.html), all characters are lower case.
Result online:
0f231777bd4525b250cfad3b1a9ff539b07c3b1a4690c0eb236e73fb72d42d10166beaf5c00e34e0a1c6b6d52cbdbad1ba1b1760db4e5e2677b7b09cedbda981

Which one is correct?How can I do for the code?
Because after HMAC,I'll do base64 encode, capital and lower case letter will get different result.
Can you help me for this?



TDC_hashclasses are defined in DCPcrypt library.

another alternative is this

If you use Lazarus 1.6 and above / FPC 3.0.0 and above
then download this library at

https://github.com/Xor-el/HashLib4Pascal

locate "HashLib4PascalPackage.lpk" in the "Packages" folder of the Library.
Open it and add it to your project.

below is a simple console program calculating the HMAC specified in your post using the library above.



Enjoy.  :)

Xor-el

  • Sr. Member
  • ****
  • Posts: 404
Re: How to use CalcHMAC function?
« Reply #4 on: June 16, 2018, 08:48:19 am »
Thanks Xor-el, according to your code,I use HashLib4Pascal calculate the HMAC value.

But for the result HMAC value,it's all characters in it are capital.
My result:
0F231777BD4525B250CFAD3B1A9FF539B07C3B1A4690C0EB236E73FB72D42D10166BEAF5C00E34E0A1C6B6D52CBDBAD1BA1B1760DB4E5E2677B7B09CEDBDA981
When I compare the value on line(https://www.freeformatter.com/hmac-generator.html), all characters are lower case.
Result online:
0f231777bd4525b250cfad3b1a9ff539b07c3b1a4690c0eb236e73fb72d42d10166beaf5c00e34e0a1c6b6d52cbdbad1ba1b1760db4e5e2677b7b09cedbda981

Which one is correct?How can I do for the code?
Because after HMAC,I'll do base64 encode, capital and lower case letter will get different result.
Can you help me for this?



TDC_hashclasses are defined in DCPcrypt library.

another alternative is this

If you use Lazarus 1.6 and above / FPC 3.0.0 and above
then download this library at

https://github.com/Xor-el/HashLib4Pascal

locate "HashLib4PascalPackage.lpk" in the "Packages" folder of the Library.
Open it and add it to your project.

below is a simple console program calculating the HMAC specified in your post using the library above.



Enjoy.  :)

both are correct but in different cases.
Just use LowerCase from https://www.freepascal.org/docs-html/rtl/sysutils/lowercase.html to Convert the UpperCase Strings to LowerCase.

So this new code will give you your answer in LowerCase

Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. uses
  4.   SysUtils,
  5.   HlpIHashInfo,
  6.   HlpConverters,
  7.   HlpHashFactory;
  8.  
  9. var
  10.   StringToSign, MyKey, MyResult: String;
  11.   LHMAC: IHMAC;
  12.  
  13. begin
  14.   StringToSign := 'The quick brown fox jumps over the lazy dog';
  15.   MyKey := 'key';
  16.   LHMAC := THashFactory.THMAC.CreateHMAC(THashFactory.TCrypto.CreateSHA2_512);
  17.  
  18.   LHMAC.Key := TConverters.ConvertStringToBytes(MyKey, TEncoding.UTF8);
  19.   MyResult := LowerCase(LHMAC.ComputeString(StringToSign, TEncoding.UTF8).ToString());
  20.   WriteLn(MyResult);
  21.   ReadLn();
  22.  
  23. end.
  24.  
« Last Edit: June 16, 2018, 08:50:15 am by Xor-el »

Gammatester

  • Jr. Member
  • **
  • Posts: 69
Re: How to use CalcHMAC function?
« Reply #5 on: June 16, 2018, 09:36:06 am »
Because after HMAC,I'll do base64 encode, capital and lower case letter will get different result.
Its pretty useless to encode the hash first in hex and then in base64. Use base64 directly for the hash digest. This is more effective and avoids the case problem.
« Last Edit: June 16, 2018, 09:52:31 am by Gammatester »

Jonvy

  • Jr. Member
  • **
  • Posts: 90
Re: How to use CalcHMAC function?
« Reply #6 on: June 16, 2018, 10:39:49 am »

Its pretty useless to encode the hash first in hex and then in base64. Use base64 directly for the hash digest. This is more effective and avoids the case problem.
[/quote]

Hello,Gammatester,Can you show me an example code how to use base64 directly? Maybe it's another solution.
Currently I just follow the website's documentation to do in this way.(See attachment photos)

Another question is when I tried use:
edtHMAC.Text:=CalcHMAC(sMessage,'0110',tdcp_sha512);

The result I get is some strange characteris, so I have to change to use HashLib4Pascal(See attachment).Do you know the reason?

Xor-el

  • Sr. Member
  • ****
  • Posts: 404
Re: How to use CalcHMAC function?
« Reply #7 on: June 16, 2018, 11:11:09 am »

Its pretty useless to encode the hash first in hex and then in base64. Use base64 directly for the hash digest. This is more effective and avoids the case problem.

Hello,Gammatester,Can you show me an example code how to use base64 directly? Maybe it's another solution.
Currently I just follow the website's documentation to do in this way.(See attachment photos)
[/quote]

here is a simple demo that does what you want, just replace the value of "StringToSign" with your chosen string and "MyKey" with your chosen key.
to use this demo, you need this two packages.
https://github.com/Xor-el/HashLib4Pascal and https://github.com/Xor-el/SimpleBaseLib4Pascal
« Last Edit: June 16, 2018, 11:23:44 am by Xor-el »

Gammatester

  • Jr. Member
  • **
  • Posts: 69
Re: How to use CalcHMAC function?
« Reply #8 on: June 16, 2018, 11:18:30 am »
Another question is when I tried use:
edtHMAC.Text:=CalcHMAC(sMessage,'0110',tdcp_sha512);

The result I get is some strange characteris, so I have to change to use HashLib4Pascal(See attachment).Do you know the reason?
Is this the CalcHMAC from your first post? If yes, this is the raw binary hash digest as (ansi-)string. Feed this into your favorite base64 routine. But as already said: The routine is buggy because it use always a 64 byte buffer, whereas for SHA512 it must be 128 bytes. I guess the length of the CalcHMAC  (your gibberisch string) has length 64.

« Last Edit: June 16, 2018, 11:21:06 am by Gammatester »

Xor-el

  • Sr. Member
  • ****
  • Posts: 404
Re: How to use CalcHMAC function?
« Reply #9 on: June 16, 2018, 11:22:32 am »
Another question is when I tried use:
edtHMAC.Text:=CalcHMAC(sMessage,'0110',tdcp_sha512);

The result I get is some strange characteris, so I have to change to use HashLib4Pascal(See attachment).Do you know the reason?
Is this the CalcHMAC from your first post? If yes, this is the raw binary hash digest as (ansi-)string. Feed this into your favorite base64 routine. But as already said: The routine is buggy because it use always a 64 byte buffer, whereas for SHA512 it must be 128 bytes. I guess the length of the CalcHMAC  (your gibberisch string) has length 64.

you are telling him to do what you told him to avoid in the beginning.??
Quote
Its pretty useless to encode the hash first in hex and then in base64. Use base64 directly for the hash digest. This is more effective and avoids the case problem.

Gammatester

  • Jr. Member
  • **
  • Posts: 69
Re: How to use CalcHMAC function?
« Reply #10 on: June 16, 2018, 11:33:04 am »
you are telling him to do what you told him to avoid in the beginning.??
I guess you know what I mean, and I hope your library can return a byte result not only hex.

Here a complete program with my library from http://www.wolfgang-ehrhardt.de/crchash_en.html. (I wanted to enter your example, but you supply only the images, so no copy and paste),
Code: Pascal  [Select][+][-]
  1. uses
  2.   hash,hmac,hmacsha5,mem_util;
  3. const
  4.   akey = 'Key';
  5. const
  6.   atext: ansistring = 'Message';
  7. var
  8.   ctx: THMAC_Context;
  9.   mac: TSHA512Digest;
  10. begin
  11.   hmac_SHA512_inits(ctx, akey);
  12.   hmac_SHA512_updateXL(ctx, @atext[1], length(atext));
  13.   hmac_SHA512_final(ctx,mac);
  14.   writeln('Hex: ', HexStr(@mac, sizeof(mac)));
  15.   writeln('B64: ', Base64str(@mac, sizeof(mac)));
  16. end.
Code: [Select]
D:\Work\CRC_HASH>D:\FPC304\bin\i386-win32\fpc.exe -B THMACSHA5.PAS
Free Pascal Compiler version 3.0.4 [2017/10/06] for i386
Copyright (c) 1993-2017 by Florian Klaempfl and others
Target OS: Win32 for i386
Compiling THMACSHA5.PAS
Compiling hash.pas
Compiling btypes.pas
Compiling hmac.pas
Compiling hmacsha5.pas
Compiling sha512.pas
Compiling mem_util.pas
Linking THMACSHA5.exe
5888 lines compiled, 0.1 sec, 35360 bytes code, 2244 bytes data
D:\Work\CRC_HASH>THMACSHA5.exe
Hex: 6b7c693d28c311b510c42bb30b07a61e9eb8d21918f257105fe65f1d82df6ac9c9abf0f282c20d646fc2f5015efbe473ac79afd67c756056f60a626afd58795d
B64: a3xpPSjDEbUQxCuzCwemHp640hkY8lcQX+ZfHYLfasnJq/DygsINZG/C9QFe++RzrHmv1nx1YFb2CmJq/Vh5XQ==
« Last Edit: June 16, 2018, 11:40:32 am by Gammatester »

Xor-el

  • Sr. Member
  • ****
  • Posts: 404
Re: How to use CalcHMAC function?
« Reply #11 on: June 16, 2018, 11:40:08 am »
you are telling him to do what you told him to avoid in the beginning.??
I guess you know what I mean, and I hope your library can return a byte result not only hex.

Here a complete program with my library from http://www.wolfgang-ehrhardt.de/crchash_en.html. (I wanted to enter your example, but you supply only the images, so no copy and paste),
Code: Pascal  [Select][+][-]
  1. uses
  2.   hash,hmac,hmacsha2,mem_util;
  3. const
  4.   akey = 'Key';
  5. const
  6.   atext: ansistring = 'Message';
  7. var
  8.   ctx: THMAC_Context;
  9.   mac: TSHA256Digest;
  10. begin
  11.   hmac_SHA256_inits(ctx, akey);
  12.   hmac_SHA256_updateXL(ctx, @atext[1], length(atext));
  13.   hmac_SHA256_final(ctx,mac);
  14.   writeln('Hex: ', HexStr(@mac, sizeof(mac)));
  15. end.
Code: [Select]
D:\Work\CRC_HASH>D:\FPC304\bin\i386-win32\fpc.exe -B THMACSHA.PAS
Free Pascal Compiler version 3.0.4 [2017/10/06] for i386
Copyright (c) 1993-2017 by Florian Klaempfl and others
Target OS: Win32 for i386
Compiling THMACSHA.PAS
Compiling hash.pas
Compiling btypes.pas
Compiling hmac.pas
Compiling hmacsha2.pas
Compiling sha256.pas
Compiling mem_util.pas
Linking THMACSHA.exe
5573 lines compiled, 0.2 sec, 32784 bytes code, 1828 bytes data
D:\Work\CRC_HASH>THMACSHA.exe
Hex: 507285548137b5424eb5572c46496631b7ade8e88ac323c529fa142def26ffa1
B64: UHKFVIE3tUJOtVcsRklmMbet6OiKwyPFKfoULe8m/6E=

yes, my library can return a bytearray, it is shown in the demo attachment above,
you example uses SHA2_256, the poster requested SHA2_512, you might want to correct that.  :)

edit: seems you have corrected it. well done, now the poster has multiple choice to choose from.  :)
« Last Edit: June 16, 2018, 11:46:35 am by Xor-el »

 

TinyPortal © 2005-2018