Add PEM cache to asymmetric keys
This commit is contained in:
parent
72a30966a1
commit
d5ffab1b42
|
@ -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;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
||||||
|
|
|
@ -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
|
||||||
|
|
Reference in New Issue