Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it("serialize", () => {
const key = JsonParser.fromJSON(json, { targetSchema: asn.RsaPublicKey });
const keyInfo = new asn.PublicKeyInfo();
keyInfo.publicKeyAlgorithm.algorithm = "1.2.840.113549.1.1.1";
keyInfo.publicKeyAlgorithm.parameters = null;
keyInfo.publicKey = AsnSerializer.serialize(key);
const asnKeyInfo = Buffer.from(AsnSerializer.serialize(keyInfo));
assert.equal(asnKeyInfo.equals(bytes), true);
});
it("serialize", () => {
const key = JsonParser.fromJSON(json, { targetSchema: asn.RsaPrivateKey });
const keyInfo = new asn.PrivateKeyInfo();
keyInfo.privateKeyAlgorithm.algorithm = "1.2.840.113549.1.1.1";
keyInfo.privateKeyAlgorithm.parameters = null;
keyInfo.privateKey = AsnSerializer.serialize(key);
const asnKeyInfo = Buffer.from(AsnSerializer.serialize(keyInfo));
assert.equal(asnKeyInfo.equals(bytes), true);
});
private static exportPkcs8Key(key: EcCryptoKey) {
const keyInfo = new asn.PrivateKeyInfo();
keyInfo.privateKeyAlgorithm.algorithm = this.ASN_ALGORITHM;
keyInfo.privateKeyAlgorithm.parameters = AsnSerializer.serialize(
new asn.ObjectIdentifier(getOidByNamedCurve(key.algorithm.namedCurve)),
);
keyInfo.privateKey = AsnSerializer.serialize(this.exportEcKey(key));
return AsnSerializer.serialize(keyInfo);
}
public fromJSON(json: JsonWebKey) {
const key = JsonParser.fromJSON(json, { targetSchema: asn.EcPublicKey });
const keyInfo = new asn.PublicKeyInfo();
keyInfo.publicKeyAlgorithm.algorithm = "1.2.840.10045.2.1";
keyInfo.publicKeyAlgorithm.parameters = AsnSerializer.serialize(
new ObjectIdentifier(getOidByNamedCurve(json.crv!)),
);
keyInfo.publicKey = AsnSerializer.toASN(key).valueHex;
this.data = Buffer.from(AsnSerializer.serialize(keyInfo));
return this;
}
}
protected static async importPrivateKey(asnKey: asn.EcPrivateKey, algorithm: EcKeyImportParams, extractable: boolean, keyUsages: KeyUsage[]) {
const keyInfo = new asn.PrivateKeyInfo();
keyInfo.privateKeyAlgorithm.algorithm = "1.2.840.10045.2.1";
keyInfo.privateKeyAlgorithm.parameters = AsnSerializer.serialize(new ObjectIdentifier(getOidByNamedCurve(algorithm.namedCurve)));
keyInfo.privateKey = AsnSerializer.serialize(asnKey);
const key = new EcPrivateKey();
key.data = Buffer.from(AsnSerializer.serialize(keyInfo));
key.algorithm = Object.assign({}, algorithm) as EcKeyAlgorithm;
key.extractable = extractable;
key.usages = keyUsages;
return key;
}
protected static importPublicKey(asnKey: asn.RsaPublicKey, algorithm: RsaHashedImportParams, extractable: boolean, keyUsages: KeyUsage[]) {
const keyInfo = new asn.PublicKeyInfo();
keyInfo.publicKeyAlgorithm.algorithm = "1.2.840.113549.1.1.1";
keyInfo.publicKeyAlgorithm.parameters = null;
keyInfo.publicKey = AsnSerializer.serialize(asnKey);
const key = new RsaPublicKey();
key.data = Buffer.from(AsnSerializer.serialize(keyInfo));
key.algorithm = Object.assign({}, algorithm) as RsaHashedKeyAlgorithm;
key.algorithm.publicExponent = new Uint8Array(asnKey.publicExponent);
key.algorithm.modulusLength = asnKey.modulus.byteLength << 3;
key.extractable = extractable;
key.usages = keyUsages;
return key;
}
public fromJSON(json: JsonWebKey) {
const key = JsonParser.fromJSON(json, { targetSchema: asn.RsaPrivateKey });
const keyInfo = new asn.PrivateKeyInfo();
keyInfo.privateKeyAlgorithm.algorithm = "1.2.840.113549.1.1.1";
keyInfo.privateKeyAlgorithm.parameters = null;
keyInfo.privateKey = AsnSerializer.serialize(key);
this.data = Buffer.from(AsnSerializer.serialize(keyInfo));
}
public fromJSON(json: JsonWebKey) {
const key = JsonParser.fromJSON(json, { targetSchema: asn.RsaPublicKey });
const keyInfo = new asn.PublicKeyInfo();
keyInfo.publicKeyAlgorithm.algorithm = "1.2.840.113549.1.1.1";
keyInfo.publicKeyAlgorithm.parameters = null;
keyInfo.publicKey = AsnSerializer.serialize(key);
this.data = Buffer.from(AsnSerializer.serialize(keyInfo));
}
}
private static exportPkcs8Key(key: RsaCryptoKey) {
const keyInfo = new asn.PrivateKeyInfo();
keyInfo.privateKeyAlgorithm.algorithm = "1.2.840.113549.1.1.1";
keyInfo.privateKeyAlgorithm.parameters = null;
keyInfo.privateKey = AsnSerializer.serialize(this.exportAsmKey(key.data));
return AsnSerializer.serialize(keyInfo);
}