-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsignature.js
39 lines (39 loc) · 1.19 KB
/
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
const crypto = require('crypto');
/**
* 讲对象通过ASCII码排序
* @param {*} Object
* @returns {*} String
*/
function objKeySort(arys) {
//先用Object内置类的keys方法获取要排序对象的属性名数组,再利用Array的sort方法进行排序
var newkey = Object.keys(arys).sort();
var newObj = ''; //创建一个新的对象,用于存放排好序的键值对
for (var i = 0; i < newkey.length; i++) {
if (i === 0) {
newObj += `${[newkey[i]]}=${arys[newkey[i]]}`;
} else {
newObj += `&${[newkey[i]]}=${arys[newkey[i]]}`;
}
}
return newObj.substring(0, newObj.length);
}
/**
* 生成signature
* @param {*} Object
* @returns {*} String
*/
function createSignature(data) {
return crypto.createHash('SHA256').update(objKeySort(data)).digest('hex')
}
/**
* 生成16位签名随机字符串 nonceStr
* @param {*} $length
* @returns
*/
function createNonceStr(length = 16) {
let $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz', maxPos = $chars.length,pwd = '';
for (i = 0; i < length; i++) {
pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
}
return pwd;
}