Source: sha256-with-ecdsa-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; /** @ignore */
  24. var ValidityPeriod = require('./security/validity-period.js').ValidityPeriod;
  25. /**
  26. * Create a new Sha256WithEcdsaSignature object, possibly copying values from
  27. * another object.
  28. *
  29. * @param {Sha256WithEcdsaSignature} value (optional) If value is a
  30. * Sha256WithEcdsaSignature, copy its values. If value is omitted, the
  31. * keyLocator is the default with unspecified values and the signature is
  32. * unspecified.
  33. * @constructor
  34. */
  35. var Sha256WithEcdsaSignature = function Sha256WithEcdsaSignature(value)
  36. {
  37. if (typeof value === 'object' && value instanceof Sha256WithEcdsaSignature) {
  38. // Copy the values.
  39. this.keyLocator_ = new ChangeCounter(new KeyLocator(value.getKeyLocator()));
  40. this.validityPeriod_ = new ChangeCounter(new ValidityPeriod
  41. (value.getValidityPeriod()));
  42. this.signature_ = value.signature_;
  43. }
  44. else {
  45. this.keyLocator_ = new ChangeCounter(new KeyLocator());
  46. this.validityPeriod_ = new ChangeCounter(new ValidityPeriod());
  47. this.signature_ = new Blob();
  48. }
  49. this.changeCount_ = 0;
  50. };
  51. exports.Sha256WithEcdsaSignature = Sha256WithEcdsaSignature;
  52. /**
  53. * Create a new Sha256WithEcdsaSignature which is a copy of this object.
  54. * @return {Sha256WithEcdsaSignature} A new object which is a copy of this
  55. * object.
  56. */
  57. Sha256WithEcdsaSignature.prototype.clone = function()
  58. {
  59. return new Sha256WithEcdsaSignature(this);
  60. };
  61. /**
  62. * Get the key locator.
  63. * @return {KeyLocator} The key locator.
  64. */
  65. Sha256WithEcdsaSignature.prototype.getKeyLocator = function()
  66. {
  67. return this.keyLocator_.get();
  68. };
  69. /**
  70. * Get the validity period.
  71. * @return {ValidityPeriod} The validity period.
  72. */
  73. Sha256WithEcdsaSignature.prototype.getValidityPeriod = function()
  74. {
  75. return this.validityPeriod_.get();
  76. };
  77. /**
  78. * Get the data packet's signature bytes.
  79. * @return {Blob} The signature bytes. If not specified, the value isNull().
  80. */
  81. Sha256WithEcdsaSignature.prototype.getSignature = function()
  82. {
  83. return this.signature_;
  84. };
  85. /**
  86. * Set the key locator to a copy of the given keyLocator.
  87. * @param {KeyLocator} keyLocator The KeyLocator to copy.
  88. */
  89. Sha256WithEcdsaSignature.prototype.setKeyLocator = function(keyLocator)
  90. {
  91. this.keyLocator_.set(typeof keyLocator === 'object' &&
  92. keyLocator instanceof KeyLocator ?
  93. new KeyLocator(keyLocator) : new KeyLocator());
  94. ++this.changeCount_;
  95. };
  96. /**
  97. * Set the validity period to a copy of the given ValidityPeriod.
  98. * @param {ValidityPeriod} validityPeriod The ValidityPeriod which is copied.
  99. */
  100. Sha256WithEcdsaSignature.prototype.setValidityPeriod = function(validityPeriod)
  101. {
  102. this.validityPeriod_.set(typeof validityPeriod === 'object' &&
  103. validityPeriod instanceof ValidityPeriod ?
  104. new ValidityPeriod(validityPeriod) : new ValidityPeriod());
  105. ++this.changeCount_;
  106. };
  107. /**
  108. * Set the data packet's signature bytes.
  109. * @param {Blob} signature
  110. */
  111. Sha256WithEcdsaSignature.prototype.setSignature = function(signature)
  112. {
  113. this.signature_ = typeof signature === 'object' && signature instanceof Blob ?
  114. signature : new Blob(signature);
  115. ++this.changeCount_;
  116. };
  117. /**
  118. * Get the change count, which is incremented each time this object (or a child
  119. * object) is changed.
  120. * @return {number} The change count.
  121. */
  122. Sha256WithEcdsaSignature.prototype.getChangeCount = function()
  123. {
  124. // Make sure each of the checkChanged is called.
  125. var changed = this.keyLocator_.checkChanged();
  126. changed = this.validityPeriod_.checkChanged() || changed;
  127. if (changed)
  128. // A child object has changed, so update the change count.
  129. ++this.changeCount_;
  130. return this.changeCount_;
  131. };