keccak.js 1.3 KB

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