how to salt a password in javascript

To salt a password in JavaScript, you can use a library called bcryptjs. Here's an example:

index.tsx
const bcrypt = require('bcryptjs');

const password = 'my-password';
const saltRounds = 10;

bcrypt.genSalt(saltRounds, function(err, salt) {
  bcrypt.hash(password, salt, function(err, hash) {
    // Store hash in your password DB.
  });
});
243 chars
11 lines

In this example, we generate a salt with bcrypt.genSalt(), using a number of rounds passed as a parameter. Then, we use the generated salt to hash the password with bcrypt.hash(), and store the resulting hash in our database.

Salting and hashing passwords is a crucial step in securing user authentication, as it makes it much more difficult for attackers to crack user passwords using methods such as rainbow tables.

gistlibby LogSnag