How to use CalcHMAC function? lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- Jonvy · 2018-06-15 11:38 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 · 2018-06-15 13:35 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program project1; uses  SysUtils,  HlpIHashInfo,  HlpConverters,  HlpHashFactory; var  StringToSign, MyKey, MyResult: String;  LHMAC: IHMAC; begin  StringToSign := 'The quick brown fox jumps over the lazy dog';  MyKey := 'key';  LHMAC := THashFactory.THMAC.CreateHMAC(THashFactory.TCrypto.CreateSHA2_512);   LHMAC.Key := TConverters.ConvertStringToBytes(MyKey, TEncoding.UTF8);  MyResult := LHMAC.ComputeString(StringToSign, TEncoding.UTF8).ToString();  WriteLn(MyResult);  ReadLn(); end.  Enjoy. ---------------------------------------------------------------------- Gammatester · 2018-06-15 13:41 If you use the version from https://sourceforge.net/projects/lazarus- ccr/files/DCPcrypt/ you find the definition Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   TDCP_hashclass= class of TDCP_hash; and therefore your procedure call should be something like this Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i CalcHMAC(message,key, TDCP_sha512) 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 ---------------------------------------------------------------------- Jonvy · 2018-06-16 03:19 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: 0F231777BD4525B250CFAD3B1A9FF539B07C3B1A4690C0EB236E73FB72D42D10166BEA F5C00E34E0A1C6B6D52CBDBAD1BA1B1760DB4E5E2677B7B09CEDBDA981 When I compare the value on line(https://www.freeformatter.com/hmac- generator.html), all characters are lower case. Result online: 0f231777bd4525b250cfad3b1a9ff539b07c3b1a4690c0eb236e73fb72d42d10166bea f5c00e34e0a1c6b6d52cbdbad1ba1b1760db4e5e2677b7b09cedbda981 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? Quote from: Xor-el on June 15, 2018, 03:35:46 pmTDC_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 · 2018-06-16 06:48 Quote from: Jonvy on June 16, 2018, 05:19:48 amThanks 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: 0F231777BD4525B250CFAD3B1A9FF539B07C3B1A4690C0EB236E73FB72D42D10166BEA F5C00E34E0A1C6B6D52CBDBAD1BA1B1760DB4E5E2677B7B09CEDBDA981 When I compare the value on line(https://www.freeformatter.com/hmac- generator.html), all characters are lower case. Result online: 0f231777bd4525b250cfad3b1a9ff539b07c3b1a4690c0eb236e73fb72d42d10166bea f5c00e34e0a1c6b6d52cbdbad1ba1b1760db4e5e2677b7b09cedbda981 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? Quote from: Xor-el on June 15, 2018, 03:35:46 pmTDC_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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program project1; uses  SysUtils,  HlpIHashInfo,  HlpConverters,  HlpHashFactory; var  StringToSign, MyKey, MyResult: String;  LHMAC: IHMAC; begin  StringToSign := 'The quick brown fox jumps over the lazy dog';  MyKey := 'key';  LHMAC := THashFactory.THMAC.CreateHMAC(THashFactory.TCrypto.CreateSHA2_512);   LHMAC.Key := TConverters.ConvertStringToBytes(MyKey, TEncoding.UTF8);  MyResult := LowerCase(LHMAC.ComputeString(StringToSign, TEncoding.UTF8).ToString());  WriteLn(MyResult);  ReadLn(); end. ---------------------------------------------------------------------- Gammatester · 2018-06-16 07:36 Quote from: Jonvy on June 16, 2018, 05:19:48 amBecause 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. ---------------------------------------------------------------------- Jonvy · 2018-06-16 08:39 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 · 2018-06-16 09:11 Quote from: Jonvy 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. 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 ---------------------------------------------------------------------- Gammatester · 2018-06-16 09:18 Quote from: Jonvy on June 16, 2018, 10:39:49 amAnother 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. ---------------------------------------------------------------------- Xor-el · 2018-06-16 09:22 Quote from: Gammatester on June 16, 2018, 11:18:30 amQuote from: Jonvy on June 16, 2018, 10:39:49 amAnother 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.?? QuoteIts 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 · 2018-06-16 09:33 Quote from: Xor-el on June 16, 2018, 11:22:32 amyou 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  hash,hmac,hmacsha5,mem_util;const  akey = 'Key';const  atext: ansistring = 'Message';var  ctx: THMAC_Context;  mac: TSHA512Digest;begin  hmac_SHA512_inits(ctx, akey);  hmac_SHA512_updateXL(ctx, @atext[1], length(atext));  hmac_SHA512_final(ctx,mac);  writeln('Hex: ', HexStr(@mac, sizeof(mac)));  writeln('B64: ', Base64str(@mac, sizeof(mac)));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: 6b7c693d28c311b510c42bb30b07a61e9eb8d21918f257105fe65f1d82df6ac9c 9abf0f282c20d646fc2f5015efbe473ac79afd67c756056f60a626afd58795d B64: a3xpPSjDEbUQxCuzCwemHp640hkY8lcQX+ZfHYLfasnJq/DygsINZG/C9QFe++Rzr Hmv1nx1YFb2CmJq/Vh5XQ== ---------------------------------------------------------------------- Xor-el · 2018-06-16 09:40 Quote from: Gammatester on June 16, 2018, 11:33:04 amQuote from: Xor- el on June 16, 2018, 11:22:32 amyou 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  hash,hmac,hmacsha2,mem_util;const  akey = 'Key';const  atext: ansistring = 'Message';var  ctx: THMAC_Context;  mac: TSHA256Digest;begin  hmac_SHA256_inits(ctx, akey);  hmac_SHA256_updateXL(ctx, @atext[1], length(atext));  hmac_SHA256_final(ctx,mac);  writeln('Hex: ', HexStr(@mac, sizeof(mac)));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.