[SOLVED] GEt a keyed SHA256 hash from string lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- torbente · 2018-06-01 18:08 Sometime ago, getmem gracefully help me a lot in this topic: https://forum.lazarus.freepascal.org/index.php/topic,39821.msg274338.h tml#msg274338 Now, i need the same, but using SHA256, so this is what im doing: (using dcpcrypt2) 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 function GetSignature(const AText, AKey: String): String;var  SHA256: TDCP_sha256;  AB: array[0..31] of byte;  I: Integer;begin  Result := '';  SHA256 := TDCP_sha256.Create(nil);  try    SHA256.Init;    SHA256.UpdateStr(AText + AKey);    SHA256.Final(AB);    for I := 0 to 31 do      Result := Result + IntToHex(AB[i], 2);  finally    SHA256.Free;  end;end; But im receiving the "Signature for this request is not valid." message ALWAYS. I used this page to test my code: https://www.freeformatter.com/hmac-generator.html And im getting different hashes. Any idea? ---------------------------------------------------------------------- Gammatester · 2018-06-01 18:49 You are not using a valid HMAC construction: HMAC is not HASH(Text+Key). Actually it is HMAC(key,text) = hash((const1 xor key) || hash((const2 xor key) || text)) , see http://www.wolfgang- ehrhardt.de/hash_intro.html. ---------------------------------------------------------------------- Xor-el · 2018-06-01 18:59 If you want to calculate hmac, you can use HashLib4Pascal https://github.com/Xor-el/HashLib4Pascal ---------------------------------------------------------------------- Gammatester · 2018-06-01 20:08 Here is a complete test program for HMAC compiled with http://www.wolfgang-ehrhardt.de/crc_hash_2018-01-01.zip 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(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 Linking THMACSHA.exe 14 lines compiled, 0.1 sec, 30848 bytes code, 1764 bytes data D:\Work\CRC_HASH>THMACSHA.exe 507285548137b5424eb5572c46496631b7ade8e88ac323c529fa142def26ffa1 and verified with your online-calculator. ---------------------------------------------------------------------- torbente · 2018-06-01 21:54 I used it and worked 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 function getsha256signature(StringToSign , mykey:string):string;var  LHMAC: IHMAC;beginLHMAC := THashFactory.THMAC.CreateHMAC(THashFactory.TCrypto .CreateSHA2_256);LHMAC.Key := TConverters.ConvertStringToBytes(MyKey, TEncoding.UTF8);getsha256signature := LHMAC.ComputeString(StringToSign, TEncoding.UTF8).ToString();end;  but my app start crashing, probably due to this previous function i had: 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 function GetTimestamp():string;BeginGetTimestamp := inttostr(Trunc((Now - EncodeDate(1970, 1 ,1)) * 24 * 60 * 60));end; ANy idea if this can be done with dcpcrypt? Quote from: Xor-el on June 01, 2018, 08:59:57 pmIf you want to calculate hmac, you can use HashLib4Pascal https://github.com/Xor-el/HashLib4Pascal @Gammatester i will try your code. Thanks!! ---------------------------------------------------------------------- Xor-el · 2018-06-01 22:57 Quote from: torbente on June 01, 2018, 11:54:10 pmI used it and worked 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 function getsha256signature(StringToSign , mykey:string):string;var  LHMAC: IHMAC;beginLHMAC := THashFactory.THMAC.CreateHMAC(THashFactory.TCrypto .CreateSHA2_256);LHMAC.Key := TConverters.ConvertStringToBytes(MyKey, TEncoding.UTF8);getsha256signature := LHMAC.ComputeString(StringToSign, TEncoding.UTF8).ToString();end;  but my app start crashing, probably due to this previous function i had: 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 function GetTimestamp():string;BeginGetTimestamp := inttostr(Trunc((Now - EncodeDate(1970, 1 ,1)) * 24 * 60 * 60));end; ANy idea if this can be done with dcpcrypt? Quote from: Xor-el on June 01, 2018, 08:59:57 pmIf you want to calculate hmac, you can use HashLib4Pascal https://github.com/Xor-el/HashLib4Pascal @Gammatester i will try your code. Thanks!! don't know why your code is crashing but HashLib4Pascal is obviously not the cause. can you upload your code so we can check the cause of the crash? ---------------------------------------------------------------------- torbente · 2018-06-02 05:50 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 don't know why your code is crashing but HashLib4Pascal is obviously not the cause.can you upload your code so we can check the cause of the crash?I think HashLib4Pascal is obviously the cause; i removed it from my project and i had no more troubles since then. IF your are the library developer, check why it crashes with my custom timestamp function (if i remove GetTimestamp function, then getsha256signature works and the app do not crash) Anyway, im cleaning my code to see if something else could be wrong. ---------------------------------------------------------------------- Xor-el · 2018-06-02 09:56 Quote from: torbente on June 02, 2018, 07:50:02 amCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i don't know why your code is crashing but HashLib4Pascal is obviously not the cause.can you upload your code so we can check the cause of the crash?I think HashLib4Pascal is obviously the cause; i removed it from my project and i had no more troubles since then. IF your are the library developer, check why it crashes with my custom timestamp function (if i remove GetTimestamp function, then getsha256signature works and the app do not crash) Anyway, im cleaning my code to see if something else could be wrong. Yes I am the library developer. I suggest you upload a small reproducible project where it crashes so we can help you out. ---------------------------------------------------------------------- torbente · 2018-06-02 10:54 I could send you the entire project by private. So you coudl check if it is something with your library. ---------------------------------------------------------------------- Xor-el · 2018-06-02 11:02 Quote from: torbente on June 02, 2018, 12:54:40 pmI could send you the entire project by private. So you coudl check if it is something with your library. Please Go ahead. ---------------------------------------------------------------------- guest48180 · 2018-06-08 05:28 It'd be nice to know how this turned out. ---------------------------------------------------------------------- Xor-el · 2018-06-08 08:28 Quote from: Landslyde on June 08, 2018, 07:28:55 amIt'd be nice to know how this turned out. So after some investigations, it was discovered that it was a bug in his own code when parsing floating points. It was not related to HashLib4Pascal or DCPCrypt for that matter. The bug was not even related to his custom timestamp function. ---------------------------------------------------------------------- torbente · 2018-06-20 01:08 Yes, it was a lack of error handlers in my code. I implemented all the necessary handlers and i had no more errors. Thanks  Xor-el for your time ---------------------------------------------------------------------- Xor-el · 2018-06-20 07:07 Quote from: torbente on June 20, 2018, 03:08:24 amYes, it was a lack of error handlers in my code. I implemented all the necessary handlers and i had no more errors. Thanks  Xor-el for your time  You are welcome, glad you finally fixed it. ---------------------------------------------------------------------- william22 · 2018-10-13 13:40 Dear Torbente, If I'm right you wrote this for the KrakenAPI. I'm working on a same project, but am stuck. Could you share your code-project with me please?