Source: hmac-with-sha256-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; /** @ignore */
  22. var ChangeCounter = require('./util/change-counter.js').ChangeCounter; /** @ignore */
  23. var KeyLocator = require('./key-locator.js').KeyLocator;
  24. /**
  25. * An HmacWithSha256Signature holds the signature bits and other info
  26. * representing an HmacWithSha256 signature in a packet.
  27. * Create a new HmacWithSha256Signature object, possibly copying values from
  28. * another object.
  29. *
  30. * @param {HmacWithSha256Signature} value (optional) If value is a
  31. * HmacWithSha256Signature, copy its values. If value is omitted, the keyLocator
  32. * is the default with unspecified values and the signature is unspecified.
  33. * @constructor
  34. */
  35. var HmacWithSha256Signature = function HmacWithSha256Signature(value)
  36. {
  37. if (typeof value === 'object' && value instanceof HmacWithSha256Signature) {
  38. // Copy the values.
  39. this.keyLocator_ = new ChangeCounter(new KeyLocator(value.getKeyLocator()));
  40. this.signature_ = value.signature_;
  41. }
  42. else {
  43. this.keyLocator_ = new ChangeCounter(new KeyLocator());
  44. this.signature_ = new Blob();
  45. }
  46. this.changeCount_ = 0;
  47. };
  48. exports.HmacWithSha256Signature = HmacWithSha256Signature;
  49. /**
  50. * Create a new HmacWithSha256Signature which is a copy of this object.
  51. * @return {HmacWithSha256Signature} A new object which is a copy of this object.
  52. */
  53. HmacWithSha256Signature.prototype.clone = function()
  54. {
  55. return new HmacWithSha256Signature(this);
  56. };
  57. /**
  58. * Get the key locator.
  59. * @return {KeyLocator} The key locator.
  60. */
  61. HmacWithSha256Signature.prototype.getKeyLocator = function()
  62. {
  63. return this.keyLocator_.get();
  64. };
  65. /**
  66. * Get the data packet's signature bytes.
  67. * @return {Blob} The signature bytes. If not specified, the value isNull().
  68. */
  69. HmacWithSha256Signature.prototype.getSignature = function()
  70. {
  71. return this.signature_;
  72. };
  73. /**
  74. * @deprecated Use getSignature. This method returns a Buffer which is the former
  75. * behavior of getSignature, and should only be used while updating your code.
  76. */
  77. HmacWithSha256Signature.prototype.getSignatureAsBuffer = function()
  78. {
  79. return this.signature_.buf();
  80. };
  81. /**
  82. * Set the key locator to a copy of the given keyLocator.
  83. * @param {KeyLocator} keyLocator The KeyLocator to copy.
  84. */
  85. HmacWithSha256Signature.prototype.setKeyLocator = function(keyLocator)
  86. {
  87. this.keyLocator_.set(typeof keyLocator === 'object' &&
  88. keyLocator instanceof KeyLocator ?
  89. new KeyLocator(keyLocator) : new KeyLocator());
  90. ++this.changeCount_;
  91. };
  92. /**
  93. * Set the data packet's signature bytes.
  94. * @param {Blob} signature
  95. */
  96. HmacWithSha256Signature.prototype.setSignature = function(signature)
  97. {
  98. this.signature_ = typeof signature === 'object' && signature instanceof Blob ?
  99. signature : new Blob(signature);
  100. ++this.changeCount_;
  101. };
  102. /**
  103. * Get the change count, which is incremented each time this object (or a child
  104. * object) is changed.
  105. * @return {number} The change count.
  106. */
  107. HmacWithSha256Signature.prototype.getChangeCount = function()
  108. {
  109. // Make sure each of the checkChanged is called.
  110. var changed = this.keyLocator_.checkChanged();
  111. if (changed)
  112. // A child object has changed, so update the change count.
  113. ++this.changeCount_;
  114. return this.changeCount_;
  115. };
  116. // Define properties so we can change member variable types and implement changeCount_.
  117. Object.defineProperty(HmacWithSha256Signature.prototype, "keyLocator",
  118. { get: function() { return this.getKeyLocator(); },
  119. set: function(val) { this.setKeyLocator(val); } });
  120. /**
  121. * @@deprecated Use getSignature and setSignature.
  122. */
  123. Object.defineProperty(HmacWithSha256Signature.prototype, "signature",
  124. { get: function() { return this.getSignatureAsBuffer(); },
  125. set: function(val) { this.setSignature(val); } });