Hashing & Encryption with larzcrypt
The two workhorses of security: hashing (for passwords and integrity) and encryption (for confidentiality). larzcrypt provides both correctly, verified against the official standards.
Two cryptographic operations underpin most security: hashing (a one-way fingerprint, used for passwords and integrity) and encryption (reversible scrambling for confidentiality). Using them correctly is essential and easy to get subtly wrong. larzcrypt provides both via standard algorithms verified against RFC test vectors, so you use them right without implementing them.
Hashing: passwords and integrity
A hash is a one-way fingerprint - you can't reverse it. That's exactly why it's used to store passwords (store the hash, never the password) and to verify data hasn't changed.
from larzcrypt import hash_password, verify_password
stored = hash_password('correct horse battery staple') # scrypt, salted
# store `stored` in your DB - never the raw password
verify_password('correct horse battery staple', stored) # True| Use | How |
|---|---|
| Store passwords | hash_password (scrypt, salted) - never plaintext |
| Check a password | verify_password against the stored hash |
| Verify integrity/authenticity | HMAC - detect tampering |
| Fingerprint data | SHA-2/3, BLAKE2 |
The golden rule: never store a password itself, only a strong, salted hash of it - so even a database breach doesn't hand attackers the passwords.
Encryption: confidentiality, authenticated
Encryption keeps data secret. Modern practice uses 'authenticated' encryption, which also detects tampering - larzcrypt provides ChaCha20-Poly1305 for this.
Use crypto correctly
- Hash passwords - scrypt, salted; never store plaintext.
- HMAC for integrity/authenticity.
- Authenticated encryption (AEAD) for confidentiality + tamper-detection.
- Standard, verified - larzcrypt matches RFC 8439/7748/8032/5869/4231 vectors.
๐ฏ Key takeaways
- Hashing is a one-way fingerprint (for passwords and integrity); encryption is reversible (for confidentiality).
- Never store passwords - store a strong salted hash (scrypt); verify against it.
- Use authenticated encryption (AEAD, ChaCha20-Poly1305) so data is both secret and tamper-evident.
- larzcrypt provides these via standard algorithms verified against official RFC test vectors.
Frequently asked
Why can't I just store passwords encrypted instead of hashed?
Because encryption is reversible - if you encrypt passwords, then whoever has the decryption key (you, or an attacker who steals it) can recover every password in plaintext, which is exactly the disaster you're trying to prevent. Hashing is one-way: you store a fingerprint that can verify a password (by hashing the attempt and comparing) but can't be reversed back into the password, so even a full database breach doesn't directly expose passwords. That's why the correct approach is a strong, salted password hash (like scrypt), never encryption and never plaintext. The salt ensures identical passwords don't produce identical hashes, defeating precomputed-table attacks.
What's 'authenticated encryption' and why does it matter?
Plain encryption only guarantees confidentiality - it hides the data - but it doesn't tell you whether the ciphertext was tampered with in transit or storage, and naive encryption can be manipulated in ways that cause subtle, serious vulnerabilities. Authenticated encryption (AEAD), like ChaCha20-Poly1305, adds an integrity/authenticity guarantee: on decryption it verifies the data wasn't altered and rejects it if it was. This is almost always what you actually want, because you care both that data is secret and that it's genuine. Using AEAD by default (as larzcrypt encourages) avoids a whole class of mistakes that come from using encryption without integrity protection.
Related lessons
Security Fundamentals for Builders
Most security breaches come from a handful of well-understood mistakes. Knowing the fundamโฆ
Security & Identity with the Larz StackManaging Secrets with larzvault
API keys, passwords, tokens - every app has secrets, and leaking them is a top cause of brโฆ
Understanding & Building on LarzChainWallets, Keys & Transactions
Owning crypto means holding a private key. Understanding keys, addresses, signatures and tโฆ