Good Hash algorithms in nodeJS
Introduction
Hash algorithms are used to protect the password, card, other sensitive information
Following are the widely used
- MD5
- SHA-256
- Argon2
- PBKDF2
- scrypt
- bcrypt
MD5:
- MD5 stands for message digest
- Rainbow table problem exists
- Brute force and reverse output is not much more difficult
- Let’s create a hash with md5
const crypto = require('crypto'); let password = "password"; let encryptedPassword = crypto.createHash('md5').update(password).digest("hex"); console.log(encryptedPassword); // outputs encrypted string
SHA-256:
- SHA-256 is more secure than the MD5
- Let’s create a hash with SHA-256
- Has low chance of collisions
- Brute force and reverse output is more difficult than MD5
- Rainbow table problem exists
const crypto = require('crypto'); let password = "password"; let encryptedPassword = crypto.createHash('sha256').update(password).digest('hex'); console.log(encryptedPassword); // outputs encrypted string
argon2:
- Argon2d maximizes resistance to GPU cracking attacks
- Argon2i is optimized to resist side-channel attacks
- It accesses the memory array in a password dependent order, which reduces the possibility of a time-memory trade-off (TMTO)
- Let’s create a hash with argon2
// Create a hash const argon2 = require('argon2'); const getHash = async (password) => { try { const encryptedPassword = await argon2.hash(password); console.log(encryptedPassword); } catch (err) { } } let password = 'password'; getHash(password); // Outputs hash string
PBKDF2:
- PBKDF2 is part of RSA Laboratories’ Public-Key Cryptography Standards (PKCS) series
- PBKDF2 applies a pseudorandom function
- Adds salt value
- Repeats the process many times to produce a derived key
- The derived key can then be used as a cryptographic key in subsequent operations
- The added computational work makes password cracking much more difficult, and is known as key stretching
- Syntax: DK = PBKDF2(PRF, Password, Salt, c, dkLen)
- PRF is a pseudorandom function of two parameters with output length hLen (e.g., a keyed HMAC)
- Password is the master password from which a derived key is generated
- Salt is a sequence of bits, known as a cryptographic salt
- c is the number of iterations desired
- dkLen is the desired bit-length of the derived key
- DK is the generated derived key
const pbkdf2 = require('pbkdf2'); const derivedKey = pbkdf2.pbkdf2Sync('password', 'salt', 1, 32, 'sha512'); const encryptedPassword = derivedKey.toString('hex'); console.log(encryptedPassword); // Outputs hash string
scrypt:
- Scrypt is an advanced crypto library used mainly for key derivation
- It is intended to be costly computationally plus memory-wise
- Brute-force attacks are made unsuccessful
- Syntax: crypto.scrypt( password, salt, keylen, options, callback )
- Adds salt value
var crypto = require('crypto'); crypto.scrypt('nodejs', 'scrypt', 32, (err, derivedKey) => { if (err) throw err; const encryptedPassword = derivedKey.toString('hex'); console.log(encryptedPassword); }); // Outputs hash string
bcrypt:
- Adds salt value
- Brute-force attacks are made unsuccessful
const bcrypt = require('bcrypt'); const saltRounds = 20; const password = 'password'; bcrypt.genSalt(saltRounds, function (err, salt) { bcrypt.hash(password, salt, function (err, hash) { console.log(hash) }); }); // Outputs hash string