ReasonJun

bcryptjs (encryption method) 본문

Frontend/Library

bcryptjs (encryption method)

ReasonJun 2023. 7. 10. 17:49
728x90

bcryptjs is a library used for hashing passwords in JavaScript. It provides a way to securely hash passwords and compare them with hashed values to verify if a password is correct. The library is a JavaScript implementation of the bcrypt algorithm, which is a widely used and respected password-hashing algorithm.

Here are some key features and usage details of bcryptjs:

  • Hashing passwords: You can use bcryptjs to hash passwords by calling the hash function. It takes the plain text password and a salt value (which can be generated using genSaltSync or genSalt) and returns the hashed password.
const bcrypt = require('bcryptjs');

const plainPassword = 'myPassword';
const salt = bcrypt.genSaltSync(10);
const hashedPassword = bcrypt.hashSync(plainPassword, salt);
  • Comparing passwords: You can compare a plain text password with a hashed password to determine if they match using the compare function. It takes the plain text password and the hashed password and returns a boolean indicating whether they match.
const bcrypt = require('bcryptjs');

const plainPassword = 'myPassword';
const hashedPassword = '$2a$10$Rau3qZKm4bJF52k.8roaoOPO4v./6h4noNNU29N/Vt.g1.2KpRqq2';

const isMatch = bcrypt.compareSync(plainPassword, hashedPassword);
console.log(isMatch); // true
  • Generating salt: genSalt or genSaltSync functions are used to generate a salt value. The salt is a random string used to further strengthen the password hashing process.
const bcrypt = require('bcryptjs');

// Asynchronous version
bcrypt.genSalt(10, (err, salt) => {
  // Use the salt value
});

// Synchronous version
const salt = bcrypt.genSaltSync(10);

Note: The cost factor (e.g., 10 in the examples) determines the computational cost of the hashing process and should be chosen based on the desired security level and system performance.

bcryptjs is commonly used in Node.js and JavaScript applications for securely storing and verifying passwords. It is recommended over simpler hashing functions like MD5 or SHA256 because bcrypt employs techniques such as salting and multiple rounds of hashing to protect against brute-force attacks and rainbow table attacks.

 

https://www.npmjs.com/package/bcryptjs

 

bcryptjs

Optimized bcrypt in plain JavaScript with zero dependencies. Compatible to 'bcrypt'.. Latest version: 2.4.3, last published: 6 years ago. Start using bcryptjs in your project by running `npm i bcryptjs`. There are 3145 other projects in the npm registry us

www.npmjs.com

 

728x90

'Frontend > Library' 카테고리의 다른 글

Framer Motion  (0) 2023.07.10
Cloudinary  (0) 2023.07.10
PostCSS & AutoFixer  (0) 2023.06.29
react-hook-form (useForm())  (0) 2023.06.23
Web Vitals : LCP / FID / CLS & Measure Performance / Lighthouse  (0) 2023.06.21
Comments