UNPKG

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