DigestInfo.ts (4824B)
1 import * as asn1js from "asn1js"; 2 import * as pvutils from "pvutils"; 3 import { AlgorithmIdentifier, AlgorithmIdentifierJson, AlgorithmIdentifierSchema } from "./AlgorithmIdentifier"; 4 import { EMPTY_STRING } from "./constants"; 5 import { AsnError } from "./errors"; 6 import { PkiObject, PkiObjectParameters } from "./PkiObject"; 7 import * as Schema from "./Schema"; 8 9 const DIGEST_ALGORITHM = "digestAlgorithm"; 10 const DIGEST = "digest"; 11 const CLEAR_PROPS = [ 12 DIGEST_ALGORITHM, 13 DIGEST 14 ]; 15 16 export interface IDigestInfo { 17 digestAlgorithm: AlgorithmIdentifier; 18 digest: asn1js.OctetString; 19 } 20 21 export interface DigestInfoJson { 22 digestAlgorithm: AlgorithmIdentifierJson; 23 digest: asn1js.OctetStringJson; 24 } 25 26 export type DigestInfoParameters = PkiObjectParameters & Partial<IDigestInfo>; 27 28 export type DigestInfoSchema = Schema.SchemaParameters<{ 29 digestAlgorithm?: AlgorithmIdentifierSchema; 30 digest?: string; 31 }>; 32 33 /** 34 * Represents the DigestInfo structure described in [RFC3447](https://datatracker.ietf.org/doc/html/rfc3447) 35 */ 36 export class DigestInfo extends PkiObject implements IDigestInfo { 37 38 public static override CLASS_NAME = "DigestInfo"; 39 40 public digestAlgorithm!: AlgorithmIdentifier; 41 public digest!: asn1js.OctetString; 42 43 /** 44 * Initializes a new instance of the {@link DigestInfo} class 45 * @param parameters Initialization parameters 46 */ 47 constructor(parameters: DigestInfoParameters = {}) { 48 super(); 49 50 this.digestAlgorithm = pvutils.getParametersValue(parameters, DIGEST_ALGORITHM, DigestInfo.defaultValues(DIGEST_ALGORITHM)); 51 this.digest = pvutils.getParametersValue(parameters, DIGEST, DigestInfo.defaultValues(DIGEST)); 52 53 if (parameters.schema) { 54 this.fromSchema(parameters.schema); 55 } 56 } 57 58 /** 59 * Returns default values for all class members 60 * @param memberName String name for a class member 61 * @returns Default value 62 */ 63 public static override defaultValues(memberName: typeof DIGEST_ALGORITHM): AlgorithmIdentifier; 64 public static override defaultValues(memberName: typeof DIGEST): asn1js.OctetString; 65 public static override defaultValues(memberName: string): any { 66 switch (memberName) { 67 case DIGEST_ALGORITHM: 68 return new AlgorithmIdentifier(); 69 case DIGEST: 70 return new asn1js.OctetString(); 71 default: 72 return super.defaultValues(memberName); 73 } 74 } 75 76 /** 77 * Compare values with default values for all class members 78 * @param memberName String name for a class member 79 * @param memberValue Value to compare with default value 80 */ 81 public static compareWithDefault(memberName: string, memberValue: any): boolean { 82 switch (memberName) { 83 case DIGEST_ALGORITHM: 84 return ((AlgorithmIdentifier.compareWithDefault("algorithmId", memberValue.algorithmId)) && 85 (("algorithmParams" in memberValue) === false)); 86 case DIGEST: 87 return (memberValue.isEqual(DigestInfo.defaultValues(memberName))); 88 default: 89 return super.defaultValues(memberName); 90 } 91 } 92 93 /** 94 * @inheritdoc 95 * @asn ASN.1 schema 96 * ```asn 97 * DigestInfo ::= SEQUENCE { 98 * digestAlgorithm DigestAlgorithmIdentifier, 99 * digest Digest } 100 * 101 * Digest ::= OCTET STRING 102 *``` 103 */ 104 public static override schema(parameters: DigestInfoSchema = {}): Schema.SchemaType { 105 const names = pvutils.getParametersValue<NonNullable<typeof parameters.names>>(parameters, "names", {}); 106 107 return (new asn1js.Sequence({ 108 name: (names.blockName || EMPTY_STRING), 109 value: [ 110 AlgorithmIdentifier.schema(names.digestAlgorithm || { 111 names: { 112 blockName: DIGEST_ALGORITHM 113 } 114 }), 115 new asn1js.OctetString({ name: (names.digest || DIGEST) }) 116 ] 117 })); 118 } 119 120 public fromSchema(schema: Schema.SchemaType): void { 121 // Clear input data first 122 pvutils.clearProps(schema, CLEAR_PROPS); 123 124 // Check the schema is valid 125 const asn1 = asn1js.compareSchema(schema, 126 schema, 127 DigestInfo.schema({ 128 names: { 129 digestAlgorithm: { 130 names: { 131 blockName: DIGEST_ALGORITHM 132 } 133 }, 134 digest: DIGEST 135 } 136 }) 137 ); 138 AsnError.assertSchema(asn1, this.className); 139 140 // Get internal properties from parsed schema 141 this.digestAlgorithm = new AlgorithmIdentifier({ schema: asn1.result.digestAlgorithm }); 142 this.digest = asn1.result.digest; 143 } 144 145 public toSchema(): asn1js.Sequence { 146 // Construct and return new ASN.1 schema for this object 147 return (new asn1js.Sequence({ 148 value: [ 149 this.digestAlgorithm.toSchema(), 150 this.digest 151 ] 152 })); 153 } 154 155 public toJSON(): DigestInfoJson { 156 return { 157 digestAlgorithm: this.digestAlgorithm.toJSON(), 158 digest: this.digest.toJSON(), 159 }; 160 } 161 162 }