UNPKG

pkijs

Version:

Public Key Infrastructure (PKI) is the basis of how identity and key management is performed on the web today. PKIjs is a pure JavaScript library implementing the formats that are used in PKI applications. It is built on WebCrypto and aspires to make it p

169 lines (162 loc) 5.09 kB
import * as asn1js from "asn1js"; import { getParametersValue } from "pvutils"; import AlgorithmIdentifier from "./AlgorithmIdentifier.js"; //************************************************************************************** /** * Class from RFC3447 */ export default class DigestInfo { //********************************************************************************** /** * Constructor for DigestInfo class * @param {Object} [parameters={}] * @property {Object} [schema] asn1js parsed value */ constructor(parameters = {}) { //region Internal properties of the object /** * @type {AlgorithmIdentifier} * @description digestAlgorithm */ this.digestAlgorithm = getParametersValue(parameters, "digestAlgorithm", DigestInfo.defaultValues("digestAlgorithm")); /** * @type {OctetString} * @description digest */ this.digest = getParametersValue(parameters, "digest", DigestInfo.defaultValues("digest")); //endregion //region If input argument array contains "schema" for this object if("schema" in parameters) this.fromSchema(parameters.schema); //endregion } //********************************************************************************** /** * Return default values for all class members * @param {string} memberName String name for a class member */ static defaultValues(memberName) { switch(memberName) { case "digestAlgorithm": return new AlgorithmIdentifier(); case "digest": return new asn1js.OctetString(); default: throw new Error(`Invalid member name for DigestInfo class: ${memberName}`); } } //********************************************************************************** /** * Compare values with default values for all class members * @param {string} memberName String name for a class member * @param {*} memberValue Value to compare with default value */ static compareWithDefault(memberName, memberValue) { switch(memberName) { case "digestAlgorithm": return ((AlgorithmIdentifier.compareWithDefault("algorithmId", memberValue.algorithmId)) && (("algorithmParams" in memberValue) === false)); case "digest": return (memberValue.isEqual(DigestInfo.defaultValues(memberName))); default: throw new Error(`Invalid member name for DigestInfo class: ${memberName}`); } } //********************************************************************************** /** * Return value of asn1js schema for current class * @param {Object} parameters Input parameters for the schema * @returns {Object} asn1js schema object */ static schema(parameters = {}) { //DigestInfo ::= SEQUENCE { // digestAlgorithm DigestAlgorithmIdentifier, // digest Digest } //Digest ::= OCTET STRING /** * @type {Object} * @property {string} [blockName] * @property {string} [type] * @property {string} [setName] * @property {string} [values] */ const names = getParametersValue(parameters, "names", {}); return (new asn1js.Sequence({ name: (names.blockName || ""), value: [ AlgorithmIdentifier.schema(names.digestAlgorithm || { names: { blockName: "digestAlgorithm" } }), new asn1js.OctetString({ name: (names.digest || "digest") }) ] })); } //********************************************************************************** /** * Convert parsed asn1js object into current class * @param {!Object} schema */ fromSchema(schema) { //region Check the schema is valid const asn1 = asn1js.compareSchema(schema, schema, DigestInfo.schema({ names: { digestAlgorithm: { names: { blockName: "digestAlgorithm" } }, digest: "digest" } }) ); if(asn1.verified === false) throw new Error("Object's schema was not verified against input data for DigestInfo"); //endregion //region Get internal properties from parsed schema this.digestAlgorithm = new AlgorithmIdentifier({ schema: asn1.result.digestAlgorithm }); this.digest = asn1.result.digest; //endregion } //********************************************************************************** /** * Convert current object to asn1js object and set correct values * @returns {Object} asn1js object */ toSchema() { //region Construct and return new ASN.1 schema for this object return (new asn1js.Sequence({ value: [ this.digestAlgorithm.toSchema(), this.digest ] })); //endregion } //********************************************************************************** /** * Convertion for the class to JSON object * @returns {Object} */ toJSON() { return { digestAlgorithm: this.digestAlgorithm.toJSON(), digest: this.digest.toJSON() }; } //********************************************************************************** } //**************************************************************************************