hashes.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. if (typeof b !== "function" && b !== null)
  11. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  12. extendStatics(d, b);
  13. function __() { this.constructor = d; }
  14. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  15. };
  16. })();
  17. Object.defineProperty(exports, "__esModule", { value: true });
  18. exports.transactionID = exports.sha512Half = exports.Sha512Half = void 0;
  19. var hash_prefixes_1 = require("./hash-prefixes");
  20. var createHash = require("create-hash");
  21. var hash_256_1 = require("./types/hash-256");
  22. var binary_serializer_1 = require("./serdes/binary-serializer");
  23. var buffer_1 = require("buffer/");
  24. /**
  25. * Class for hashing with SHA512
  26. * @extends BytesList So SerializedTypes can write bytes to a Sha512Half
  27. */
  28. var Sha512Half = /** @class */ (function (_super) {
  29. __extends(Sha512Half, _super);
  30. function Sha512Half() {
  31. var _this = _super !== null && _super.apply(this, arguments) || this;
  32. _this.hash = createHash('sha512');
  33. return _this;
  34. }
  35. /**
  36. * Construct a new Sha512Hash and write bytes this.hash
  37. *
  38. * @param bytes bytes to write to this.hash
  39. * @returns the new Sha512Hash object
  40. */
  41. Sha512Half.put = function (bytes) {
  42. return new Sha512Half().put(bytes);
  43. };
  44. /**
  45. * Write bytes to an existing Sha512Hash
  46. *
  47. * @param bytes bytes to write to object
  48. * @returns the Sha512 object
  49. */
  50. Sha512Half.prototype.put = function (bytes) {
  51. this.hash.update(bytes);
  52. return this;
  53. };
  54. /**
  55. * Compute SHA512 hash and slice in half
  56. *
  57. * @returns half of a SHA512 hash
  58. */
  59. Sha512Half.prototype.finish256 = function () {
  60. return buffer_1.Buffer.from(this.hash.digest().slice(0, 32));
  61. };
  62. /**
  63. * Constructs a Hash256 from the Sha512Half object
  64. *
  65. * @returns a Hash256 object
  66. */
  67. Sha512Half.prototype.finish = function () {
  68. return new hash_256_1.Hash256(this.finish256());
  69. };
  70. return Sha512Half;
  71. }(binary_serializer_1.BytesList));
  72. exports.Sha512Half = Sha512Half;
  73. /**
  74. * compute SHA512 hash of a list of bytes
  75. *
  76. * @param args zero or more arguments to hash
  77. * @returns the sha512half hash of the arguments.
  78. */
  79. function sha512Half() {
  80. var args = [];
  81. for (var _i = 0; _i < arguments.length; _i++) {
  82. args[_i] = arguments[_i];
  83. }
  84. var hash = new Sha512Half();
  85. args.forEach(function (a) { return hash.put(a); });
  86. return hash.finish256();
  87. }
  88. exports.sha512Half = sha512Half;
  89. /**
  90. * Construct a transactionID from a Serialized Transaction
  91. *
  92. * @param serialized bytes to hash
  93. * @returns a Hash256 object
  94. */
  95. function transactionID(serialized) {
  96. return new hash_256_1.Hash256(sha512Half(hash_prefixes_1.HashPrefix.transactionID, serialized));
  97. }
  98. exports.transactionID = transactionID;
  99. //# sourceMappingURL=hashes.js.map