UNPKG

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