選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

hashes.js 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.transactionID = exports.sha512Half = exports.Sha512Half = void 0;
  4. const hash_prefixes_1 = require("./hash-prefixes");
  5. const createHash = require("create-hash");
  6. const hash_256_1 = require("./types/hash-256");
  7. const binary_serializer_1 = require("./serdes/binary-serializer");
  8. const buffer_1 = require("buffer/");
  9. /**
  10. * Class for hashing with SHA512
  11. * @extends BytesList So SerializedTypes can write bytes to a Sha512Half
  12. */
  13. class Sha512Half extends binary_serializer_1.BytesList {
  14. constructor() {
  15. super(...arguments);
  16. this.hash = createHash('sha512');
  17. }
  18. /**
  19. * Construct a new Sha512Hash and write bytes this.hash
  20. *
  21. * @param bytes bytes to write to this.hash
  22. * @returns the new Sha512Hash object
  23. */
  24. static put(bytes) {
  25. return new Sha512Half().put(bytes);
  26. }
  27. /**
  28. * Write bytes to an existing Sha512Hash
  29. *
  30. * @param bytes bytes to write to object
  31. * @returns the Sha512 object
  32. */
  33. put(bytes) {
  34. this.hash.update(bytes);
  35. return this;
  36. }
  37. /**
  38. * Compute SHA512 hash and slice in half
  39. *
  40. * @returns half of a SHA512 hash
  41. */
  42. finish256() {
  43. return buffer_1.Buffer.from(this.hash.digest().slice(0, 32));
  44. }
  45. /**
  46. * Constructs a Hash256 from the Sha512Half object
  47. *
  48. * @returns a Hash256 object
  49. */
  50. finish() {
  51. return new hash_256_1.Hash256(this.finish256());
  52. }
  53. }
  54. exports.Sha512Half = Sha512Half;
  55. /**
  56. * compute SHA512 hash of a list of bytes
  57. *
  58. * @param args zero or more arguments to hash
  59. * @returns the sha512half hash of the arguments.
  60. */
  61. function sha512Half(...args) {
  62. const hash = new Sha512Half();
  63. args.forEach((a) => hash.put(a));
  64. return hash.finish256();
  65. }
  66. exports.sha512Half = sha512Half;
  67. /**
  68. * Construct a transactionID from a Serialized Transaction
  69. *
  70. * @param serialized bytes to hash
  71. * @returns a Hash256 object
  72. */
  73. function transactionID(serialized) {
  74. return new hash_256_1.Hash256(sha512Half(hash_prefixes_1.HashPrefix.transactionID, serialized));
  75. }
  76. exports.transactionID = transactionID;
  77. //# sourceMappingURL=hashes.js.map