1
我想從我的Agnular4項目調用Kraken API。我正在使用crypto-js
庫https://www.npmjs.com/package/crypto-js。Angular4/Typescript標誌Kraken API調用(CryptoJS)
海妖需要此簽名:
API-Sign = Message signature using HMAC-SHA512 of (URI path + SHA256(nonce + POST data)) and base64 decoded secret API key
Correct result:
gy+Ljq8nPD2kFFCQSofQ46pbPV8z8D7klrU78Kxvl6jyior/Ev2FiclhKs+zoqrmL8u8u1Y9CXzIw0n3CswtNA==
Result from:
CryptoJS.HmacSHA512(path + CryptoJS.enc.Hex.stringify(hashDigest), secret);
DWtQpzZJkcHw7Ai1aFUNAR8ne0h3P/GaHc1OIUGsITifOOE+IAWF9f5HxspSY5wF6qcQwCKx9Bz1MDwFhu01QA==
我以前做這些調用了Android應用的,它的工作(見代碼,並在文章底部的結果)。
我也嘗試了很多東西好幾天。我可能已經做了一個愚蠢的錯誤:d
預先感謝閱讀,希望回答這個:)
這裏是我的Angular4客戶端代碼:
import * as CryptoJS from 'crypto-js';
...
this.API_SECRET = "0a...g==";
...
private getMessageSignature(path: string, request: string, nonce: number) {
console.log('getMessageSignature', path, request, nonce);
console.log('request', request);
const message = nonce + request;
var secret = CryptoJS.enc.Base64.parse(this.API_SECRET);
var hashDigest = CryptoJS.SHA256(message);
var hashDigestBase64 = CryptoJS.enc.Base64.stringify(hashDigest);
console.log('hashDigest', CryptoJS.enc.Base64.stringify(hashDigest)); // correct
console.log('hashDigest', CryptoJS.enc.Hex.stringify(hashDigest)); // correct
var hmacDigest = CryptoJS.HmacSHA512(path + hashDigest, secret);
console.log('hmacDigest', CryptoJS.enc.Base64.stringify(hmacDigest));
var hmacDigest1 = CryptoJS.HmacSHA512(path + CryptoJS.enc.Base64.stringify(hashDigest), secret);
console.log('hmacDigest1', CryptoJS.enc.Base64.stringify(hmacDigest1));
var hmacDigest2 = CryptoJS.HmacSHA512(path + CryptoJS.enc.Hex.stringify(hashDigest), secret);
console.log('hmacDigest2', CryptoJS.enc.Base64.stringify(hmacDigest2));
var hmacDigest3 = CryptoJS.HmacSHA512(path + CryptoJS.enc.Latin1.stringify(hashDigest), secret);
console.log('hmacDigest3', CryptoJS.enc.Base64.stringify(hmacDigest3));
var hmacDigest4 = CryptoJS.HmacSHA512(path + hashDigest, this.API_SECRET);
console.log('hmacDigest4', CryptoJS.enc.Base64.stringify(hmacDigest4));
var hmacDigest5 = CryptoJS.HmacSHA512(path + CryptoJS.enc.Base64.stringify(hashDigest), this.API_SECRET);
console.log('hmacDigest5', CryptoJS.enc.Base64.stringify(hmacDigest5));
var hmacDigest6 = CryptoJS.HmacSHA512(path + CryptoJS.enc.Hex.stringify(hashDigest), this.API_SECRET);
console.log('hmacDigest6', CryptoJS.enc.Base64.stringify(hmacDigest6));
var hmacDigest7 = CryptoJS.HmacSHA512(path + CryptoJS.enc.Latin1.stringify(hashDigest), this.API_SECRET);
console.log('hmacDigest7', CryptoJS.enc.Base64.stringify(hmacDigest7));
return CryptoJS.enc.Base64.stringify(hmacDigest);
}
Angular4結果:
getMessageSignature /0/private/Balance nonce=1503065538999 1503065538999
request nonce=1503065538999
hashDigest EcvBaI+IvdvibXZ4UiNlcuAvT8fPPdAItwhBrCNx7q8=
hashDigest 11cbc1688f88bddbe26d767852236572e02f4fc7cf3dd008b70841ac2371eeaf
hmacDigest DWtQpzZJkcHw7Ai1aFUNAR8ne0h3P/GaHc1OIUGsITifOOE+IAWF9f5HxspSY5wF6qcQwCKx9Bz1MDwFhu01QA==
hmacDigest1 3f+oCR9hagmWESRcaZhfU6gHlMNFnnRCP25Yslc1nitfFvkq+/SikcByeWNQFWICkTLUOxMxQr4LBw2mcGwC9g==
hmacDigest2 DWtQpzZJkcHw7Ai1aFUNAR8ne0h3P/GaHc1OIUGsITifOOE+IAWF9f5HxspSY5wF6qcQwCKx9Bz1MDwFhu01QA==
hmacDigest3 87Zs0C/7b1fPuINnkB8WHQ1LhUG9u3hMMIt1hGOKszWZ4yHfohdlLLe2eUdpZr+c2B24ecYaqc/r6WRcDp5sdg==
hmacDigest4 MySDdpd+ufXJLp6BhP2oGpvnCrs1FUlIFp5+4P6oW0Zf3H9CfuQ3Z6BioUr9l2O3Y1VXfo9qhHNtRO/F8NS5Ag==
hmacDigest5 ndMT5/pjEgYI1qd7cpaloiJJ6Q6ktBl2R9Kgi4kL3UwSiSix/jLi9sFQdZTNqmqJIt70GFNqbDUyQII+FOj/tw==
hmacDigest6 MySDdpd+ufXJLp6BhP2oGpvnCrs1FUlIFp5+4P6oW0Zf3H9CfuQ3Z6BioUr9l2O3Y1VXfo9qhHNtRO/F8NS5Ag==
hmacDigest7 iOU29TPq7vm2gQ6totB9UbVrQGUBNbxK5G7nbqMXWJFjdO2BTTpQD59bCPjkijPz3hgQkwdHpdHimJfCIXYYAw==
Android代碼:
private static String calculateSignature(String nonce, String data, String path) {
System.out.println("calculateSignature " + nonce + " " + data + " " + path);
String signature = "";
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update((nonce + data).getBytes());
Mac mac = Mac.getInstance("HmacSHA512");
mac.init(new SecretKeySpec(Base64.decode(API_SECRET.getBytes(), Base64.NO_WRAP), "HmacSHA512"));
mac.update(path.getBytes());
byte[] digest = md.digest();
System.out.println("digest = " + Base64.encodeToString(digest, Base64.NO_WRAP));
signature = Base64.encodeToString(mac.doFinal(digest), Base64.NO_WRAP);
System.out.println("signature = " + signature);
} catch(Exception e) {}
return signature;
}
Android的結果:
calculateSignature 1503065538999 nonce=1503065538999 /0/private/Balance
digest = EcvBaI+IvdvibXZ4UiNlcuAvT8fPPdAItwhBrCNx7q8=
signature = gy+Ljq8nPD2kFFCQSofQ46pbPV8z8D7klrU78Kxvl6jyior/Ev2FiclhKs+zoqrmL8u8u1Y9CXzIw0n3CswtNA==