Skip to content

Commit 230dcd4

Browse files
committed
Add bcryptjs definitions and tests
1 parent 1132252 commit 230dcd4

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

bcryptjs/bcryptjs-tests.ts

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/// <reference path="./bcryptjs.d.ts"/>
2+
3+
import bcryptjs = require("bcryptjs");
4+
5+
let str: string;
6+
let num: number;
7+
let bool: boolean;
8+
9+
str = bcryptjs.genSaltSync();
10+
str = bcryptjs.genSaltSync(10);
11+
12+
bcryptjs.genSalt((err: Error, salt: string) => {
13+
str = salt;
14+
});
15+
bcryptjs.genSalt(10, (err: Error, salt: string) => {
16+
str = salt;
17+
});
18+
19+
str = bcryptjs.hashSync("string");
20+
str = bcryptjs.hashSync("string", 10);
21+
str = bcryptjs.hashSync("string", "salt");
22+
23+
bcryptjs.hash("string", 10, (err: Error, hash: string) => {
24+
str = hash;
25+
});
26+
bcryptjs.hash("string", 10, (err: Error, hash: string) => {
27+
str = hash;
28+
}, (percent: number) => {
29+
num = percent;
30+
});
31+
32+
bcryptjs.hash("string", "salt", (err: Error, hash: string) => {
33+
str = hash;
34+
});
35+
bcryptjs.hash("string", "salt", (err: Error, hash: string) => {
36+
str = hash;
37+
}, (percent: number) => {
38+
num = percent;
39+
});
40+
41+
bool = bcryptjs.compareSync("string1", "string2");
42+
43+
bcryptjs.compare("string1", "string2", (err: Error, success: boolean) => {
44+
bool = success;
45+
});
46+
bcryptjs.compare("string1", "string2", (err: Error, success: boolean) => {
47+
bool = success;
48+
}, (percent: number) => {
49+
num = percent;
50+
});
51+
52+
num = bcryptjs.getRounds("string");
53+
54+
str = bcryptjs.getSalt("string");

bcryptjs/bcryptjs.d.ts

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Type definitions for bcryptjs v2.3.0
2+
// Project: https://github.com/dcodeIO/bcrypt.js
3+
// Definitions by: Joshua Filby <https://github.com/Joshua-F/>
4+
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5+
6+
declare module "bcryptjs" {
7+
8+
/**
9+
* Sets the pseudo random number generator to use as a fallback if neither node's crypto module nor the Web Crypto API is available.
10+
* Please note: It is highly important that the PRNG used is cryptographically secure and that it is seeded properly!
11+
* @param random Function taking the number of bytes to generate as its sole argument, returning the corresponding array of cryptographically secure random byte values.
12+
*/
13+
export function setRandomFallback(random: (random: number) => number[]): void;
14+
15+
/**
16+
* Synchronously generates a salt.
17+
* @param rounds Number of rounds to use, defaults to 10 if omitted
18+
* @return Resulting salt
19+
*/
20+
export function genSaltSync(rounds?: number): string;
21+
22+
/**
23+
* Asynchronously generates a salt.
24+
* @param callback Callback receiving the error, if any, and the resulting salt
25+
*/
26+
export function genSalt(callback: (err: Error, salt: string) => void): void;
27+
28+
/**
29+
* Asynchronously generates a salt.
30+
* @param rounds Number of rounds to use, defaults to 10 if omitted
31+
* @param callback Callback receiving the error, if any, and the resulting salt
32+
*/
33+
export function genSalt(rounds: number, callback: (err: Error, salt: string) => void): void;
34+
35+
/**
36+
* Synchronously generates a hash for the given string.
37+
* @param s String to hash
38+
* @param salt Salt length to generate or salt to use, default to 10
39+
* @return Resulting hash
40+
*/
41+
export function hashSync(s: string, salt?: number | string): string;
42+
43+
/**
44+
* Asynchronously generates a hash for the given string.
45+
* @param s String to hash
46+
* @param salt Salt length to generate or salt to use
47+
* @param callback Callback receiving the error, if any, and the resulting hash
48+
* @param progressCallback Callback successively called with the percentage of rounds completed (0.0 - 1.0), maximally once per MAX_EXECUTION_TIME = 100 ms.
49+
*/
50+
export function hash(s: string, salt: number | string, callback: (err: Error, hash: string) => void, progressCallback?: (percent: number) => void): void;
51+
52+
/**
53+
* Synchronously tests a string against a hash.
54+
* @param s String to compare
55+
* @param hash Hash to test against
56+
* @return true if matching, otherwise false
57+
*/
58+
export function compareSync(s: string, hash: string): boolean;
59+
60+
/**
61+
* Asynchronously compares the given data against the given hash.
62+
* @param s Data to compare
63+
* @param hash Data to be compared to
64+
* @param callback Callback receiving the error, if any, otherwise the result
65+
* @param progressCallback Callback successively called with the percentage of rounds completed (0.0 - 1.0), maximally once per MAX_EXECUTION_TIME = 100 ms.
66+
*/
67+
export function compare(s: string, hash: string, callback: (err: Error, success: boolean) => void, progressCallback?: (percent: number) => void): void;
68+
69+
/**
70+
* Gets the number of rounds used to encrypt the specified hash.
71+
* @param hash Hash to extract the used number of rounds from
72+
* @return Number of rounds used
73+
*/
74+
export function getRounds(hash: string): number;
75+
76+
/**
77+
* Gets the salt portion from a hash. Does not validate the hash.
78+
* @param hash Hash to extract the salt from
79+
* @return Extracted salt part
80+
*/
81+
export function getSalt(hash: string): string;
82+
}

0 commit comments

Comments
 (0)