Forum > Networking and Web Programming
[SOLVED] GEt a keyed SHA256 hash from string
torbente:
Sometime ago, getmem gracefully help me a lot in this topic:
https://forum.lazarus.freepascal.org/index.php/topic,39821.msg274338.html#msg274338
Now, i need the same, but using SHA256, so this is what im doing: (using dcpcrypt2)
--- 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";}};} ---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:
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:
If you want to calculate hmac, you can use HashLib4Pascal
https://github.com/Xor-el/HashLib4Pascal
Gammatester:
Here is a complete test program for HMAC compiled with http://www.wolfgang-ehrhardt.de/crc_hash_2018-01-01.zip
--- 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 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: ---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
--- End code ---
and verified with your online-calculator.
torbente:
I used it and worked
--- 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";}};} ---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 [+][-]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";}};} ---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 pm ---If you want to calculate hmac, you can use HashLib4Pascal
https://github.com/Xor-el/HashLib4Pascal
--- End quote ---
@Gammatester i will try your code. Thanks!!
Navigation
[0] Message Index
[#] Next page