Bcryptjs
Basic start up
https://www.npmjs.com/package/bcryptjs
Terminal
npm install bcryptjs
npm install --save @types/bcryptjs
Hash and compare
Basically, we can use the bcrypt
to hash the plain password to hashed value.
hashed.ts
import { bcrypt } from 'bcryptjs';
const salt = 12; // Default is 10
// Hash data
const hash: string = bcrypt.hashSync("data_that_want_to_hash", salt);
// Compart hashed and incoming data
const isMatch: boolean = bcrypt.compareSync("incoming_data", hash);
For password checking
Basically, we can use the bcrypt
to hash the plain password to hashed value.
hashed.ts
import { bcrypt } from 'bcryptjs';
const salt = 12;
function hashPlainPassword(plainPassword: string): string{
return bcrypt.hashSync(plainPassword, salt);
}
function checkPasswordHash(userInputPlainPassword: string, hashedValue: string): boolean{
return bcrypt.compareSync(userInputPlainPassword, hash);
}