ripemd160.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { ripemd160 as noble_ripemd160 } from "@noble/hashes/ripemd160";
  2. import { getBytes, hexlify } from "../utils/index.js";
  3. import type { BytesLike } from "../utils/index.js";
  4. let locked = false;
  5. const _ripemd160 = function(data: Uint8Array): Uint8Array {
  6. return noble_ripemd160(data);
  7. }
  8. let __ripemd160: (data: Uint8Array) => BytesLike = _ripemd160;
  9. /**
  10. * Compute the cryptographic RIPEMD-160 hash of %%data%%.
  11. *
  12. * @_docloc: api/crypto:Hash Functions
  13. * @returns DataHexstring
  14. *
  15. * @example:
  16. * ripemd160("0x")
  17. * //_result:
  18. *
  19. * ripemd160("0x1337")
  20. * //_result:
  21. *
  22. * ripemd160(new Uint8Array([ 0x13, 0x37 ]))
  23. * //_result:
  24. *
  25. */
  26. export function ripemd160(_data: BytesLike): string {
  27. const data = getBytes(_data, "data");
  28. return hexlify(__ripemd160(data));
  29. }
  30. ripemd160._ = _ripemd160;
  31. ripemd160.lock = function(): void { locked = true; }
  32. ripemd160.register = function(func: (data: Uint8Array) => BytesLike) {
  33. if (locked) { throw new TypeError("ripemd160 is locked"); }
  34. __ripemd160 = func;
  35. }
  36. Object.freeze(ripemd160);