我正在尝试学习Angular2,作为第一个练习,我想从Kraken.com应用程序接口中检索数据。(我知道,我本可以选择更简单的:)
嗯,对于“公共”调用,它工作得很好。现在我正在尝试调用"Private“方法。(那些需要认证的)
如下所示:https://www.kraken.com/help/api
期望的签名是:
API-Sign = Message signature using HMAC-SHA512 of (URI path + SHA256(nonce + POST data)) and base64 decoded secret API key所以我正试着生成那个...没有成功..。我经常收到这样的错误消息:
Kraken API returned an error: API:Invalid nonce但我很确定我的nonce是正确的,所以我宁愿认为它与数据加密有关。
我发现here是一个可以完成这项工作的函数,但我不能在我的angular项目中“按原样”使用它。
/**
* This method returns a signature for a request as a Base64-encoded string
* @param {String} path The relative URL path for the request
* @param {Object} request The POST body
* @param {Integer} nonce A unique, incrementing integer
* @return {String} The request signature
*/
function getMessageSignature(path, request, nonce) {
var message = querystring.stringify(request);
var secret = new Buffer(config.secret, 'base64');
var hash = new crypto.createHash('sha256');
var hmac = new crypto.createHmac('sha512', secret);
var hash_digest = hash.update(nonce + message).digest('binary');
var hmac_digest = hmac.update(path + hash_digest, 'binary').digest('base64');
return hmac_digest;
}下面是我的代码:
private getMessageSignature(path: string, request: any, nonce: number) {
//API-Sign = Message signature using HMAC-SHA512 of (URI path + SHA256(nonce + POST data)) and base64 decoded secret API key
var message = this.querystring.stringify(request);
var secret = this.CryptoJS.enc.Base64.parse(this.config.secret);
var hash_digest = this.CryptoJS.SHA256(nonce + message);
var hmac = this.CryptoJS.HmacSHA512(path + hash_digest, secret).toString(this.CryptoJS.enc.Base64);
return hmac;
}发布于 2017-03-15 03:59:27
问题很可能是path + hash_digest,这是一个类型不匹配,导致奇怪的结果,因为hash_digest将是十六进制编码的,并且不会在它的二进制表示中使用。
以下方法可能会起作用:
var hmac = this.CryptoJS.HmacSHA512(path + hash_digest.toString(this.CryptoJS.enc.Latin1), secret).toString(this.CryptoJS.enc.Base64);但是下面是一个更好的选择:
var hmacHasher = this.CryptoJS.algo.HMAC.create(this.CryptoJS.algo.SHA512, secret);
hmacHasher.update(path);
hmacHasher.update(hash_digest);
var hmac = hmacHasher.finalize().toString(this.CryptoJS.enc.Base64);发布于 2017-03-15 08:20:58
哇塞!多亏了Artjom的帮助,我终于成功地进行了API调用!
在实际显示代码之前,我必须更改一些内容:在我的查询中,我将参数作为
application/x-www-form-urlencoded在实际发送的数据和用于计算签名的数据之间存在不匹配。所以我必须纠正这一点,以便在我的请求体中发送的数据与签名计算中使用的数据完全相同:
let paramsStr = this.querystring.stringify(params);好的,最后,下面是计算签名的代码:
private getMessageSignature(path: string, request: any, nonce: number) {
//API-Sign = Message signature using HMAC-SHA512 of
// (URI path + SHA256(nonce + POST data)) and base64 decoded secret API key
var message = this.querystring.stringify(request);
var secret = this.CryptoJS.enc.Base64.parse(this.config.secret);
var hash256 = this.CryptoJS.algo.SHA256.create();
hash256.update(String(nonce));
hash256.update(message);
var hmacHasher = this.CryptoJS.algo.HMAC.create(this.CryptoJS.algo.SHA512, secret);
hmacHasher.update(path);
hmacHasher.update(hash256.finalize());
var hashInBase64 = this.CryptoJS.enc.Base64.stringify(hmacHasher.finalize());
return hashInBase64;
}https://stackoverflow.com/questions/42784299
复制相似问题