【问题标题】:Can't get HMAC Authentication working with API无法使用 API 获得 HMAC 身份验证
【发布时间】:2015-08-18 19:16:54
【问题描述】:

我正在尝试使用带有 LocalBitcoins API 的 HMAC 进行身份验证。

这是用 Python 编写的认证:

  message = str(nonce) + hmac_auth_key + relative_path + get_or_post_params_urlencoded
  signature = hmac.new(hmac_auth_secret, msg=message, digestmod=hashlib.sha256).hexdigest().upper()

以及创建HMAC消息的参数:

Nonce. A 63 bit positive integer, for example unix timestamp as milliseconds.

HMAC authentication key. This is the first one of a key/secret pair.

Relative path, for example /api/wallet/.

GET or POST parameters in their URL encoded format, for example foo=bar&baz=quux.

这是我构建 HMAC 的方式:

var milliseconds = (new Date).getTime();
var key = config.key;
var secret = config.secret;
var nonce = milliseconds.toString()


var message = nonce + key + 'api/myself';

var hmac_digest = crypto.createHmac("sha256", secret).update(message).digest('hex').toUpperCase();

签名通过 3 个 HTTP 标头发送。调用 api/myself 方法的选项如下所示(使用请求):

{ url: 'https://localbitcoins.com/api/myself',
  method: 'GET',
  headers:
   { 'Apiauth-Key': 'my api key',
     'Apiauth-Nonce': 1439925212276,
     'Apiauth-Signature': 'the created signature' },
  timeout: 5000 }

请求:

var req = request.get(options, function(error, response, body) {
    console.log(body);
});

但每次我收到以下错误消息:

{ error:
   { message: 'HMAC authentication key and signature was given, but they are invalid.',
     error_code: 41 } }

我在测试中尝试了很多不同的组合,但没有任何效果。我错过了什么?

【问题讨论】:

  • 我不确定这是否可行,您是否尝试过使用 OAuth 进行身份验证?
  • 我没看过。在这一点上仍然超级喜欢 HMAC。

标签: node.js hmac


【解决方案1】:

原来我的路错了。

/path 必须是 /path/,这是我通过使用有效的 Python 实现发现的。

该软件包现已在此处启动并运行:https://github.com/mrmayfield/localbitcoins-node

【讨论】:

    【解决方案2】:

    我认为(new Date).getTime(); 没有创建一个 63 位整数。根据 Axel 博士的post。 JavaScript 有 53 位整数加符号。

    【讨论】:

    • 如果我使用 (new Date).getTime()*1000 我会得到同样的错误。也不知道如何检查整数的位值。
    • 我不认为你可以将 js 整数设为 63 位。 max int mozilla
    • 那么就没有办法发送63位整数了吗?我正在考虑使用这个库:github.com/broofa/node-int64。这将允许我发送一个 63 位整数,还是在我尝试将变量作为原始标头发送时它会爆炸?
    • 老实说,我不知道,但是当您尝试获取该值时,它看起来会爆炸,因为它会注册为无穷大。似乎只有八位字节表示是非易失性的。
    • 问题是整数不需要完全精确。我不能通过添加数字来欺骗 63 位整数,而不管发送到服务器的正是它吗?
    猜你喜欢
    • 1970-01-01
    • 2018-05-28
    • 2012-10-24
    • 2016-05-21
    • 1970-01-01
    • 2019-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多