Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials Node.js Password Hashing
Node.js Intermediate FREE

Password Hashing

Lesson 2 of 17 Intermediate Interactive

Never store plain text passwords. Use bcrypt for hashing.

Syntax

NODEJS
const bcrypt = require('bcrypt');

const hash = await bcrypt.hash("password123", 10);
const match = await bcrypt.compare("password123", hash);
Password Hashing with bcrypt
NODE
const bcrypt = require("bcrypt");

async function hashPassword(password) {
    const salt = await bcrypt.genSalt(10);
    const hash = await bcrypt.hash(password, salt);
    console.log("Hash:", hash);
    return hash;
}

async function verifyPassword(password, hash) {
    const match = await bcrypt.compare(password, hash);
    console.log("Match:", match);
    return match;
}

// Usage
hashPassword("mySecret123").then(hash => {
    verifyPassword("mySecret123", hash);
    verifyPassword("wrongPassword", hash);
});