Jump to content

Recommended Posts

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&timestamp=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
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
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&timestamp=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&timestamp=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

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.

  • Like 2

Share this post


Link to post

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

 

  • Like 1

Share this post


Link to post
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

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.

 

  • Thanks 1

Share this post


Link to post
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
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 by skande

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×