Skip to content

Commit 58333a1

Browse files
committed
Update types
1 parent 2e3b176 commit 58333a1

File tree

2 files changed

+37
-46
lines changed

2 files changed

+37
-46
lines changed

README.md

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,43 +127,54 @@ Usage: bcrypt <input> [rounds|salt]
127127

128128
### Callback types
129129

130-
- type **Callback<`T`>**: `(err: Error | null, result?: T) => void`
130+
- **Callback<`T`>**: `(err: Error | null, result?: T) => void`<br />
131+
Called with an error on failure or a value of type `T` upon success.
131132

132-
- type **ProgressCallback**: `(percentage: number) => void`
133+
- **ProgressCallback**: `(percentage: number) => void`<br />
134+
Called with the percentage of rounds completed (0.0 - 1.0), maximally once per `MAX_EXECUTION_TIME = 100` ms.
133135

134-
### Functions
136+
- **RandomFallback**: `(length: number) => number[]`<br />
137+
Called to obtain random bytes when both [Web Crypto API](http://www.w3.org/TR/WebCryptoAPI/) and Node.js
138+
[crypto](http://nodejs.org/api/crypto.html) are not available.
135139

136-
- bcrypt.**setRandomFallback**(random: `(length: number) => number[]`): `void`<br />
137-
Sets the pseudo random number generator to use as a fallback if neither Node's [crypto](http://nodejs.org/api/crypto.html) module nor the [Web Crypto API](http://www.w3.org/TR/WebCryptoAPI/) is available. Please note: It is highly important that the PRNG used is cryptographically secure and that it is seeded properly!
140+
### Functions
138141

139142
- bcrypt.**genSaltSync**(rounds?: `number`): `string`<br />
140143
Synchronously generates a salt. Number of rounds defaults to 10 when omitted.
141144

142145
- bcrypt.**genSalt**(rounds?: `number`): `Promise<string>`<br />
143-
bcrypt.**genSalt**(rounds: `number`, callback: `Callback<string>`): `void`<br />
144-
bcrypt.**genSalt**(callback: `Callback<string>`): `void`<br />
146+
Asynchronously generates a salt. Number of rounds defaults to 10 when omitted.
147+
148+
- bcrypt.**genSalt**([rounds: `number`, ]callback: `Callback<string>`): `void`<br />
145149
Asynchronously generates a salt. Number of rounds defaults to 10 when omitted.
146150

147151
- bcrypt.**hashSync**(s: `string`, salt?: `number | string`): `string`
148152
Synchronously generates a hash for the given string. Number of rounds defaults to 10 when omitted.
149153

150154
- bcrypt.**hash**(s: `string`, salt: `number | string`): `Promise<string>`<br />
151-
bcrypt.**hash**(s: `string`, salt: `number | string`, callback: `Callback<string>`, progressCallback?: `ProgressCallback`): `void`<br />
152-
Asynchronously generates a hash for the given string. Optionally calls a progress callback with the percentage of rounds completed (0.0 - 1.0), maximally once per `MAX_EXECUTION_TIME = 100` ms.
155+
Asynchronously generates a hash for the given string.
156+
157+
- bcrypt.**hash**(s: `string`, salt: `number | string`, callback: `Callback<string>`, progressCallback?: `ProgressCallback`): `void`<br />
158+
Asynchronously generates a hash for the given string.
153159

154160
- bcrypt.**compareSync**(s: `string`, hash: `string`): `boolean`<br />
155161
Synchronously tests a string against a hash.
156162

157163
- bcrypt.**compare**(s: `string`, hash: `string`): `Promise<boolean>`<br />
158-
bcrypt.**compare**(s: `string`, hash: `string`, callback: `Callback<boolean>`, progressCallback?: `ProgressCallback`)<br />
159-
Asynchronously compares a string against a hash. Optionally calls a progress callback with the percentage of rounds completed (0.0 - 1.0), maximally once per `MAX_EXECUTION_TIME = 100` ms.
164+
Asynchronously compares a string against a hash.
165+
166+
- bcrypt.**compare**(s: `string`, hash: `string`, callback: `Callback<boolean>`, progressCallback?: `ProgressCallback`)<br />
167+
Asynchronously compares a string against a hash.
160168

161169
- bcrypt.**getRounds**(hash: `string`): `number`<br />
162170
Gets the number of rounds used to encrypt the specified hash.
163171

164172
- bcrypt.**getSalt**(hash: `string`): `string`<br />
165173
Gets the salt portion from a hash. Does not validate the hash.
166174

175+
- bcrypt.**setRandomFallback**(random: `RandomFallback`): `void`<br />
176+
Sets the pseudo random number generator to use as a fallback if neither [Web Crypto API](http://www.w3.org/TR/WebCryptoAPI/) nor Node.js [crypto](http://nodejs.org/api/crypto.html) are available. Please note: It is highly important that the PRNG used is cryptographically secure and that it is seeded properly!
177+
167178
## Building
168179

169180
Building the UMD fallback:

types.d.ts

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,19 @@
1-
// imported from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/bcryptjs/index.d.ts
1+
// Originally imported from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/8b36dbdf95b624b8a7cd7f8416f06c15d274f9e6/types/bcryptjs/index.d.ts
2+
// MIT license.
23

3-
/*
4-
MIT License
5-
6-
Copyright (c) Microsoft Corporation.
7-
8-
Permission is hereby granted, free of charge, to any person obtaining a copy
9-
of this software and associated documentation files (the "Software"), to deal
10-
in the Software without restriction, including without limitation the rights
11-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12-
copies of the Software, and to permit persons to whom the Software is
13-
furnished to do so, subject to the following conditions:
14-
15-
The above copyright notice and this permission notice shall be included in all
16-
copies or substantial portions of the Software.
17-
18-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24-
SOFTWARE
25-
*/
4+
/** Called with an error on failure or a value of type `T` upon success. */
5+
type Callback<T> = (err: Error | null, result?: T) => void;
6+
/** Called with the percentage of rounds completed (0.0 - 1.0), maximally once per `MAX_EXECUTION_TIME = 100` ms. */
7+
type ProgressCallback = (percentage: number) => void;
8+
/** Called to obtain random bytes when both Web Crypto API and Node.js crypto are not available. */
9+
type RandomFallback = (length: number) => number[];
2610

2711
/**
2812
* Sets the pseudo random number generator to use as a fallback if neither node's crypto module nor the Web Crypto API is available.
2913
* Please note: It is highly important that the PRNG used is cryptographically secure and that it is seeded properly!
3014
* @param random Function taking the number of bytes to generate as its sole argument, returning the corresponding array of cryptographically secure random byte values.
3115
*/
32-
export declare function setRandomFallback(
33-
random: (random: number) => number[],
34-
): void;
16+
export declare function setRandomFallback(random: RandomFallback): void;
3517

3618
/**
3719
* Synchronously generates a salt.
@@ -52,9 +34,7 @@ export declare function genSalt(rounds?: number): Promise<string>;
5234
* Asynchronously generates a salt.
5335
* @param callback Callback receiving the error, if any, and the resulting salt
5436
*/
55-
export declare function genSalt(
56-
callback: (err: Error | null, salt: string) => void,
57-
): void;
37+
export declare function genSalt(callback: Callback<string>): void;
5838

5939
/**
6040
* Asynchronously generates a salt.
@@ -63,7 +43,7 @@ export declare function genSalt(
6343
*/
6444
export declare function genSalt(
6545
rounds: number,
66-
callback: (err: Error | null, salt: string) => void,
46+
callback: Callback<string>,
6747
): void;
6848

6949
/**
@@ -92,8 +72,8 @@ export declare function hash(s: string, salt: number | string): Promise<string>;
9272
export declare function hash(
9373
s: string,
9474
salt: number | string,
95-
callback?: (err: Error | null, hash: string) => void,
96-
progressCallback?: (percent: number) => void,
75+
callback?: Callback<string>,
76+
progressCallback?: ProgressCallback,
9777
): void;
9878

9979
/**
@@ -122,8 +102,8 @@ export declare function compare(s: string, hash: string): Promise<boolean>;
122102
export declare function compare(
123103
s: string,
124104
hash: string,
125-
callback?: (err: Error | null, success: boolean) => void,
126-
progressCallback?: (percent: number) => void,
105+
callback?: Callback<boolean>,
106+
progressCallback?: ProgressCallback,
127107
): void;
128108

129109
/**

0 commit comments

Comments
 (0)