Security
Encryption and hashing
Store passwords so a leaked database is not a disaster, and encrypt what you must read back.
Two different tools protect stored secrets, and mixing them up is a classic mistake:
| Hashing | Encryption | |
|---|---|---|
| reversible? | no, one way | yes, with the key |
| use for | passwords (you only need to check them) | data you must read again (an access token, an address) |
| the stored value | a hash | ciphertext |
Hashing passwords
Never store a password, and never store a fast hash of it (MD5, SHA-256). A database leak would let attackers try billions of guesses per second. A password hash is deliberately slow and memory-hungry, so each guess is expensive, and includes a random salt so identical passwords look different.
PasswordHasher offers three algorithms. The result is a self-describing string (it records the algorithm and settings), so any hasher verifies any hash: you can change the algorithm for new passwords and old ones keep working, then upgrade each one when the user logs in.
val hasher = PasswordHasher.argon2id() // or bcrypt(cost = 12), or pbkdf2()
val stored = hasher.hash("secret") // save this string in the user row
// at login
if (hasher.verify(password, user.hash)) {
if (hasher.needsRehash(user.hash)) users.save(user.copy(hash = hasher.hash(password))) // quietly move to the current settings
return signIn(user)
}| Algorithm | Factory | Notes |
|---|---|---|
| PBKDF2-HMAC-SHA256 | pbkdf2(iterations = 210_000) | JDK only, no extra library |
| bcrypt | bcrypt(cost = 12) | uses the first 72 bytes of the password; $2y$ hashes from other tools verify |
| argon2id | argon2id(memoryKb = 65_536, iterations = 3, parallelism = 1) | the OWASP minimum by default; the PHC string format, so hashes from the reference implementation verify |
Which to choose? argon2id for new applications; bcrypt if you are migrating from a system that uses it; pbkdf2 if you cannot add a library.
bcrypt and argon2id use Bouncy Castle, which is not bundled: add org.bouncycastle:bcprov-jdk18on to your build (a clear error tells you if it is missing). needsRehash is true for another algorithm, a lower cost, less memory, or fewer iterations than the hasher now uses.
Do not truncate or "clean" passwords, and do not log them. Rate-limit login attempts (Rate limiting).
Encrypting data you must read back
A hash cannot be reversed. To store something you must read again later (an access token for another service, a national id, an address), encrypt it. Encryptor uses AES-256-GCM: authenticated encryption with a fresh random nonce per message, so changing any byte of the stored value makes decryption fail (tampering is detected, not silently decrypted to garbage).
val enc = Encryptor.aesGcm(Base64.getDecoder().decode(System.getenv("DATA_KEY"))) // a 32-byte key from your secrets store
val stored: String = enc.encrypt("4111 1111 1111 1111") // base64 text, safe for a column
val plain = enc.decrypt(stored)The key is the secret. Keep it outside the database and outside the repository (an environment variable, a secrets manager, a KMS). If the key is stored next to the data, encryption protects nothing.
Rotating the key: give both keys, new first: Encryptor.aesGcm(newKey, previousKeys = listOf(oldKey)). New values use the new key and old values still decrypt; re-encrypt old rows at your leisure.
Do you need to store it at all?
The best protection is not holding the data. Card numbers belong to your payment provider (store their token), and you rarely need to keep what you can ask for again.