Bcrypt
Class for hashing passwords.
note
Class should be used in backend.
Import
- ES5
- ES6
const { Bcrypt } = require("js-ts-kit");
import { Bcrypt } from "js-ts-kit";
Hashing Password
Code
import { Bcrypt } from "js-ts-kit";
import bcrypt from "bcrypt";
async function main() {
const myBcrypt = new Bcrypt(bcrypt);
await myBcrypt.hashPassword("test", 10);
}
Output
$2a$10$a8raagrcuMpJmP53OAFDdu.I7UpwcZLLWGsKFtQx4uOxFPly5nhrW;
Comparing Hashed & Unhashed Passwords
Code
import { Bcrypt } from "js-ts-kit";
import bcrypt from "bcrypt";
async function main() {
const myBcrypt = new Bcrypt(bcrypt);
await myBcrypt.isMatchingPassword(
"$2a$10$a8raagrcuMpJmP53OAFDdu.I7UpwcZLLWGsKFtQx4uOxFPly5nhrW",
"test",
);
}
Output
true;
Reference
note
Check out Bcrypt-Generator.com if you need online tool for hashing password or checking hashed password
Create Object / Object Properties
new Bcrypt(bcrypt)
| Name | Type | Static | Description |
|---|---|---|---|
bcrypt | no | instance from importing bcrypt package |
Methods
static hashPassword(passwordToBeHashed, rounds)
Parameters
| Name | Type | Default Value | Description |
|---|---|---|---|
passwordToBeHashed | string | password that has not been hashed | |
rounds | number | 10 | cost of processing data |
Returns
-
Type:
Promise<string> -
Description: hashed password
static isMatchingPassword(hashedPassword, unhashedPassword)
Parameters
| Name | Type | Description |
|---|---|---|
hashedPassword | string | hashed password |
unhashedPassword | string | password that has not been hashed |
Returns
-
Type:
Promise<boolean> -
Description:
trueif unhashed password can be hashed to hashed password. Otherwise,false.