UNPKG

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