plastkort 0 Posted July 26, 2019 hi! I have been playing with the tought on playing with websockets and rest API. i got the documentation on how to connect but encrypion stuff is hard to swallow, is there any tricks on how to solve this little easter egg ? var param_str : string; secret : string; begin Secret := 't7T0YlFnYXk0Fx3JswQsDrViLg1Gh3DUU5Mr'; param_str := 'api_key=B2Rou0PLPpGqcU0Vu2&leverage=100&symbol=BTCUSD×tamp=1542434791000'; sign := hex(HMAC_SHA256($secret, $param_str)); end; result should be sign = 670e3e4aa32b243f2dedf1dafcec2fd17a440e71b05681550416507de591d908 any solution is OK, but cannot affford anything that costs... help 🤪🤪 Share this post Link to post
stijnsanders 35 Posted July 26, 2019 I copied the reference code for sha256 and hmac to Delphi once, have a look if you can make these work for you: http://yoy.be/md5.html Share this post Link to post
plastkort 0 Posted July 26, 2019 13 minutes ago, stijnsanders said: I copied the reference code for sha256 and hmac to Delphi once, have a look if you can make these work for you: http://yoy.be/md5.html thanks, I will take a look at this, as an alternative i have been looking into python, and i see there are a component for integrating python scripts.. that opens many possibilities as well Share this post Link to post
Ugochukwu Mmaduekwe 42 Posted July 26, 2019 4 hours ago, plastkort said: hi! I have been playing with the tought on playing with websockets and rest API. i got the documentation on how to connect but encrypion stuff is hard to swallow, is there any tricks on how to solve this little easter egg ? var param_str : string; secret : string; begin Secret := 't7T0YlFnYXk0Fx3JswQsDrViLg1Gh3DUU5Mr'; param_str := 'api_key=B2Rou0PLPpGqcU0Vu2&leverage=100&symbol=BTCUSD×tamp=1542434791000'; sign := hex(HMAC_SHA256($secret, $param_str)); end; result should be sign = 670e3e4aa32b243f2dedf1dafcec2fd17a440e71b05681550416507de591d908 any solution is OK, but cannot affford anything that costs... help 🤪🤪 if you use Berlin upwards, you can use the inbuilt version found here else you can try the code below. you will need this library though program SimpleHMAC; uses SysUtils, HlpHashFactory; var param_str, secret, sign: string; begin try secret := 't7T0YlFnYXk0Fx3JswQsDrViLg1Gh3DUU5Mr'; param_str := 'api_key=B2Rou0PLPpGqcU0Vu2&leverage=100&symbol=BTCUSD×tamp=1542434791000'; sign := THashFactory.THMAC.CreateHMAC(THashFactory.TCrypto.CreateSHA2_256(), TEncoding.UTF8.GetBytes(secret)).ComputeString(param_str, TEncoding.UTF8).ToString(); WriteLn(LowerCase(sign)); ReadLn; except raise; end; end. Share this post Link to post
Arnaud Bouchez 407 Posted July 27, 2019 Take a look at https://github.com/synopse/mORMot/blob/master/SynCrypto.pas It is Open Source, works from Delphi 6 up to latest Delphi 10.3, and is the fastest library for Delphi AFAIK - for instance, there is an Intel's SSE4 x64 optimized asm for SHA-256 on x86_64 (Windows and FPC Linux). You can use the THMAC_SHA256 record, or HMAC_SHA256() function for your purpose. 2 Share this post Link to post
Angus Robertson 574 Posted July 27, 2019 This is avaiIable in the OverbyteIcsSslJose unit: { digests and hashes - note all digests are binary bytes in AnsiStrings } function IcsHMACDigest(const Data, Key: AnsiString; HashDigest: TEvpDigest = Digest_sha256): AnsiString; function IcsHMACDigestEx(const Data, Key: AnsiString; HashDigest: TEvpDigest = Digest_sha256): AnsiString; function IcsHMACDigestVerify(const Data, Key, OldDigest: AnsiString; HashDigest: TEvpDigest = Digest_sha256): Boolean; With various other functions used for Json Object Signing and Encryption, Angus 1 Share this post Link to post
plastkort 0 Posted July 31, 2019 thanks everyone i will take a look later tonight. 🙂 Share this post Link to post
plastkort 0 Posted July 31, 2019 On 7/27/2019 at 10:34 AM, Angus Robertson said: This is avaiIable in the OverbyteIcsSslJose unit: { digests and hashes - note all digests are binary bytes in AnsiStrings } function IcsHMACDigest(const Data, Key: AnsiString; HashDigest: TEvpDigest = Digest_sha256): AnsiString; function IcsHMACDigestEx(const Data, Key: AnsiString; HashDigest: TEvpDigest = Digest_sha256): AnsiString; function IcsHMACDigestVerify(const Data, Key, OldDigest: AnsiString; HashDigest: TEvpDigest = Digest_sha256): Boolean; With various other functions used for Json Object Signing and Encryption, Angus do you have any example on this one? I got error could not find libeau.. but i can use tsslwsocket, maybe something missing Share this post Link to post
Angus Robertson 574 Posted July 31, 2019 There is an example in the same unit, IcsJoseGetSig. There is also a sample project OverbyteIcsJoseTst.dpr that is used to test the various Jose functions, including hex, base64 and base64url encoding and decoding. But this unit needs the OpenSSL DLLs to be distributed with your application, as does any use of SSL or encryption in ICS. And you need to initialise OpenSSL before calling those functions, see the sample application. As others mentioned, there are Delphi only implementations of SHA256, but IcsHMACDigest more modern digests as well. and most REST applications need HTTPS. 1 Share this post Link to post
plastkort 0 Posted July 31, 2019 52 minutes ago, Angus Robertson said: There is an example in the same unit, IcsJoseGetSig. There is also a sample project OverbyteIcsJoseTst.dpr that is used to test the various Jose functions, including hex, base64 and base64url encoding and decoding. But this unit needs the OpenSSL DLLs to be distributed with your application, as does any use of SSL or encryption in ICS. And you need to initialise OpenSSL before calling those functions, see the sample application. As others mentioned, there are Delphi only implementations of SHA256, but IcsHMACDigest more modern digests as well. and most REST applications need HTTPS. I have delphi 10.2 maybe it does not have this library Share this post Link to post
skande 0 Posted March 14, 2022 (edited) On 7/27/2019 at 9:36 AM, Arnaud Bouchez said: Take a look at https://github.com/synopse/mORMot/blob/master/SynCrypto.pas It is Open Source, works from Delphi 6 up to latest Delphi 10.3, and is the fastest library for Delphi AFAIK - for instance, there is an Intel's SSE4 x64 optimized asm for SHA-256 on x86_64 (Windows and FPC Linux). You can use the THMAC_SHA256 record, or HMAC_SHA256() function for your purpose. THANK YOU !!! I have try this , and it works ( the necesary units ( by dependecy you must be put in the same directory ) are .... SynCrypto.pas, SynCommons.pas, SynTable.pas , SynLZ.pas , Synopse.inc , SynopseCommit.inc ( all these files are in this link Github ) OR if you prefer ... are attched to this post Uses system, SynCrypto function CalculateHMACSHA256(const value, sKey: String): String; var sha256Digest: TSHA256Digest; begin try HMAC_SHA256(UTF8Encode(skey),UTF8Encode(value),sha256Digest); Result:=UpperCase(SHA256DigestToString(sha256Digest)); except Result:=''; end; end; Synopse.inc SynopseCommit.inc SynCommons.pas SynCrypto.pas SynLZ.pas SynTable.pas sha512-x64sse4.obj sha512-x86.obj Edited March 14, 2022 by skande Share this post Link to post