import * as asn1js from "asn1js"; import * as pvutils from "pvutils"; import { AlgorithmIdentifier, AlgorithmIdentifierJson, AlgorithmIdentifierSchema } from "./AlgorithmIdentifier"; import { Attribute, AttributeJson } from "./Attribute"; import { EMPTY_STRING } from "./constants"; import { ECPrivateKey } from "./ECPrivateKey"; import { AsnError } from "./errors"; import { PkiObject, PkiObjectParameters } from "./PkiObject"; import { RSAPrivateKey } from "./RSAPrivateKey"; import * as Schema from "./Schema";
/** * Represents the PrivateKeyInfo structure described in [RFC5280](https://datatracker.ietf.org/doc/html/rfc5208)
*/
export class PrivateKeyInfo extends PkiObject implements IPrivateKeyInfo {
public version!: number; public privateKeyAlgorithm!: AlgorithmIdentifier; public privateKey!: asn1js.OctetString; public attributes?: Attribute[]; public parsedKey?: RSAPrivateKey | ECPrivateKey;
/** * Initializes a new instance of the {@link PrivateKeyInfo} class * @param parameters Initialization parameters
*/
constructor(parameters: PrivateKeyInfoParameters = {}) { super();
this.version = pvutils.getParametersValue(parameters, VERSION, PrivateKeyInfo.defaultValues(VERSION)); this.privateKeyAlgorithm = pvutils.getParametersValue(parameters, PRIVATE_KEY_ALGORITHM, PrivateKeyInfo.defaultValues(PRIVATE_KEY_ALGORITHM)); this.privateKey = pvutils.getParametersValue(parameters, PRIVATE_KEY, PrivateKeyInfo.defaultValues(PRIVATE_KEY)); if (ATTRIBUTES in parameters) { this.attributes = pvutils.getParametersValue(parameters, ATTRIBUTES, PrivateKeyInfo.defaultValues(ATTRIBUTES));
} if (PARSED_KEY in parameters) { this.parsedKey = pvutils.getParametersValue(parameters, PARSED_KEY, PrivateKeyInfo.defaultValues(PARSED_KEY));
}
if (parameters.json) { this.fromJSON(parameters.json);
}
if (parameters.schema) { this.fromSchema(parameters.schema);
}
}
/** * Returns default values for all class members * @param memberName String name for a class member * @returns Default value
*/ publicstatic override defaultValues(memberName: string): any { switch (memberName) { case VERSION: return0; case PRIVATE_KEY_ALGORITHM: returnnew AlgorithmIdentifier(); case PRIVATE_KEY: returnnew asn1js.OctetString(); case ATTRIBUTES: return []; case PARSED_KEY: return {}; default: returnsuper.defaultValues(memberName);
}
}
//#region Construct and return new ASN.1 schema for this object return (new asn1js.Sequence({
value: outputArray
})); //#endregion
}
public toJSON(): PrivateKeyInfoJson | JsonWebKey { //#region Return common value in case we do not have enough info fo making JWK if (!this.parsedKey) { const object: PrivateKeyInfoJson = {
version: this.version,
privateKeyAlgorithm: this.privateKeyAlgorithm.toJSON(),
privateKey: this.privateKey.toJSON(),
};
if (this.attributes) {
object.attributes = Array.from(this.attributes, o => o.toJSON());
}
// TODO Unclear behavior const publicKeyJWK = this.parsedKey.toJSON();
Object.assign(jwk, publicKeyJWK);
return jwk; //#endregion
}
/** * Converts JSON value into current object * @param json JSON object
*/ public fromJSON(json: any): void { if ("kty" in json) { switch (json.kty.toUpperCase()) { case"EC": this.parsedKey = new ECPrivateKey({ json });
this.privateKeyAlgorithm = new AlgorithmIdentifier({
algorithmId: "1.2.840.10045.2.1",
algorithmParams: new asn1js.ObjectIdentifier({ value: this.parsedKey.namedCurve })
}); break; case"RSA": this.parsedKey = new RSAPrivateKey({ json });
this.privateKeyAlgorithm = new AlgorithmIdentifier({
algorithmId: "1.2.840.113549.1.1.1",
algorithmParams: new asn1js.Null()
}); break; default: thrownew Error(`Invalid value for"kty" parameter: ${json.kty}`);
}
this.privateKey = new asn1js.OctetString({ valueHex: this.parsedKey.toSchema().toBER(false) });
}
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.13 Sekunden
(vorverarbeitet am 2026-06-06)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.