sha2.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.sha512 = exports.sha256 = void 0;
  4. const crypto_js_1 = require("./crypto.js");
  5. const index_js_1 = require("../utils/index.js");
  6. const _sha256 = function (data) {
  7. return (0, crypto_js_1.createHash)("sha256").update(data).digest();
  8. };
  9. const _sha512 = function (data) {
  10. return (0, crypto_js_1.createHash)("sha512").update(data).digest();
  11. };
  12. let __sha256 = _sha256;
  13. let __sha512 = _sha512;
  14. let locked256 = false, locked512 = false;
  15. /**
  16. * Compute the cryptographic SHA2-256 hash of %%data%%.
  17. *
  18. * @_docloc: api/crypto:Hash Functions
  19. * @returns DataHexstring
  20. *
  21. * @example:
  22. * sha256("0x")
  23. * //_result:
  24. *
  25. * sha256("0x1337")
  26. * //_result:
  27. *
  28. * sha256(new Uint8Array([ 0x13, 0x37 ]))
  29. * //_result:
  30. *
  31. */
  32. function sha256(_data) {
  33. const data = (0, index_js_1.getBytes)(_data, "data");
  34. return (0, index_js_1.hexlify)(__sha256(data));
  35. }
  36. exports.sha256 = sha256;
  37. sha256._ = _sha256;
  38. sha256.lock = function () { locked256 = true; };
  39. sha256.register = function (func) {
  40. if (locked256) {
  41. throw new Error("sha256 is locked");
  42. }
  43. __sha256 = func;
  44. };
  45. Object.freeze(sha256);
  46. /**
  47. * Compute the cryptographic SHA2-512 hash of %%data%%.
  48. *
  49. * @_docloc: api/crypto:Hash Functions
  50. * @returns DataHexstring
  51. *
  52. * @example:
  53. * sha512("0x")
  54. * //_result:
  55. *
  56. * sha512("0x1337")
  57. * //_result:
  58. *
  59. * sha512(new Uint8Array([ 0x13, 0x37 ]))
  60. * //_result:
  61. */
  62. function sha512(_data) {
  63. const data = (0, index_js_1.getBytes)(_data, "data");
  64. return (0, index_js_1.hexlify)(__sha512(data));
  65. }
  66. exports.sha512 = sha512;
  67. sha512._ = _sha512;
  68. sha512.lock = function () { locked512 = true; };
  69. sha512.register = function (func) {
  70. if (locked512) {
  71. throw new Error("sha512 is locked");
  72. }
  73. __sha512 = func;
  74. };
  75. Object.freeze(sha256);
  76. //# sourceMappingURL=sha2.js.map