Delphi HMAC SHA512签署了对Bittrex Exchange的调用

时间:2014-10-09 13:43:04

标签: delphi indy hmac sha512

我一直在研究这个问题,但我无法从服务器获得成功的响应。 有关此内容的所有文档均可在Bittrex Exchange Wesite

中找到

签名位的主要症结可以在身份验证标题下找到

<小时/> 我一直在使用的散列文件可以在SourceForge上的Fundamentals找到。它是底部的一个名为Fundamentals Hash 4.00.15

我使用此文件的原因非常简单,它似乎是唯一一个给我正确答案的人。或者我应该说,与Hashing Website给我的结果相比,它给了我正确答案。

我尝试使用Indy组件生成正确的哈希值,但它似乎永远不会匹配网站的值。也许我没有正确使用它或正确的库或其他东西,但我会为我创建的那个添加示例。 (在我写这篇文章的时候,我刚刚再次测试过,看起来我得到了正确的答案,想想,也许我正在使用一个更好的OpenSSL库。无论如何,我还会将我的INDY示例放在下面以及)。

<小时/>

function Test: String;
const
  FAPIKey = 'APIKEY';
  FAPISecret = 'APISECRET';
  FURL = 'https://bittrex.com/api/v1.1/account/getbalances?apikey=%s&nonce=%d';
var
  FPost, FSignature: String;
  FNonce: Integer;
  Response: TStringStream;
  HTTP: TIdHTTP;
  SSL:TIdSSLIOHandlerSocketOpenSSL;
begin
  Result := '';

  FNonce := DateTimeToUnix(Now);
  FPost := Format(FURL, [FAPIKey, FNonce]);

  HTTP := TIdHTTP.Create;
  try
    SSL := TIdSSLIOHandlerSocketOpenSSL.Create(HTTP);
    try
      HTTP.IOHandler := SSL;

      FSignature := SHA512DigestToHex(CalcHMAC_SHA512(FAPISecret, FPost));
      HTTP.Request.CustomHeaders.AddValue('apisign', FSignature);

      Response := TStringStream.Create;
      try
        HTTP.Get(FPost, Response);
        Result := Response.DataString;
      finally
        Response := nil;
      end;
    finally
      SSL := nil;
    end;
  finally
    HTTP := nil;
  end;
end;

<小时/> 在使用此版本进行散列之前,我只是得到了 &#39; {&#34;成功&#34;:假,&#34;消息&#34;:&#34; APISIGN_NOT_PROVIDED&#34;&#34;导致&#34;:空}&#39; 当我解决自定义HTTP标头时,我终于继续前进了 &#39; {&#34;成功&#34;:假,&#34;消息&#34;:&#34; INVALID_SIGNATURE&#34;&#34;导致&#34;:空}&#39; 它可能是一个简单的无效的nonce,还是一个太旧的nonce? 一切都看起来不错,或者我错过了INDY组件的一些基本组件设置?

function Test: String;
const
  FAPIKey = 'APIKEY';
  FAPISecret = 'APISECRET';
  FURL = 'https://bittrex.com/api/v1.1/account/getbalances?apikey=%s&nonce=%d';
var
  FPost, FSignature: String;
  FNonce: Integer;
  Response: TStringStream;
  HTTP: TIdHTTP;
  SSL:TIdSSLIOHandlerSocketOpenSSL;
  FSHA512Hasher: TIdHMACSHA512;
begin
  Result := '';
  if not LoadOpenSSLLibrary then exit;

  FNonce := DateTimeToUnix(Now);
  FPost := Format(FURL, [FAPIKey, FNonce]);

  HTTP := TIdHTTP.Create;
  try
    SSL := TIdSSLIOHandlerSocketOpenSSL.Create(HTTP);
    try
      HTTP.IOHandler := SSL;

      FSHA512Hasher := TIdHMACSHA512.Create;
      try
        FSHA512Hasher.Key := ToBytes(FAPISecret);
        FSignature := Lowercase(ToHex(FSHA512Hasher.HashValue(ToBytes(FPost))));
      finally
        FSHA512Hasher := nil;
      end;

      HTTP.Request.CustomHeaders.AddValue('apisign', FSignature);

      Response := TStringStream.Create;
      try
        HTTP.Get(FPost, Response);
        Result := Response.DataString;
      finally
        Response := nil;
      end;
    finally
      SSL := nil;
    end;
  finally
    HTTP := nil;
  end;
end;

1 个答案:

答案 0 :(得分:0)

我有一个类似的问题,我最终想通了。我使用了我通过谷歌搜索找到的hmac.pas单元,但不得不修改它以使用10.1西雅图(特别是IDBytes有点不同)。

重要提示:我还必须修改hmac.pas单元以删除&#39; 0x&#39;从HexStr前面的功能变化。

另外:我做了所有输入ansistring。

我的测试功能如下。

function TBTXClient.Get(sCommand, sAddParams: string): string;
var
  sURL: string;
  nonce: string;
  res: string;
  sec2: string;
  sha: TIDHashSha256;
  hmac: TIdHMAC;
  hash: string;
  ms: TMemoryStream;
begin
  nonce := inttostr(getticker);
  sURL := 'https://bittrex.com/api/v1.1/'+sCommand+'?apikey='+MY_KEY+'&nonce='+nonce+sAddParams;

  hash := THMACUtils<TIDHMacSha512>.HMAC_HexStr(MY_SECRET,sURL);

  //Debug.Log('url = '+sUrl);
  //Debug.Log('hash = '+hash);
  QuickHTTPSGet(sURL, res, 'apisign', hash);

  result := res;

end;

我的hmac.pas单元的修改版本(有一些新的依赖项)

unit hmac;

interface

uses
  System.SysUtils,
  EncdDecd,
  IdHMAC,
  IdSSLOpenSSL,
  helpers.indy,
  idglobal,
  IdHash;

type
  localstringtype = ansistring;

  THMACUtils<T: TIdHMAC, constructor> = class
  public
    class function HMAC(aKey, aMessage: localstringtype): TIdBytes;
    class function HMAC_HexStr(aKey, aMessage: localstringtype): localstringtype;
    class function HMAC_Base64(aKey, aMessage: localstringtype): localstringtype;
  end;

implementation

class function THMACUtils<T>.HMAC(aKey, aMessage: localstringtype): TIdBytes;
var
  _HMAC: T;
begin
  if not IdSSLOpenSSL.LoadOpenSSLLibrary then Exit;
  _HMAC:= T.Create;
  try
    _HMAC.Key := AnsiStringToIDBytes(aKey);
    Result:= _HMAC.HashValue(AnsiStringToIDBytes(aMessage));
  finally
    _HMAC.Free;
  end;
end;

class function THMACUtils<T>.HMAC_HexStr(aKey, aMessage: localstringtype):     localstringtype;
var
  I: Byte;
begin
  Result:= '';//'0x';
  for I in HMAC(aKey, aMessage) do
    Result:= Result + IntToHex(I, 2);
end;

class function THMACUtils<T>.HMAC_Base64(aKey, aMessage: localstringtype):     localstringtype;
var
  _HMAC: TIdBytes;
begin
  _HMAC:= HMAC(aKey, aMessage);
  Result:= EncodeBase64(_HMAC, Length(_HMAC));
end;

end.

你可能也想要这个助手单位。

unit helpers.indy;

interface

uses
  idglobal, types, classes, sysutils, typex;


function TBytesToIDBytes(b: TBytes): TIDBytes;
function AnsiStringToIDBytes(a: ansistring): TIDBytes;

implementation


function TBytesToIDBytes(b: TBytes): TIDBytes;
var
  t: ni;
begin
  setlength(result, length(b));
  for t := low(b) to high(b) do
    result[t] := b[t];

end;
function AnsiStringToIDBytes(a: ansistring): TIDBytes;
var
  t: ni;
begin
  setlength(result, length(a));
  for t := 0  to length(a)-1 do
    result[t] := ord(a[STRZ+t]);

end;

这个俗气的功能,我从我自己的https单元借来的,显示了如何处理标题参数。

function QuickHTTPSGet(sURL: ansistring; out sOutREsponse: string; 
  addHead: string =''; addHeadValue: string = ''): boolean;
var
  htp: IXMLhttprequest;
begin

  htp := ComsXMLHTTP30.create();
  try
    htp.open('GET', sURL, false, null, null);
    if addHead <> '' then
      htp.setRequestHeader(addHead, addHeadValue);
    htp.send('');
    result := htp.status = 200;
    if result then
      sOutREsponse := htp.responsetext
    else
      soutResponse := 'error '+inttostr(htp.status);
  except
    on e: Exception do begin
      result := false;
      sOutResponse := 'error '+e.message;
    end;
  end;

end;