Home Reference Source

src/DigestInfo.js

  1. import * as asn1js from "asn1js";
  2. import { getParametersValue, clearProps } from "pvutils";
  3. import AlgorithmIdentifier from "./AlgorithmIdentifier.js";
  4. //**************************************************************************************
  5. /**
  6. * Class from RFC3447
  7. */
  8. export default class DigestInfo
  9. {
  10. //**********************************************************************************
  11. /**
  12. * Constructor for DigestInfo class
  13. * @param {Object} [parameters={}]
  14. * @property {Object} [schema] asn1js parsed value
  15. */
  16. constructor(parameters = {})
  17. {
  18. //region Internal properties of the object
  19. /**
  20. * @type {AlgorithmIdentifier}
  21. * @description digestAlgorithm
  22. */
  23. this.digestAlgorithm = getParametersValue(parameters, "digestAlgorithm", DigestInfo.defaultValues("digestAlgorithm"));
  24. /**
  25. * @type {OctetString}
  26. * @description digest
  27. */
  28. this.digest = getParametersValue(parameters, "digest", DigestInfo.defaultValues("digest"));
  29. //endregion
  30. //region If input argument array contains "schema" for this object
  31. if("schema" in parameters)
  32. this.fromSchema(parameters.schema);
  33. //endregion
  34. }
  35. //**********************************************************************************
  36. /**
  37. * Return default values for all class members
  38. * @param {string} memberName String name for a class member
  39. */
  40. static defaultValues(memberName)
  41. {
  42. switch(memberName)
  43. {
  44. case "digestAlgorithm":
  45. return new AlgorithmIdentifier();
  46. case "digest":
  47. return new asn1js.OctetString();
  48. default:
  49. throw new Error(`Invalid member name for DigestInfo class: ${memberName}`);
  50. }
  51. }
  52. //**********************************************************************************
  53. /**
  54. * Compare values with default values for all class members
  55. * @param {string} memberName String name for a class member
  56. * @param {*} memberValue Value to compare with default value
  57. */
  58. static compareWithDefault(memberName, memberValue)
  59. {
  60. switch(memberName)
  61. {
  62. case "digestAlgorithm":
  63. return ((AlgorithmIdentifier.compareWithDefault("algorithmId", memberValue.algorithmId)) &&
  64. (("algorithmParams" in memberValue) === false));
  65. case "digest":
  66. return (memberValue.isEqual(DigestInfo.defaultValues(memberName)));
  67. default:
  68. throw new Error(`Invalid member name for DigestInfo class: ${memberName}`);
  69. }
  70. }
  71. //**********************************************************************************
  72. /**
  73. * Return value of asn1js schema for current class
  74. * @param {Object} parameters Input parameters for the schema
  75. * @returns {Object} asn1js schema object
  76. */
  77. static schema(parameters = {})
  78. {
  79. //DigestInfo ::= SEQUENCE {
  80. // digestAlgorithm DigestAlgorithmIdentifier,
  81. // digest Digest }
  82. //Digest ::= OCTET STRING
  83. /**
  84. * @type {Object}
  85. * @property {string} [blockName]
  86. * @property {string} [type]
  87. * @property {string} [setName]
  88. * @property {string} [values]
  89. */
  90. const names = getParametersValue(parameters, "names", {});
  91. return (new asn1js.Sequence({
  92. name: (names.blockName || ""),
  93. value: [
  94. AlgorithmIdentifier.schema(names.digestAlgorithm || {
  95. names: {
  96. blockName: "digestAlgorithm"
  97. }
  98. }),
  99. new asn1js.OctetString({ name: (names.digest || "digest") })
  100. ]
  101. }));
  102. }
  103. //**********************************************************************************
  104. /**
  105. * Convert parsed asn1js object into current class
  106. * @param {!Object} schema
  107. */
  108. fromSchema(schema)
  109. {
  110. //region Clear input data first
  111. clearProps(schema, [
  112. "digestAlgorithm",
  113. "digest"
  114. ]);
  115. //endregion
  116. //region Check the schema is valid
  117. const asn1 = asn1js.compareSchema(schema,
  118. schema,
  119. DigestInfo.schema({
  120. names: {
  121. digestAlgorithm: {
  122. names: {
  123. blockName: "digestAlgorithm"
  124. }
  125. },
  126. digest: "digest"
  127. }
  128. })
  129. );
  130. if(asn1.verified === false)
  131. throw new Error("Object's schema was not verified against input data for DigestInfo");
  132. //endregion
  133. //region Get internal properties from parsed schema
  134. this.digestAlgorithm = new AlgorithmIdentifier({ schema: asn1.result.digestAlgorithm });
  135. this.digest = asn1.result.digest;
  136. //endregion
  137. }
  138. //**********************************************************************************
  139. /**
  140. * Convert current object to asn1js object and set correct values
  141. * @returns {Object} asn1js object
  142. */
  143. toSchema()
  144. {
  145. //region Construct and return new ASN.1 schema for this object
  146. return (new asn1js.Sequence({
  147. value: [
  148. this.digestAlgorithm.toSchema(),
  149. this.digest
  150. ]
  151. }));
  152. //endregion
  153. }
  154. //**********************************************************************************
  155. /**
  156. * Convertion for the class to JSON object
  157. * @returns {Object}
  158. */
  159. toJSON()
  160. {
  161. return {
  162. digestAlgorithm: this.digestAlgorithm.toJSON(),
  163. digest: this.digest.toJSON()
  164. };
  165. }
  166. //**********************************************************************************
  167. }
  168. //**************************************************************************************