@peculiar/webcrypto
Version:
A WebCrypto Polyfill for NodeJS
38 lines (37 loc) • 1.29 kB
JavaScript
/**
* Copyright (c) 2019-2026, Peculiar Ventures
* SPDX-License-Identifier: MIT
*/
import { AsymmetricKey } from "../../keys/asymmetric.js";
import { getJwkAlgorithm } from "./helper.js";
import * as core from "webcrypto-core";
import { Buffer } from "node:buffer";
import { JsonParser, JsonSerializer } from "@peculiar/json-schema";
import { AsnParser, AsnSerializer } from "@peculiar/asn1-schema";
// src/mechs/rsa/public_key.ts
var RsaPublicKey = class extends AsymmetricKey {
type = "public";
getKey() {
const keyInfo = AsnParser.parse(this.data, core.asn1.PublicKeyInfo);
return AsnParser.parse(keyInfo.publicKey, core.asn1.RsaPublicKey);
}
toJSON() {
const key = this.getKey();
const json = {
kty: "RSA",
alg: getJwkAlgorithm(this.algorithm),
key_ops: this.usages,
ext: this.extractable
};
return Object.assign(json, JsonSerializer.toJSON(key));
}
fromJSON(json) {
const key = JsonParser.fromJSON(json, { targetSchema: core.asn1.RsaPublicKey });
const keyInfo = new core.asn1.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));
}
};
export { RsaPublicKey };