UNPKG

4.85 kBTypeScriptView Raw
1/**
2 * Sets the pseudo random number generator to use as a fallback if neither node's crypto module nor the Web Crypto API is available.
3 * Please note: It is highly important that the PRNG used is cryptographically secure and that it is seeded properly!
4 * @param random Function taking the number of bytes to generate as its sole argument, returning the corresponding array of cryptographically secure random byte values.
5 */
6export declare function setRandomFallback(random: (random: number) => number[]): void;
7
8/**
9 * Synchronously generates a salt.
10 * @param rounds Number of rounds to use, defaults to 10 if omitted
11 * @return Resulting salt
12 * @throws If a random fallback is required but not set
13 */
14export declare function genSaltSync(rounds?: number): string;
15
16/**
17 * Asynchronously generates a salt.
18 * @param rounds Number of rounds to use, defaults to 10 if omitted
19 * @return Promise with resulting salt, if callback has been omitted
20 */
21export declare function genSalt(rounds?: number): Promise<string>;
22
23/**
24 * Asynchronously generates a salt.
25 * @param callback Callback receiving the error, if any, and the resulting salt
26 */
27export declare function genSalt(callback: (err: Error | null, salt: string) => void): void;
28
29/**
30 * Asynchronously generates a salt.
31 * @param rounds Number of rounds to use, defaults to 10 if omitted
32 * @param callback Callback receiving the error, if any, and the resulting salt
33 */
34export declare function genSalt(rounds: number, callback: (err: Error | null, salt: string) => void): void;
35
36/**
37 * Synchronously generates a hash for the given string.
38 * @param s String to hash
39 * @param salt Salt length to generate or salt to use, default to 10
40 * @return Resulting hash
41 */
42export declare function hashSync(s: string, salt?: number | string): string;
43
44/**
45 * Asynchronously generates a hash for the given string.
46 * @param s String to hash
47 * @param salt Salt length to generate or salt to use
48 * @return Promise with resulting hash, if callback has been omitted
49 */
50export declare function hash(s: string, salt: number | string): Promise<string>;
51
52/**
53 * Asynchronously generates a hash for the given string.
54 * @param s String to hash
55 * @param salt Salt length to generate or salt to use
56 * @param callback Callback receiving the error, if any, and the resulting hash
57 * @param progressCallback Callback successively called with the percentage of rounds completed (0.0 - 1.0), maximally once per MAX_EXECUTION_TIME = 100 ms.
58 */
59export declare function hash(
60 s: string,
61 salt: number | string,
62 callback?: (err: Error | null, hash: string) => void,
63 progressCallback?: (percent: number) => void,
64): void;
65
66/**
67 * Synchronously tests a string against a hash.
68 * @param s String to compare
69 * @param hash Hash to test against
70 * @return true if matching, otherwise false
71 */
72export declare function compareSync(s: string, hash: string): boolean;
73
74/**
75 * Asynchronously compares the given data against the given hash.
76 * @param s Data to compare
77 * @param hash Data to be compared to
78 * @return Promise, if callback has been omitted
79 */
80export declare function compare(s: string, hash: string): Promise<boolean>;
81
82/**
83 * Asynchronously compares the given data against the given hash.
84 * @param s Data to compare
85 * @param hash Data to be compared to
86 * @param callback Callback receiving the error, if any, otherwise the result
87 * @param progressCallback Callback successively called with the percentage of rounds completed (0.0 - 1.0), maximally once per MAX_EXECUTION_TIME = 100 ms.
88 */
89export declare function compare(
90 s: string,
91 hash: string,
92 callback?: (err: Error | null, success: boolean) => void,
93 progressCallback?: (percent: number) => void,
94): void;
95
96/**
97 * Gets the number of rounds used to encrypt the specified hash.
98 * @param hash Hash to extract the used number of rounds from
99 * @return Number of rounds used
100 */
101export declare function getRounds(hash: string): number;
102
103/**
104 * Gets the salt portion from a hash. Does not validate the hash.
105 * @param hash Hash to extract the salt from
106 * @return Extracted salt part
107 */
108export declare function getSalt(hash: string): string;
109
110/**
111 * Encodes a byte array to base64 with up to len bytes of input, using the custom bcrypt alphabet.
112 * @function
113 * @param b Byte array
114 * @param len Maximum input length
115 */
116export declare function encodeBase64(b: Readonly<ArrayLike<number>>, len: number): string;
117
118/**
119 * Decodes a base64 encoded string to up to len bytes of output, using the custom bcrypt alphabet.
120 * @function
121 * @param s String to decode
122 * @param len Maximum output length
123 */
124export declare function decodeBase64(s: string, len: number): number[];