优化sdk-nodejs以下功能:

1、新增同步请求方式(executeSync、executeTokenSync);
2、优化请求异常中断问题,返回502错误码;
3、优化签名方法;
This commit is contained in:
Changeden
2021-04-12 15:42:52 +08:00
parent 472942e93e
commit e1b860c75a
8 changed files with 146 additions and 147 deletions

View File

@@ -1,12 +1,12 @@
const {KJUR, hextob64} = require('jsrsasign')
const {KJUR, hextob64} = require('jsrsasign');
const HashMap = {
SHA256withRSA: 'SHA256withRSA',
SHA1withRSA: 'SHA1withRSA'
}
};
const PEM_BEGIN = '-----BEGIN PRIVATE KEY-----\n'
const PEM_END = '\n-----END PRIVATE KEY-----'
const PEM_BEGIN = '-----BEGIN PRIVATE KEY-----\n';
const PEM_END = '\n-----END PRIVATE KEY-----';
/**
* rsa签名参考https://www.jianshu.com/p/145eab95322c
@@ -20,16 +20,16 @@ exports.SignUtil = {
* @returns 返回签名内容
*/
createSign(params, privateKey, signType) {
const content = this.getSignContent(params)
return this.sign(content, privateKey, signType)
const content = this.getSignContent(params);
return this.sign(content, privateKey, signType);
},
sign: function (content, privateKey, signType) {
if (signType.toUpperCase() === 'RSA') {
return this.rsaSign(content, privateKey, HashMap.SHA1withRSA)
return this.rsaSign(content, privateKey, HashMap.SHA1withRSA);
} else if (signType.toUpperCase() === 'RSA2') {
return this.rsaSign(content, privateKey, HashMap.SHA256withRSA)
return this.rsaSign(content, privateKey, HashMap.SHA256withRSA);
} else {
throw 'signType错误'
throw 'signType错误';
}
},
/**
@@ -40,26 +40,26 @@ exports.SignUtil = {
* @returns 返回签名字符串base64
*/
rsaSign: function (content, privateKey, hash) {
privateKey = this._formatKey(privateKey)
privateKey = this._formatKey(privateKey);
// 创建 Signature 对象
const signature = new KJUR.crypto.Signature({
alg: hash,
//!这里指定 私钥 pem!
prvkeypem: privateKey
})
signature.updateString(content)
const signData = signature.sign()
});
signature.updateString(content);
const signData = signature.sign();
// 将内容转成base64
return hextob64(signData)
return hextob64(signData);
},
_formatKey: function (key) {
if (!key.startsWith(PEM_BEGIN)) {
key = PEM_BEGIN + key
key = PEM_BEGIN + key;
}
if (!key.endsWith(PEM_END)) {
key = key + PEM_END
key = key + PEM_END;
}
return key
return key;
},
/**
* 获取签名内容
@@ -67,23 +67,23 @@ exports.SignUtil = {
* @returns {string}
*/
getSignContent: function (params) {
const paramNames = []
const paramNames = [];
// 获取对象中的Key
paramNames.push(...Object.keys(params || {})
// 过滤无效的KeyValue
.filter(paramName => {
// 参数名不为undefined且参数值不为undefined
return !(typeof paramName === undefined || typeof params[paramName] === undefined)
}))
// 过滤无效的KeyValue
.filter(paramName => {
// 参数名不为undefined且参数值不为undefined
return !(typeof paramName === undefined || typeof params[paramName] === undefined);
}));
paramNames.sort()
paramNames.sort();
// 合成签名字符串
const paramNameValue = paramNames.map(paramName => {
const val = params[paramName];
return `${paramName}=${val}`
})
return `${paramName}=${val}`;
});
return paramNameValue.join('&')
return paramNameValue.join('&');
}
}
};