scrypt.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { scrypt as _nobleSync, scryptAsync as _nobleAsync } from "@noble/hashes/scrypt";
  2. import { getBytes, hexlify as H } from "../utils/index.js";
  3. import type { BytesLike } from "../utils/index.js";
  4. /**
  5. * A callback during long-running operations to update any
  6. * UI or provide programatic access to the progress.
  7. *
  8. * The %%percent%% is a value between ``0`` and ``1``.
  9. *
  10. * @_docloc: api/crypto:Passwords
  11. */
  12. export type ProgressCallback = (percent: number) => void;
  13. let lockedSync = false, lockedAsync = false;
  14. const _scryptAsync = async function(passwd: Uint8Array, salt: Uint8Array, N: number, r: number, p: number, dkLen: number, onProgress?: ProgressCallback) {
  15. return await _nobleAsync(passwd, salt, { N, r, p, dkLen, onProgress });
  16. }
  17. const _scryptSync = function(passwd: Uint8Array, salt: Uint8Array, N: number, r: number, p: number, dkLen: number) {
  18. return _nobleSync(passwd, salt, { N, r, p, dkLen });
  19. }
  20. let __scryptAsync: (passwd: Uint8Array, salt: Uint8Array, N: number, r: number, p: number, dkLen: number, onProgress?: ProgressCallback) => Promise<BytesLike> = _scryptAsync;
  21. let __scryptSync: (passwd: Uint8Array, salt: Uint8Array, N: number, r: number, p: number, dkLen: number) => BytesLike = _scryptSync
  22. /**
  23. * The [[link-wiki-scrypt]] uses a memory and cpu hard method of
  24. * derivation to increase the resource cost to brute-force a password
  25. * for a given key.
  26. *
  27. * This means this algorithm is intentionally slow, and can be tuned to
  28. * become slower. As computation and memory speed improve over time,
  29. * increasing the difficulty maintains the cost of an attacker.
  30. *
  31. * For example, if a target time of 5 seconds is used, a legitimate user
  32. * which knows their password requires only 5 seconds to unlock their
  33. * account. A 6 character password has 68 billion possibilities, which
  34. * would require an attacker to invest over 10,000 years of CPU time. This
  35. * is of course a crude example (as password generally aren't random),
  36. * but demonstrates to value of imposing large costs to decryption.
  37. *
  38. * For this reason, if building a UI which involved decrypting or
  39. * encrypting datsa using scrypt, it is recommended to use a
  40. * [[ProgressCallback]] (as event short periods can seem lik an eternity
  41. * if the UI freezes). Including the phrase //"decrypting"// in the UI
  42. * can also help, assuring the user their waiting is for a good reason.
  43. *
  44. * @_docloc: api/crypto:Passwords
  45. *
  46. * @example:
  47. * // The password must be converted to bytes, and it is generally
  48. * // best practices to ensure the string has been normalized. Many
  49. * // formats explicitly indicate the normalization form to use.
  50. * password = "hello"
  51. * passwordBytes = toUtf8Bytes(password, "NFKC")
  52. *
  53. * salt = id("some-salt")
  54. *
  55. * // Compute the scrypt
  56. * scrypt(passwordBytes, salt, 1024, 8, 1, 16)
  57. * //_result:
  58. */
  59. export async function scrypt(_passwd: BytesLike, _salt: BytesLike, N: number, r: number, p: number, dkLen: number, progress?: ProgressCallback): Promise<string> {
  60. const passwd = getBytes(_passwd, "passwd");
  61. const salt = getBytes(_salt, "salt");
  62. return H(await __scryptAsync(passwd, salt, N, r, p, dkLen, progress));
  63. }
  64. scrypt._ = _scryptAsync;
  65. scrypt.lock = function(): void { lockedAsync = true; }
  66. scrypt.register = function(func: (passwd: Uint8Array, salt: Uint8Array, N: number, r: number, p: number, dkLen: number, progress?: ProgressCallback) => Promise<BytesLike>) {
  67. if (lockedAsync) { throw new Error("scrypt is locked"); }
  68. __scryptAsync = func;
  69. }
  70. Object.freeze(scrypt);
  71. /**
  72. * Provides a synchronous variant of [[scrypt]].
  73. *
  74. * This will completely lock up and freeze the UI in a browser and will
  75. * prevent any event loop from progressing. For this reason, it is
  76. * preferred to use the [async variant](scrypt).
  77. *
  78. * @_docloc: api/crypto:Passwords
  79. *
  80. * @example:
  81. * // The password must be converted to bytes, and it is generally
  82. * // best practices to ensure the string has been normalized. Many
  83. * // formats explicitly indicate the normalization form to use.
  84. * password = "hello"
  85. * passwordBytes = toUtf8Bytes(password, "NFKC")
  86. *
  87. * salt = id("some-salt")
  88. *
  89. * // Compute the scrypt
  90. * scryptSync(passwordBytes, salt, 1024, 8, 1, 16)
  91. * //_result:
  92. */
  93. export function scryptSync(_passwd: BytesLike, _salt: BytesLike, N: number, r: number, p: number, dkLen: number): string {
  94. const passwd = getBytes(_passwd, "passwd");
  95. const salt = getBytes(_salt, "salt");
  96. return H(__scryptSync(passwd, salt, N, r, p, dkLen));
  97. }
  98. scryptSync._ = _scryptSync;
  99. scryptSync.lock = function(): void { lockedSync = true; }
  100. scryptSync.register = function(func: (passwd: Uint8Array, salt: Uint8Array, N: number, r: number, p: number, dkLen: number) => BytesLike) {
  101. if (lockedSync) { throw new Error("scryptSync is locked"); }
  102. __scryptSync = func;
  103. }
  104. Object.freeze(scryptSync);