scrypt.js 3.8 KB

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