Implementing Salt with Encryption in TypeScript
If two users have the same password and you hash without a salt, they produce identical hashes. An attacker who cracks one cracks both. That's why salts e...
8 May 2024 · 2 min read

If two users have the same password and you hash without a salt, they produce identical hashes. An attacker who cracks one cracks both. That's why salts exist.
A salt is a random string added to the input before hashing. Even identical passwords produce different hashes because each one uses a different salt.
How it works in TypeScript
import crypto from 'crypto';
const generateSalt = (length: number = 16): string => {
return crypto.randomBytes(Math.ceil(length / 2)).toString('hex').slice(0, length);
};
const encryptWithSalt = (data: string, salt: string): string => {
const hashedSalt = crypto.createHash('sha256').update(salt).digest('hex');
const cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(hashedSalt.slice(0, 32)), Buffer.alloc(16, 0));
let encryptedData = cipher.update(data, 'utf8', 'hex');
encryptedData += cipher.final('hex');
return encryptedData;
};
Using it:
const plaintextData = 'Sensitive information';
const salt = generateSalt();
const encryptedData = encryptWithSalt(plaintextData, salt);
console.log('Encrypted Data:', encryptedData);
console.log('Salt:', salt);
Store the salt alongside the hash
This is the part that confuses people. The salt isn't secret. Store it right next to the hashed password in your database. The salt's job isn't to be hidden — it's to make each hash unique so rainbow table attacks are useless.
The trade-off
Salted hashing protects against precomputed attacks (rainbow tables) and ensures identical inputs don't produce identical outputs. That's a big win.
But salting alone isn't enough. For password storage specifically, use a purpose-built algorithm like bcrypt, scrypt, or argon2. These algorithms are intentionally slow, which makes brute-force attacks expensive. A fast hash like SHA-256 with a salt is better than nothing, but it's not what you should reach for when storing user passwords.
Use crypto and manual salting for encryption and data integrity checks. Use bcrypt or argon2 for password hashing.
Ehsan Gazar helps software engineers take LLM and agent systems from demo to production. He writes about building LLM systems that hold up in production: evals, tracing, RAG, agent architecture and cost control. He teaches the Senior to Staff AI Engineer cohort on Maven, where engineers learn to design and defend these systems.
Forward Deployed AI Engineering: From Customer Problem to Production Value
A live Maven cohort for engineers: 10 live 90-minute sessions taking one AI engagement from customer problem to production, ending with eight artefacts and a working slice in your own repo.
January cohort starts Tue 12 Jan 2027, Tue & Thu 6:30pm London. Enrolling now, 20 seats, $1,800.
View the live cohortKeep reading
- TypeScript Anti-Patterns That Cost You Twice: Build Time, Runtime, and the Interview
- The Frontend Toolchain Is Now Written in Rust and Go. What That Means for You
- oxlint vs ESLint: Why I Replaced ESLint with oxlint
- SystemJS Is Dead. Native ESM Finally Won.
- Replace Axios with a Simple Custom Fetch Wrapper (Production-Ready Guide)
- Stop Shipping ChatGPT Wrappers. Ship an Agent in TypeScript, or Don't Bother.