-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (32 loc) · 923 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"use strict";
var forge = require("node-forge");
function trimZeroes(data) {
return data.toString().replace(/^\0+/, "").replace(/\0+$/, "");
}
function padKey(key) {
while (key.length % 16 !== 0) {
key = key + "\0";
}
return key;
}
function decryptHash(tangoHash, tangoSharedSecret, cb) {
var err = null;
var res;
try {
var IV_SIZE = 16;
var decodedHash = new Buffer(tangoHash, "base64");
var iv = forge.util.createBuffer(decodedHash.slice(0, IV_SIZE).toString("binary"));
var data = forge.util.createBuffer(decodedHash.slice(IV_SIZE).toString("binary"));
var decipher = forge.cipher.createDecipher("AES-CBC", padKey(tangoSharedSecret));
decipher.start({iv: iv});
decipher.update(data);
decipher.finish();
res = JSON.parse(trimZeroes(decipher.output));
} catch (e) {
err = e;
}
process.nextTick(function () {
cb(err, res);
});
}
module.exports.decryptHash = decryptHash;