Add PEM cache to asymmetric keys

This commit is contained in:
microshine 2019-02-17 10:35:59 +03:00
parent 72a30966a1
commit d5ffab1b42
3 changed files with 17 additions and 4 deletions

View File

@ -3,5 +3,6 @@ import { CryptoKey } from "./key";
export abstract class AsymmetricKey extends CryptoKey { export abstract class AsymmetricKey extends CryptoKey {
public abstract type: "public" | "private"; public abstract type: "public" | "private";
public pem?: string;
} }

View File

@ -55,8 +55,11 @@ export class EcCrypto {
const signer = crypto.createSign(cryptoAlg); const signer = crypto.createSign(cryptoAlg);
signer.update(Buffer.from(data)); signer.update(Buffer.from(data));
if (!key.pem) {
key.pem = `-----BEGIN PRIVATE KEY-----\n${key.data.toString("base64")}\n-----END PRIVATE KEY-----`;
}
const options = { const options = {
key: `-----BEGIN PRIVATE KEY-----\n${key.data.toString("base64")}\n-----END PRIVATE KEY-----`, key: key.pem,
}; };
const signature = signer.sign(options); const signature = signer.sign(options);
@ -75,8 +78,11 @@ export class EcCrypto {
const signer = crypto.createVerify(cryptoAlg); const signer = crypto.createVerify(cryptoAlg);
signer.update(Buffer.from(data)); signer.update(Buffer.from(data));
if (!key.pem) {
key.pem = `-----BEGIN PUBLIC KEY-----\n${key.data.toString("base64")}\n-----END PUBLIC KEY-----`;
}
const options = { const options = {
key: `-----BEGIN PUBLIC KEY-----\n${key.data.toString("base64")}\n-----END PUBLIC KEY-----`, key: key.pem,
}; };
const ecSignature = new asn.EcDsaSignature(); const ecSignature = new asn.EcDsaSignature();

View File

@ -192,8 +192,11 @@ export class RsaCrypto {
const signer = crypto.createSign(cryptoAlg); const signer = crypto.createSign(cryptoAlg);
signer.update(Buffer.from(data)); signer.update(Buffer.from(data));
if (!key.pem) {
key.pem = `-----BEGIN PRIVATE KEY-----\n${key.data.toString("base64")}\n-----END PRIVATE KEY-----`;
}
const options: INodeCryptoSignOptions = { const options: INodeCryptoSignOptions = {
key: `-----BEGIN PRIVATE KEY-----\n${key.data.toString("base64")}\n-----END PRIVATE KEY-----`, key: key.pem,
}; };
if (algorithm.name.toUpperCase() === "RSA-PSS") { if (algorithm.name.toUpperCase() === "RSA-PSS") {
// @ts-ignore // @ts-ignore
@ -210,8 +213,11 @@ export class RsaCrypto {
const signer = crypto.createVerify(cryptoAlg); const signer = crypto.createVerify(cryptoAlg);
signer.update(Buffer.from(data)); signer.update(Buffer.from(data));
if (!key.pem) {
key.pem = `-----BEGIN PUBLIC KEY-----\n${key.data.toString("base64")}\n-----END PUBLIC KEY-----`;
}
const options: INodeCryptoSignOptions = { const options: INodeCryptoSignOptions = {
key: `-----BEGIN PUBLIC KEY-----\n${key.data.toString("base64")}\n-----END PUBLIC KEY-----`, key: key.pem,
}; };
if (algorithm.name.toUpperCase() === "RSA-PSS") { if (algorithm.name.toUpperCase() === "RSA-PSS") {
// @ts-ignore // @ts-ignore