OtherPrimeInfo.ts (5041B)
1 import * as asn1js from "asn1js"; 2 import * as pvtsutils from "pvtsutils"; 3 import * as pvutils from "pvutils"; 4 import { EMPTY_STRING } from "./constants"; 5 import { AsnError, ParameterError } from "./errors"; 6 import { PkiObject, PkiObjectParameters } from "./PkiObject"; 7 import * as Schema from "./Schema"; 8 9 const PRIME = "prime"; 10 const EXPONENT = "exponent"; 11 const COEFFICIENT = "coefficient"; 12 const CLEAR_PROPS = [ 13 PRIME, 14 EXPONENT, 15 COEFFICIENT, 16 ]; 17 18 export interface IOtherPrimeInfo { 19 prime: asn1js.Integer; 20 exponent: asn1js.Integer; 21 coefficient: asn1js.Integer; 22 } 23 24 export type OtherPrimeInfoParameters = PkiObjectParameters & Partial<IOtherPrimeInfo> & { json?: OtherPrimeInfoJson; }; 25 26 export interface OtherPrimeInfoJson { 27 r: string; 28 d: string; 29 t: string; 30 } 31 32 export type OtherPrimeInfoSchema = Schema.SchemaParameters<{ 33 prime?: string; 34 exponent?: string; 35 coefficient?: string; 36 }>; 37 38 /** 39 * Represents the OtherPrimeInfo structure described in [RFC3447](https://datatracker.ietf.org/doc/html/rfc3447) 40 */ 41 export class OtherPrimeInfo extends PkiObject implements IOtherPrimeInfo { 42 43 public static override CLASS_NAME = "OtherPrimeInfo"; 44 45 public prime!: asn1js.Integer; 46 public exponent!: asn1js.Integer; 47 public coefficient!: asn1js.Integer; 48 49 /** 50 * Initializes a new instance of the {@link OtherPrimeInfo} class 51 * @param parameters Initialization parameters 52 */ 53 constructor(parameters: OtherPrimeInfoParameters = {}) { 54 super(); 55 56 this.prime = pvutils.getParametersValue(parameters, PRIME, OtherPrimeInfo.defaultValues(PRIME)); 57 this.exponent = pvutils.getParametersValue(parameters, EXPONENT, OtherPrimeInfo.defaultValues(EXPONENT)); 58 this.coefficient = pvutils.getParametersValue(parameters, COEFFICIENT, OtherPrimeInfo.defaultValues(COEFFICIENT)); 59 60 if (parameters.json) { 61 this.fromJSON(parameters.json); 62 } 63 64 if (parameters.schema) { 65 this.fromSchema(parameters.schema); 66 } 67 } 68 69 /** 70 * Returns default values for all class members 71 * @param memberName String name for a class member 72 * @returns Default value 73 */ 74 public static override defaultValues(memberName: typeof PRIME | typeof EXPONENT | typeof COEFFICIENT): asn1js.Integer; 75 public static override defaultValues(memberName: string): any { 76 switch (memberName) { 77 case PRIME: 78 return new asn1js.Integer(); 79 case EXPONENT: 80 return new asn1js.Integer(); 81 case COEFFICIENT: 82 return new asn1js.Integer(); 83 default: 84 return super.defaultValues(memberName); 85 } 86 } 87 88 /** 89 * @inheritdoc 90 * @asn ASN.1 schema 91 * ```asn 92 * OtherPrimeInfo ::= Sequence { 93 * prime Integer, -- ri 94 * exponent Integer, -- di 95 * coefficient Integer -- ti 96 * } 97 *``` 98 */ 99 public static override schema(parameters: OtherPrimeInfoSchema = {}): Schema.SchemaType { 100 const names = pvutils.getParametersValue<NonNullable<typeof parameters.names>>(parameters, "names", {}); 101 102 return (new asn1js.Sequence({ 103 name: (names.blockName || EMPTY_STRING), 104 value: [ 105 new asn1js.Integer({ name: (names.prime || EMPTY_STRING) }), 106 new asn1js.Integer({ name: (names.exponent || EMPTY_STRING) }), 107 new asn1js.Integer({ name: (names.coefficient || EMPTY_STRING) }) 108 ] 109 })); 110 } 111 112 public fromSchema(schema: Schema.SchemaType): void { 113 // Clear input data first 114 pvutils.clearProps(schema, CLEAR_PROPS); 115 116 // Check the schema is valid 117 const asn1 = asn1js.compareSchema(schema, 118 schema, 119 OtherPrimeInfo.schema({ 120 names: { 121 prime: PRIME, 122 exponent: EXPONENT, 123 coefficient: COEFFICIENT 124 } 125 }) 126 ); 127 AsnError.assertSchema(asn1, this.className); 128 129 //#region Get internal properties from parsed schema 130 this.prime = asn1.result.prime.convertFromDER(); 131 this.exponent = asn1.result.exponent.convertFromDER(); 132 this.coefficient = asn1.result.coefficient.convertFromDER(); 133 //#endregion 134 } 135 136 public toSchema(): asn1js.Sequence { 137 return (new asn1js.Sequence({ 138 value: [ 139 this.prime.convertToDER(), 140 this.exponent.convertToDER(), 141 this.coefficient.convertToDER() 142 ] 143 })); 144 } 145 146 public toJSON(): OtherPrimeInfoJson { 147 return { 148 r: pvtsutils.Convert.ToBase64Url(this.prime.valueBlock.valueHexView), 149 d: pvtsutils.Convert.ToBase64Url(this.exponent.valueBlock.valueHexView), 150 t: pvtsutils.Convert.ToBase64Url(this.coefficient.valueBlock.valueHexView), 151 }; 152 } 153 154 /** 155 * Converts JSON value into current object 156 * @param json JSON object 157 */ 158 public fromJSON(json: OtherPrimeInfoJson): void { 159 ParameterError.assert("json", json, "r", "d", "r"); 160 161 this.prime = new asn1js.Integer({ valueHex: pvtsutils.Convert.FromBase64Url(json.r) }); 162 this.exponent = new asn1js.Integer({ valueHex: pvtsutils.Convert.FromBase64Url(json.d) }); 163 this.coefficient = new asn1js.Integer({ valueHex: pvtsutils.Convert.FromBase64Url(json.t) }); 164 } 165 166 }