Get your own Node server
const crypto = require('crypto');

// Function to hash a password with a salt
function hashPassword(password, salt) {
  // Create hash object
  const hash = crypto.createHash('sha256');
  
  // Update with salt and password
  hash.update(salt);
  hash.update(password);
  
  // Return digest
  return hash.digest('hex');
}

// Generate a random salt
function generateSalt() {
  return crypto.randomBytes(16).toString('hex');
}

// Example usage
const password = 'mySecurePassword123';

// For a new user, generate a salt and hash the password
const salt = generateSalt();
const hashedPassword = hashPassword(password, salt);

console.log('Password:', password);
console.log('Salt:', salt);
console.log('Hashed Password:', hashedPassword);

// To verify a password, hash it with the same salt and compare
function verifyPassword(password, salt, storedHash) {
  const hash = hashPassword(password, salt);
  return hash === storedHash;
}

// Check correct password
console.log('Verification with correct password:', 
  verifyPassword(password, salt, hashedPassword));

// Check incorrect password
console.log('Verification with incorrect password:', 
  verifyPassword('wrongPassword', salt, hashedPassword));

// Note: For production, use crypto.pbkdf2, bcrypt, scrypt, or Argon2 instead

              
Password: mySecurePassword123
Salt: 1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7
Hashed Password: 5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a2b31a2b3c4d
Verification with correct password: true
Verification with incorrect password: false