palmpaywrappersdk
Version:
PalmPay Wrapper Sdk
54 lines (53 loc) • 1.86 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.sign = void 0;
const md5_1 = __importDefault(require("md5"));
const jsrsasign_1 = require("jsrsasign");
// Define the HashMap type and values
const HashMap = {
SHA256withRSA: 'SHA256withRSA',
SHA1withRSA: 'SHA1withRSA',
};
const PEM_BEGIN = '-----BEGIN PRIVATE KEY-----\n';
const PEM_END = '\n-----END PRIVATE KEY-----';
const sign = (params, privateKey) => {
let parseStr = Object.keys(params)
.sort()
.filter((item) => params[item])
.map((item) => `${item}=${params[item]}`)
.join('&');
console.log('parseStr', parseStr);
parseStr = (0, md5_1.default)(parseStr).toUpperCase();
console.log('MD5', parseStr);
const str = SignUtil.rsaSign(parseStr, privateKey, HashMap.SHA1withRSA);
console.log('sign', str);
return str;
};
exports.sign = sign;
const SignUtil = {
rsaSign: function (content, privateKey, hash) {
const _privateKey = this._formatKey(privateKey);
console.log('parseStr', _privateKey);
// Type assertion to handle the private key type issue
const signature = new jsrsasign_1.KJUR.crypto.Signature({
alg: hash,
prvkeypem: _privateKey,
}); // <-- Type assertion to bypass type checking
signature.updateString(content);
const signData = signature.sign();
return (0, jsrsasign_1.hextob64)(signData);
},
_formatKey: function (key) {
if (!key.startsWith(PEM_BEGIN)) {
key = PEM_BEGIN + key;
}
if (!key.endsWith(PEM_END)) {
key = key + PEM_END;
}
return key;
},
};
exports.default = SignUtil;