palmpaywrappersdk
Version:
PalmPay Wrapper Sdk
47 lines (46 loc) • 1.69 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.VerifyUtil = void 0;
const md5_1 = __importDefault(require("md5"));
const jsrsasign_1 = require("jsrsasign");
const PUB_PEM_BEGIN = '-----BEGIN PUBLIC KEY-----\n';
const PUB_PEM_END = '\n-----END PUBLIC KEY-----\n';
const HashMap = {
SHA256withRSA: 'SHA256withRSA',
SHA1withRSA: 'SHA1withRSA',
};
class VerifyUtil {
static rsaVerify(content, signature, publicKey, hash) {
const _publicKey = this._formatKey(publicKey);
const sig = new jsrsasign_1.KJUR.crypto.Signature({ alg: hash });
sig.init(_publicKey);
sig.updateString(content);
return sig.verify((0, jsrsasign_1.b64tohex)(signature));
}
static verify(params, signature, publicKey) {
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 isValid = this.rsaVerify(parseStr, signature, publicKey, HashMap.SHA1withRSA);
console.log('isValid', isValid);
return isValid;
}
static _formatKey(key) {
if (!key.startsWith(PUB_PEM_BEGIN)) {
key = PUB_PEM_BEGIN + key;
}
if (!key.endsWith(PUB_PEM_END)) {
key = key + PUB_PEM_END;
}
return key;
}
}
exports.VerifyUtil = VerifyUtil;