Skip to content

Commit

Permalink
fix: 微信支付的敏感信息加密机增加获取公钥序列号的方法 (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpyer committed Dec 20, 2023
1 parent d276032 commit fb9a3f3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
31 changes: 31 additions & 0 deletions src/Pay/Encryptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';
import { PrivateKey } from "../Core/Support/PrivateKey";
import { PublicKey } from "../Core/Support/PublicKey";
import RSA from "../Core/Support/RSA";

class Encryptor extends RSA {
protected publicCert: PublicKey;
protected privateCert: PrivateKey;

/**
* 设置加密机所需的公私密钥
* @param publicCert PublicKey封装的公钥
* @param privateCert PrivateKey封装的私钥
*/
setCerts(publicCert: PublicKey, privateCert: PrivateKey) {
this.publicCert = publicCert;
this.privateCert = privateCert;
this.setPublicKey(publicCert.toString());
this.setPrivateKey(privateCert.toString());
}

/**
* 获取公钥的序列号
*/
getSerialNo(): string {
return this.publicCert.getSerialNo();
}

}

export = Encryptor;
8 changes: 4 additions & 4 deletions src/Pay/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import RSA from '../Core/Support/RSA';
import { createHash, createHmac, getTimestamp, randomString } from '../Core/Support/Utils';
import { PayAppConfig, PayBridgeConfig, PaySdkConfig } from '../Types/global';
import MerchantInterface from './Contracts/MerchantInterface';
import Encryptor from './Encryptor';

class Utils
{
Expand All @@ -30,13 +31,12 @@ class Utils
* @returns
*/
async getEncryptor(platformCert: PublicKey = null) {
let rsa = new RSA;
if (!platformCert || !(platformCert instanceof PublicKey)) {
platformCert = await this.getPlatformCert();
}
rsa.setPublicKey(platformCert.toString());
rsa.setPrivateKey(this.merchant.getPrivateKey().toString());
return rsa;
let encryptor = new Encryptor;
encryptor.setCerts(platformCert, this.merchant.getPrivateKey());
return encryptor;
}

/**
Expand Down
5 changes: 2 additions & 3 deletions test/Pay/Utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ class TestUnit extends BaseTestUnit {

it('Should encrypt and decrypt correctly', async () => {
let data = 'Abc123';
let cert = await utils.getPlatformCert();
let encryptor = await utils.getEncryptor(cert);
let encryptor = await utils.getEncryptor();
let ciphertext = encryptor.encrypt(data);
this.assert.strictEqual(cert.getSerialNo(), '0DC0DF83');
this.assert.strictEqual(encryptor.getSerialNo(), '0DC0DF83');

let plaintext = encryptor.decrypt(ciphertext);
this.assert.strictEqual(plaintext, data);
Expand Down

0 comments on commit fb9a3f3

Please sign in to comment.