-
Notifications
You must be signed in to change notification settings - Fork 1
/
hash.js
42 lines (33 loc) · 930 Bytes
/
hash.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
var crypto = require('crypto');
/////////////////////////////////////
// Sign
/////////////////////////////////////
exports.sign = function (obj, pass) {
// uppercase object
var uObj = {};
if (!obj || !pass) {
return null;
}
// keys to uppercase
for (var key in obj) {
var value = obj[key];
if (["string", "number", "boolean"].indexOf(typeof value) === -1) {
continue;
}
uObj[key.toUpperCase()] = value;
}
// sort array of keys alphabetically
var keys = Object.keys(uObj);
keys.sort();
// create the text
var text = "";
for (var i in keys) {
var value = uObj[keys[i]];
if (value) {
text += keys[i] + "=" + value + pass;
}
}
// create the hash to upper case
uObj.SHASIGN = crypto.createHash("sha1").update(text, "utf8").digest("hex").toUpperCase();
return uObj;
};