1
|
|
|
"use strict"; |
2
|
|
|
|
3
|
|
|
var crypto = require ('crypto') |
4
|
|
|
var CryptoJS = require ('crypto-js') |
5
|
|
|
|
6
|
|
|
let apiKey = 'CGAra4mIwKxgV3uYB4XZI4nVVmjXyIpER+5bFUuXGlIQ4MJFKLQfTJWK' |
|
|
|
|
7
|
|
|
let secret = 'wrkstzsrwdj9pJlUGjj+wOTQgYAaO8MankKJPhASNGEY7hbFAtumQ45C7K/2SwCSgL8WcusUuTgarP5mmqo1uQ==' |
8
|
|
|
let nonce = '1501027285' |
9
|
|
|
let body = 'nonce=' + nonce |
10
|
|
|
let url = '/0/private/Balance' |
11
|
|
|
|
12
|
|
|
var correct = function (path, request, secret64, nonce) { |
13
|
|
|
const secret = new Buffer (secret64, 'base64'); // decode secret64 → secret |
|
|
|
|
14
|
|
|
const hash = new crypto.createHash ('sha256'); // create a hash from secret |
15
|
|
|
const hmac = new crypto.createHmac ('sha512', secret); // create a hmac signer with secret64 as private key |
16
|
|
|
const hash_digest = hash.update (nonce + request).digest ('binary'); // sha256 hash of nonce+request, results in binary hash |
17
|
|
|
const hmac_digest = hmac.update (path + hash_digest, 'binary').digest ('base64'); // hmac-sign path+hash (binary), encode output in base64 |
18
|
|
|
return hmac_digest; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
var incorrect = function (path, request, secret64, nonce) { |
22
|
|
|
let secret = CryptoJS.enc.Base64.parse (secret64); // decode secret64 → secret |
23
|
|
|
let hash_digest = CryptoJS.SHA256 (nonce + request).toString (CryptoJS.enc.Latin1); // sha256 hash of nonce+request, latin1 for binary |
24
|
|
|
let hmac_digest = CryptoJS.HmacSHA512 (path + hash_digest, secret).toString (CryptoJS.enc.Base64); // hmac-sign path+hash (binary), encode output in base64 |
25
|
|
|
return hmac_digest; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
let good = correct (url, body, secret, nonce); |
29
|
|
|
let bad = incorrect (url, body, secret, nonce); |
30
|
|
|
console.log ('good:', correct (url, body, secret, nonce)); |
|
|
|
|
31
|
|
|
console.log ('bad:', incorrect (url, body, secret, nonce)); |
32
|
|
|
console.log ('(good == bad) ==', good == bad) |