-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignature.js
42 lines (35 loc) · 933 Bytes
/
signature.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
require('./md5.js')
var md5 = faultylabs.MD5;
function parseUrl (url) {
url = url.trim();
var query = {};
if (url) {
url.split('&').forEach(function (line) {
var s = line.split('=');
if (s.length !== 2) {
throw new Error('URL格式不正确');
}
query[s[0]] = s[1];
});
}
return query;
}
function getSignature (query, token) {
var string = Object.keys(query).sort().map(function (k) {
return k + '=' + encodeURIComponent(query[k]);
}).join('&') + ':' + token;
console.log(string);
return md5(string);
}
function test() {
var app_id = "5vY4mg0Eog8SWo0mHYSWbqpl";
var token = "Ppuj8xfvb8jltBkcDvALFcEtWvgXGdxj";
var url = "a=2&b=3";
var urlParams = parseUrl(url);
urlParams["app_id"] = app_id;
urlParams["timestamp"] = "1442198292";
urlParams["noncestr"] = "TWSm66JpFIlzdRyk";
var sign = getSignature(urlParams, token);
console.log(sign)
}
test()