Password Security: Hashing, Salts, Encryption, and Sensitive Data

Store verifiable password records and distinguish hashing from reversible encryption

🎯 Introduction

By the end of this article, you will be able to store password hashes and verify login passwords without retaining the original password.

🔐 What to store at registration

A password hash is a verification record derived from a password. At login, it lets you check whether the input matches. Store this record in the database, never the original password.

Use the Argon2 password hashing library to handle random salts and hash computation. Install Argon2:

npm install argon2@0.45.1

Create password-demo.mjs:

password-demo.mjs
import * as argon2 from 'argon2';

const passwordHash = await argon2.hash('demo-password-only');
console.log(passwordHash.startsWith('$argon2id$'));

Running node password-demo.mjs prints true. passwordHash is a complete string containing the algorithm, computation parameters, salt, and result. At registration, store the entire string in the database's password hash field. Do not split it up or return it to the frontend.


🧂 Why salts produce different records for the same password

A salt is random data added during hashing. Different salts produce different hash records for the same password, reducing an attacker's ability to reuse one set of guesses.

The library generates salts automatically. Continue with:

password-demo.mjs
const anotherHash = await argon2.hash('demo-password-only');
console.log(passwordHash === anotherHash);

The output is false. Both records verify the original password despite being different, so do not hash the input again and compare strings at login.


✅ Verify login input

At login, retrieve passwordHash by account from the database, then pass the submitted password to verify. Test the record generated above:

password-demo.mjs
const matched = await argon2.verify(passwordHash, 'demo-password-only');
console.log(matched);

The output is true. verify reads the original salt and parameters from the stored record and performs the comparison. Create a login session only after true. For incorrect input:

password-demo.mjs
console.log(await argon2.verify(passwordHash, 'wrong-password'));

The output is false. The endpoint should return a generic “Invalid username or password” message. Corrupt records or computation failures may throw; endpoint error handling should log the internal cause rather than treating it as successful login.


🛡 Limit input and login attempts too

Password hashing deliberately consumes time and memory to make large-scale guessing expensive. Limit password input length and login request frequency to prevent excessive concurrent computation.

Do not silently trim or truncate passwords: whitespace may be intentional. Passwords, password hashes, and login tokens must not appear in ordinary request logs.


🗝 Which data needs encryption rather than hashing?

If the server must recover the original content, such as a key used to access a third-party service on a user's behalf, use reversible encrypted storage. Encryption lets a key holder recover the data. Password login only needs a match check, so it uses hashing.

Store encryption keys separately from the application database and restrict access. When users forget passwords, provide a reset flow for choosing a new password rather than sending the old one.


❓ Questions and Answers

Q1: Must the salt be kept secret separately?

No. The library includes it in the hash record. Protect the database nonetheless, since leaked records allow offline password guessing.

Q2: Can the backend directly store a password hashed by the frontend?

That does not replace server-side password storage. The backend must still process the received login secret with a password hashing library and transmit it over HTTPS.

Q3: Can the old hash remain after a password change?

Generate a new hash from the new password, replace the old record, and revoke old sessions according to the login policy.


🧪 Practice

Verify that anotherHash also accepts the original password, then confirm that both records return false for an incorrect password.

// Write your code here