PBKDF2
Password-Based Key Derivation Function 2
PBKDF2 (Password-Based Key Derivation Function 2) is a standard algorithm for securely deriving keys from passwords.
Table of Contents
Theory
PBKDF2 applies a Pseudorandom Function (typically HMAC-SHA256) to the password along with a salt value, and repeats this process many times (iterations).
Iterations: Repeated hashing forces an attacker to spend significantly more computing power to verify each password guess.
Salt: A random value added to the password. It prevents the use of "Rainbow Tables" (pre-computed hash databases) and ensures that two users with the same password have different hashes.
Module Methods
pbkdf2(password, salt, iterations, keylen, digest, callback)
Asynchronous key derivation.
Parameters:
Prop
Type
Returns: void
Example:
import { pbkdf2, randomBytes } from 'react-native-quick-crypto';
const pass = 'password123';
const salt = randomBytes(16);
pbkdf2(pass, salt, 600000, 64, 'sha512', (err, derivedKey) => {
if (err) throw err;
console.log(derivedKey.toString('hex'));
});pbkdf2Sync(password, salt, iterations, keylen, digest)
Synchronous version.
Returns: Buffer
Real-World Examples
User Registration
Securely hashing a user's password before storing it in a database.
import { pbkdf2, randomBytes } from 'react-native-quick-crypto';
function hashUserPassword(password: string): Promise<{ salt: string, hash: string }> {
return new Promise((resolve, reject) => {
const salt = randomBytes(16);
const iterations = 600000;
pbkdf2(password, salt, iterations, 64, 'sha512', (err, key) => {
if (err) return reject(err);
resolve({
salt: salt.toString('hex'),
hash: key.toString('hex')
});
});
});
}