-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
44 lines (40 loc) · 1.19 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { genSalt, hash, compare } from "bcrypt";
const stringToHash = (
PasswordString: string,
saltRounds = 10
): Promise<string> => {
return new Promise(async (resolve, reject) => {
try {
const salt = await genSalt(saltRounds);
const hashedPassword = await hash(PasswordString, salt);
resolve(hashedPassword);
} catch (err) {
reject(err);
}
});
};
const verifyHash = (
realPassword: string,
hashString: string
): Promise<boolean> => {
return new Promise(async (resolve, reject) => {
try {
const result = await compare(realPassword, hashString);
resolve(result); //return with boolean 'Hash' is matched or not
} catch (err) {
reject(false); //it means hash is invalid
}
});
};
const validateHash = (hashString: string): Promise<boolean> => {
//true or false in resolve, no reject
return new Promise(async (resolve, reject) => {
try {
const result = await compare("dummy", hashString);
resolve(result); //return with boolean 'Hash' is matched or not
} catch (err) {
reject(false); //it means hash is invalid
}
});
};
export { stringToHash, verifyHash, verifyHash as varifyHash, validateHash };