Source: generic-signature.js

  1. /**
  2. * This class represents an NDN Data Signature object.
  3. * Copyright (C) 2016-2018 Regents of the University of California.
  4. * @author: Jeff Thompson <jefft0@remap.ucla.edu>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. * A copy of the GNU Lesser General Public License is in the file COPYING.
  19. */
  20. /** @ignore */
  21. var Blob = require('./util/blob.js').Blob;
  22. /**
  23. * A GenericSignature extends Signature and holds the encoding bytes of the
  24. * SignatureInfo so that the application can process experimental signature
  25. * types. When decoding a packet, if the type of SignatureInfo is not
  26. * recognized, the library creates a GenericSignature.
  27. * Create a new GenericSignature object, possibly copying values from another
  28. * object.
  29. *
  30. * @param {GenericSignature} value (optional) If value is a GenericSignature,
  31. * copy its values.
  32. * @constructor
  33. */
  34. var GenericSignature = function GenericSignature(value)
  35. {
  36. if (typeof value === 'object' && value instanceof GenericSignature) {
  37. // Copy the values.
  38. this.signature_ = value.signature_;
  39. this.signatureInfoEncoding_ = value.signatureInfoEncoding_;
  40. this.typeCode_ = value.typeCode_;
  41. }
  42. else {
  43. this.signature_ = new Blob();
  44. this.signatureInfoEncoding_ = new Blob();
  45. this.typeCode_ = null;
  46. }
  47. this.changeCount_ = 0;
  48. };
  49. exports.GenericSignature = GenericSignature;
  50. /**
  51. * Create a new GenericSignature which is a copy of this object.
  52. * @return {GenericSignature} A new object which is a copy of this object.
  53. */
  54. GenericSignature.prototype.clone = function()
  55. {
  56. return new GenericSignature(this);
  57. };
  58. /**
  59. * Get the data packet's signature bytes.
  60. * @return {Blob} The signature bytes. If not specified, the value isNull().
  61. */
  62. GenericSignature.prototype.getSignature = function()
  63. {
  64. return this.signature_;
  65. };
  66. /**
  67. * @deprecated Use getSignature. This method returns a Buffer which is the former
  68. * behavior of getSignature, and should only be used while updating your code.
  69. */
  70. GenericSignature.prototype.getSignatureAsBuffer = function()
  71. {
  72. return this.signature_.buf();
  73. };
  74. /**
  75. * Get the bytes of the entire signature info encoding (including the type
  76. * code).
  77. * @return {Blob} The encoding bytes. If not specified, the value isNull().
  78. */
  79. GenericSignature.prototype.getSignatureInfoEncoding = function()
  80. {
  81. return this.signatureInfoEncoding_;
  82. };
  83. /**
  84. * Get the type code of the signature type. When wire decode calls
  85. * setSignatureInfoEncoding, it sets the type code. Note that the type code
  86. * is ignored during wire encode, which simply uses getSignatureInfoEncoding()
  87. * where the encoding already has the type code.
  88. * @return {number} The type code, or null if not known.
  89. */
  90. GenericSignature.prototype.getTypeCode = function() { return this.typeCode_; };
  91. /**
  92. * Set the data packet's signature bytes.
  93. * @param {Blob} signature
  94. */
  95. GenericSignature.prototype.setSignature = function(signature)
  96. {
  97. this.signature_ = typeof signature === 'object' && signature instanceof Blob ?
  98. signature : new Blob(signature);
  99. ++this.changeCount_;
  100. };
  101. /**
  102. * Set the bytes of the entire signature info encoding (including the type
  103. * code).
  104. * @param {Blob} signatureInfoEncoding A Blob with the encoding bytes.
  105. * @param {number} (optional) The type code of the signature type, or null if
  106. * not known. (When a GenericSignature is created by wire decoding, it sets
  107. * the typeCode.)
  108. */
  109. GenericSignature.prototype.setSignatureInfoEncoding = function
  110. (signatureInfoEncoding, typeCode)
  111. {
  112. this.signatureInfoEncoding_ =
  113. typeof signatureInfoEncoding === 'object' && signatureInfoEncoding instanceof Blob ?
  114. signatureInfoEncoding : new Blob(signatureInfoEncoding);
  115. this.typeCode_ = typeCode;
  116. ++this.changeCount_;
  117. };
  118. /**
  119. * Get the change count, which is incremented each time this object (or a child
  120. * object) is changed.
  121. * @return {number} The change count.
  122. */
  123. GenericSignature.prototype.getChangeCount = function()
  124. {
  125. return this.changeCount_;
  126. };
  127. /**
  128. * @@deprecated Use getSignature and setSignature.
  129. */
  130. Object.defineProperty(GenericSignature.prototype, "signature",
  131. { get: function() { return this.getSignatureAsBuffer(); },
  132. set: function(val) { this.setSignature(val); } });