Completed
Push — master ( 69292d...688c68 )
by Igor
48s
created

crypto-js-bug-test-case.js   A

Complexity

Total Complexity 2
Complexity/F 1

Size

Lines of Code 32
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 0
c 1
b 0
f 1
nc 1
dl 0
loc 32
rs 10
wmc 2
mnd 0
bc 2
fnc 2
bpm 1
cpm 1
noi 3

2 Functions

Rating   Name   Duplication   Size   Complexity  
A ➔ correct 0 8 1
A ➔ incorrect 0 6 1
1
"use strict";
2
3
var crypto = require ('crypto')
4
var CryptoJS = require ('crypto-js')
5
6
let apiKey = 'CGAra4mIwKxgV3uYB4XZI4nVVmjXyIpER+5bFUuXGlIQ4MJFKLQfTJWK'
0 ignored issues
show
Unused Code introduced by
The variable apiKey seems to be never used. Consider removing it.
Loading history...
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
0 ignored issues
show
Bug introduced by
The variable Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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));
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
31
console.log ('bad:', incorrect (url, body, secret, nonce));
32
console.log ('(good == bad) ==', good == bad)