Common Hash Functions
- SHA-256
- SHA-3
- BLAKE2
- Argon2 (Password Hashing)
Password Hashing Implementation
// Node.js - Secure Password Hashing
const crypto = require('crypto');
class PasswordHash {
static async hash(password) {
return new Promise((resolve, reject) => {
// Generate random salt
const salt = crypto.randomBytes(16);
crypto.scrypt(password, salt, 64, (err, derivedKey) => {
if (err) reject(err);
resolve(salt.toString('hex') + ':' + derivedKey.toString('hex'));
});
});
}
static async verify(password, hash) {
return new Promise((resolve, reject) => {
const [salt, key] = hash.split(':');
const keyBuffer = Buffer.from(key, 'hex');
crypto.scrypt(password, Buffer.from(salt, 'hex'), 64, (err, derivedKey) => {
if (err) reject(err);
resolve(crypto.timingSafeEqual(keyBuffer, derivedKey));
});
});
}
}