keccak.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. /**
  3. * Cryptographic hashing functions
  4. *
  5. * @_subsection: api/crypto:Hash Functions [about-crypto-hashing]
  6. */
  7. Object.defineProperty(exports, "__esModule", { value: true });
  8. exports.keccak256 = void 0;
  9. const sha3_1 = require("@noble/hashes/sha3");
  10. const index_js_1 = require("../utils/index.js");
  11. let locked = false;
  12. const _keccak256 = function (data) {
  13. return (0, sha3_1.keccak_256)(data);
  14. };
  15. let __keccak256 = _keccak256;
  16. /**
  17. * Compute the cryptographic KECCAK256 hash of %%data%%.
  18. *
  19. * The %%data%% **must** be a data representation, to compute the
  20. * hash of UTF-8 data use the [[id]] function.
  21. *
  22. * @returns DataHexstring
  23. * @example:
  24. * keccak256("0x")
  25. * //_result:
  26. *
  27. * keccak256("0x1337")
  28. * //_result:
  29. *
  30. * keccak256(new Uint8Array([ 0x13, 0x37 ]))
  31. * //_result:
  32. *
  33. * // Strings are assumed to be DataHexString, otherwise it will
  34. * // throw. To hash UTF-8 data, see the note above.
  35. * keccak256("Hello World")
  36. * //_error:
  37. */
  38. function keccak256(_data) {
  39. const data = (0, index_js_1.getBytes)(_data, "data");
  40. return (0, index_js_1.hexlify)(__keccak256(data));
  41. }
  42. exports.keccak256 = keccak256;
  43. keccak256._ = _keccak256;
  44. keccak256.lock = function () { locked = true; };
  45. keccak256.register = function (func) {
  46. if (locked) {
  47. throw new TypeError("keccak256 is locked");
  48. }
  49. __keccak256 = func;
  50. };
  51. Object.freeze(keccak256);
  52. //# sourceMappingURL=keccak.js.map