Skip to main content

Bcrypt

Class for hashing passwords.

note

Class should be used in backend.

Import

const { Bcrypt } = require("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)

NameTypeStaticDescription
bcryptnoinstance from importing bcrypt package

Methods

static hashPassword(passwordToBeHashed, rounds)

Parameters
NameTypeDefault ValueDescription
passwordToBeHashedstringpassword that has not been hashed
roundsnumber10cost of processing data
Returns
  • Type: Promise<string>

  • Description: hashed password

static isMatchingPassword(hashedPassword, unhashedPassword)

Parameters
NameTypeDescription
hashedPasswordstringhashed password
unhashedPasswordstringpassword that has not been hashed
Returns
  • Type: Promise<boolean>

  • Description: true if unhashed password can be hashed to hashed password. Otherwise, false.