UNPKG

6.76 kBTypeScriptView Raw
1/// <reference types="node" />
2
3/**
4 * @param rounds The cost of processing the data. Default 10.
5 * @param minor The minor version of bcrypt to use. Either 'a' or 'b'. Default 'b'.
6 *
7 * @example
8 * import * as bcrypt from 'bcrypt';
9 * const saltRounds = 10;
10 *
11 * const salt = bcrypt.genSaltSync(saltRounds);
12 */
13export declare function genSaltSync(rounds?: number, minor?: "a" | "b"): string;
14
15/**
16 * @param rounds The cost of processing the data. Default 10.
17 * @param minor The minor version of bcrypt to use. Either 'a' or 'b'. Default 'b'.
18 * @return A promise to be either resolved with the generated salt or rejected with an Error
19 *
20 * @example
21 * import * as bcrypt from 'bcrypt';
22 * const saltRounds = 10;
23 *
24 * (async () => {
25 * const salt = await bcrypt.genSalt(saltRounds);
26 * })();
27 */
28export declare function genSalt(rounds?: number, minor?: "a" | "b"): Promise<string>;
29
30/**
31 * @param rounds The cost of processing the data. Default 10.
32 * @param minor The minor version of bcrypt to use. Either 'a' or 'b'. Default 'b'.
33 * @param callback A callback to be fire once the salt has been generated. Uses eio making it asynchronous.
34 *
35 * @example
36 * import * as bcrypt from 'bcrypt';
37 * const saltRounds = 10;
38 *
39 * // Technique 1 (generate a salt and hash on separate function calls):
40 * bcrypt.genSalt(saltRounds, (err, salt) => {
41 * // ...
42 * });
43 */
44export declare function genSalt(callback: (err: Error | undefined, salt: string) => any): void;
45export declare function genSalt(rounds: number, callback: (err: Error | undefined, salt: string) => any): void;
46export declare function genSalt(
47 rounds: number,
48 minor: "a" | "b",
49 callback: (err: Error | undefined, salt: string) => any,
50): void;
51
52/**
53 * @param data The data to be encrypted.
54 * @param saltOrRounds The salt to be used to hash the password. If specified as a number then a
55 * salt will be generated with the specified number of rounds and used.
56 *
57 * @example
58 * import * as bcrypt from 'bcrypt';
59 * const saltRounds = 10;
60 * const myPlaintextPassword = 's0/\/\P4$$w0rD';
61 *
62 * // Technique 1 (generate a salt and hash on separate function calls):
63 * const salt = bcrypt.genSaltSync(saltRounds);
64 * const hash = bcrypt.hashSync(myPlaintextPassword, salt);
65 * // Store hash in your password DB.
66 *
67 * // Technique 2 (auto-gen a salt and hash):
68 * const hash2 = bcrypt.hashSync(myPlaintextPassword, saltRounds);
69 * // Store hash in your password DB.
70 */
71export declare function hashSync(data: string | Buffer, saltOrRounds: string | number): string;
72
73/**
74 * @param data The data to be encrypted.
75 * @param saltOrRounds The salt to be used in encryption. If specified as a number then a
76 * salt will be generated with the specified number of rounds and used.
77 * @return A promise to be either resolved with the encrypted data salt or rejected with an Error
78 *
79 * @example
80 * import * as bcrypt from 'bcrypt';
81 * const saltRounds = 10;
82 * const myPlaintextPassword = 's0/\/\P4$$w0rD';
83 *
84 * (async () => {
85 * // Technique 1 (generate a salt and hash on separate function calls):
86 * const salt = await bcrypt.genSalt(saltRounds);
87 * const hash = await bcrypt.hash(myPlaintextPassword, salt);
88 * // Store hash in your password DB.
89 *
90 * // Technique 2 (auto-gen a salt and hash):
91 * const hash2 = await bcrypt.hash(myPlaintextPassword, saltRounds);
92 * // Store hash in your password DB.
93 * })();
94 */
95export declare function hash(data: string | Buffer, saltOrRounds: string | number): Promise<string>;
96
97/**
98 * @param data The data to be encrypted.
99 * @param saltOrRounds The salt to be used in encryption. If specified as a number then a
100 * salt will be generated with the specified number of rounds and used.
101 * @param callback A callback to be fired once the data has been encrypted. Uses eio making it asynchronous.
102 *
103 * @example
104 * import * as bcrypt from 'bcrypt';
105 * const saltRounds = 10;
106 * const myPlaintextPassword = 's0/\/\P4$$w0rD';
107 *
108 * // Technique 1 (generate a salt and hash on separate function calls):
109 * bcrypt.genSalt(saltRounds, (err, salt) => {
110 * bcrypt.hash(myPlaintextPassword, salt, (err, hash) => {
111 * // Store hash in your password DB.
112 * });
113 * });
114 *
115 * // Technique 2 (auto-gen a salt and hash):
116 * bcrypt.hash(myPlaintextPassword, saltRounds, (err, hash) => {
117 * // Store hash in your password DB.
118 * });
119 */
120export declare function hash(
121 data: string | Buffer,
122 saltOrRounds: string | number,
123 callback: (err: Error | undefined, encrypted: string) => any,
124): void;
125
126/**
127 * @param data The data to be encrypted.
128 * @param encrypted The data to be compared against.
129 *
130 * @example
131 * import * as bcrypt from 'bcrypt';
132 * const myPlaintextPassword = 's0/\/\P4$$w0rD';
133 * const someOtherPlaintextPassword = 'not_bacon';
134 *
135 * // Load hash from your password DB.
136 * bcrypt.compareSync(myPlaintextPassword, hash); // true
137 * bcrypt.compareSync(someOtherPlaintextPassword, hash); // false
138 */
139export declare function compareSync(data: string | Buffer, encrypted: string): boolean;
140
141/**
142 * @param data The data to be encrypted.
143 * @param encrypted The data to be compared against.
144 * @return A promise to be either resolved with the comparison result salt or rejected with an Error
145 *
146 * @example
147 * import * as bcrypt from 'bcrypt';
148 * const myPlaintextPassword = 's0/\/\P4$$w0rD';
149 * const someOtherPlaintextPassword = 'not_bacon';
150 *
151 * (async () => {
152 * // Load hash from your password DB.
153 * const result1 = await bcrypt.compare(myPlaintextPassword, hash);
154 * // result1 == true
155 *
156 * const result2 = await bcrypt.compare(someOtherPlaintextPassword, hash);
157 * // result2 == false
158 * })();
159 */
160export declare function compare(data: string | Buffer, encrypted: string): Promise<boolean>;
161
162/**
163 * @param data The data to be encrypted.
164 * @param encrypted The data to be compared against.
165 * @param callback A callback to be fire once the data has been compared. Uses eio making it asynchronous.
166 *
167 * @example
168 * import * as bcrypt from 'bcrypt';
169 * const myPlaintextPassword = 's0/\/\P4$$w0rD';
170 * const someOtherPlaintextPassword = 'not_bacon';
171 *
172 * // Load hash from your password DB.
173 * bcrypt.compare(myPlaintextPassword, hash, (err, result) => {
174 * // result == true
175 * });
176 * bcrypt.compare(someOtherPlaintextPassword, hash, (err, result) => {
177 * // result == false
178 * });
179 */
180export declare function compare(
181 data: string | Buffer,
182 encrypted: string,
183 callback: (err: Error | undefined, same: boolean) => any,
184): void;
185
186/**
187 * @param encrypted Hash from which the number of rounds used should be extracted.
188 * @returns The number of rounds used to encrypt a given hash.
189 */
190export declare function getRounds(encrypted: string): number;