UNPKG

@wildboar/pkcs

Version:
132 lines (131 loc) 4.14 kB
/* eslint-disable */ import { ASN1ConstructionError as _ConstructionError, ASN1TagClass as _TagClass, } from "@wildboar/asn1"; import * as $ from "@wildboar/asn1/functional"; /** * @summary RSAPublicKey * @description * * ### ASN.1 Definition: * * ```asn1 * RSAPublicKey ::= SEQUENCE { * modulus INTEGER, -- n * publicExponent INTEGER -- e * } * ``` * */ export class RSAPublicKey { modulus; publicExponent; constructor( /** * @summary `modulus`. * @public * @readonly */ modulus, /** * @summary `publicExponent`. * @public * @readonly */ publicExponent) { this.modulus = modulus; this.publicExponent = publicExponent; } /** * @summary Restructures an object into a RSAPublicKey * @description * * This takes an `object` and converts it to a `RSAPublicKey`. * * @public * @static * @method * @param {Object} _o An object having all of the keys and values of a `RSAPublicKey`. * @returns {RSAPublicKey} */ static _from_object(_o) { return new RSAPublicKey(_o.modulus, _o.publicExponent); } } /** * @summary The Leading Root Component Types of RSAPublicKey * @description * * This is an array of `ComponentSpec`s that define how to decode the leading root component type list of a SET or SEQUENCE. * * @constant */ export const _root_component_type_list_1_spec_for_RSAPublicKey = [ new $.ComponentSpec("modulus", false, $.hasTag(_TagClass.universal, 2)), new $.ComponentSpec("publicExponent", false, $.hasTag(_TagClass.universal, 2)), ]; /** * @summary The Trailing Root Component Types of RSAPublicKey * @description * * This is an array of `ComponentSpec`s that define how to decode the trailing root component type list of a SET or SEQUENCE. * * @constant */ export const _root_component_type_list_2_spec_for_RSAPublicKey = []; /** * @summary The Extension Addition Component Types of RSAPublicKey * @description * * This is an array of `ComponentSpec`s that define how to decode the extension addition component type list of a SET or SEQUENCE. * * @constant */ export const _extension_additions_list_spec_for_RSAPublicKey = []; let _cached_decoder_for_RSAPublicKey = null; /** * @summary Decodes an ASN.1 element into a(n) RSAPublicKey * @function * @param {_Element} el The element being decoded. * @returns {RSAPublicKey} The decoded data structure. */ export function _decode_RSAPublicKey(el) { if (!_cached_decoder_for_RSAPublicKey) { _cached_decoder_for_RSAPublicKey = function (el) { const sequence = el.sequence; if (sequence.length < 2) { throw new _ConstructionError("RSAPublicKey contained only " + sequence.length.toString() + " elements."); } sequence[0].name = "modulus"; sequence[1].name = "publicExponent"; let modulus; let publicExponent; modulus = $._decodeInteger(sequence[0]); publicExponent = $._decodeInteger(sequence[1]); return new RSAPublicKey(modulus, publicExponent); }; } return _cached_decoder_for_RSAPublicKey(el); } let _cached_encoder_for_RSAPublicKey = null; /** * @summary Encodes a(n) RSAPublicKey into an ASN.1 Element. * @function * @param value The element being encoded. * @param elGetter A function that can be used to get new ASN.1 elements. * @returns {_Element} The RSAPublicKey, encoded as an ASN.1 Element. */ export function _encode_RSAPublicKey(value, elGetter) { if (!_cached_encoder_for_RSAPublicKey) { _cached_encoder_for_RSAPublicKey = function (value) { return $._encodeSequence([] .concat([ /* REQUIRED */ $._encodeInteger(value.modulus, $.BER), /* REQUIRED */ $._encodeInteger(value.publicExponent, $.BER), ]) .filter((c) => !!c), $.BER); }; } return _cached_encoder_for_RSAPublicKey(value, elGetter); } /* eslint-enable */