Sefism is free for learners. We are here to help you grow, one step at a time.Join for free
Sefism.

Best practices · Security

Cryptography Best Practices for Developers

Opens with credits

A practical cryptography guide for application developers, not cryptographers. It covers choosing the right primitive and a high-level library, authenticated encryption and nonces, secure randomness, key management with KMS and rotation, TLS configuration, and how to prepare for post-quantum algorithms with crypto agility.

Written by Tauseef Fayyaz6 chapters, 24 practicesAbout 28 min23 quiz questionsReviewed 13 October 2026

What you will learn

6 chapters · about 28 min

  1. 1Choosing the right tool and libraryBefore writing any crypto code, decide which property you need (secrecy, integrity, authenticity) and use a high-level library that gives you exactly that.4 practices · 5 min
  2. 2Encrypting data with AEADAuthenticated encryption keeps data secret and detects tampering in one step. Most of the risk sits in nonces, associated data and how you encrypt with public keys.4 practices · 5 min
  3. Randomness, tokens and key derivationKeys, nonces, tokens and IDs are only as strong as the random numbers behind them. This chapter covers where to get randomness and how to derive keys from other keys.3 practices · 3 minOpens with credits
  4. Key management and rotationEncryption moves the problem from protecting data to protecting keys. This chapter covers where keys live, envelope encryption, cryptoperiods and rotating without downtime.4 practices · 5 minOpens with credits
  5. TLS configuration for servers and clientsTLS protects data in transit, but only with sound protocol settings, well-managed certificates, and clients that actually verify what they connect to.5 practices · 5 minOpens with credits
  6. Post-quantum readiness and crypto agilityNIST has standardized the first post-quantum algorithms. This chapter covers what changed, what did not, and how to make your code ready to swap algorithms.4 practices · 5 minOpens with credits

About this guide

Most cryptography failures in real applications are not broken algorithms. They are a nonce reused with AES-GCM, a token generated with Math.random, a key hard-coded next to the data it protects, a TLS client with certificate checks switched off "for testing", or a home-made scheme that hashes when it should sign. Cryptography best practices for developers are mostly about choosing well-tested building blocks and using them the way their authors intended.

This guide is written for people who build apps and APIs. It starts with picking the right tool for the job (hashing, encryption, MACs or signatures) and a high-level library such as libsodium or Tink, so you are not assembling primitives yourself. It then covers authenticated encryption and nonce rules, randomness, key management and rotation with a KMS or HSM, TLS settings for servers and clients, and the post-quantum transition: what NIST finalized in FIPS 203, 204 and 205, and what crypto agility means for code you write today.

The advice follows NIST SP 800-57 on key management, NIST's post-quantum cryptography project, the OWASP Cryptographic Storage and Transport Layer Security cheat sheets, the TLSRef server-side TLS profiles, and the libsodium and Tink documentation. Password hashing and secret storage have their own guides on Sefism, so this one only points to them.

Common questions

Should I use AES-GCM or ChaCha20-Poly1305?

Both are sound AEADs with 256-bit keys. AES-GCM is fast on CPUs with AES hardware and widely supported by cloud KMS and Web Crypto; ChaCha20-Poly1305 is fast everywhere. If you want random nonces with no practical message limit, libsodium recommends XChaCha20-Poly1305 with its 192-bit nonce.

What is the difference between hashing and encryption?

A hash is a one-way fingerprint with no key; anyone can compute it and nobody can reverse it. Encryption uses a key and is meant to be reversed by whoever holds that key. For proving who sent data you need a MAC (shared key) or a digital signature (private key), and for passwords you need a slow password hash such as Argon2id.

How often should I rotate encryption keys?

NIST SP 800-57 suggests an originator-usage period of up to 2 years for symmetric data encryption and key-wrapping keys, with older data still decryptable for up to 3 more years. Rotate immediately on suspected compromise, when someone with access leaves, or when usage limits such as message counts approach. Cloud KMS automatic rotation makes the scheduled part routine.

What is envelope encryption?

Data is encrypted locally with a data encryption key (DEK), and the DEK is encrypted with a key encryption key (KEK) that stays in a KMS or HSM. The wrapped DEK is stored with the data. It keeps KMS calls small and rare, and rotating the KEK only means rewrapping DEKs.

Do I need to do anything about post-quantum cryptography now?

Start with an inventory of where you use public-key cryptography, and enable hybrid key exchange such as X25519MLKEM768 in TLS where your stack supports it, because recorded traffic can be decrypted later. NIST finalized ML-KEM, ML-DSA and SLH-DSA in FIPS 203, 204 and 205 in August 2024 and plans to remove quantum-vulnerable algorithms from its standards by 2035.

Which TLS versions should a server support?

TLS 1.3, plus TLS 1.2 with ECDHE and AEAD cipher suites for compatibility. TLS 1.0 and 1.1 are deprecated by RFC 8996 and should be disabled, along with SSLv2 and SSLv3.

Sources (8)

Checked against these, last on 13 October 2026.

  1. Cryptographic Storage Cheat Sheet, OWASPcheatsheetseries.owasp.org/cheatsheets/Cryptographic_Storage_Cheat_Sheet.html
  2. Transport Layer Security Cheat Sheet, OWASPcheatsheetseries.owasp.org/cheatsheets/Transport_Layer_Security_Cheat_Sheet.html
  3. SP 800-57 Part 1 Rev. 5: Recommendation for Key Management, Part 1: General, NISTcsrc.nist.gov/pubs/sp/800/57/pt1/r5/final
  4. Post-Quantum Cryptography project, NISTcsrc.nist.gov/projects/post-quantum-cryptography
  5. NIST Releases First 3 Finalized Post-Quantum Encryption Standards, NISTwww.nist.gov/news-events/news/2024/08/nist-releases-first-3-finalized-post-quantum-encryption-standards
  6. Server-Side TLS guidelines, TLSRefdocs.tlsref.org/server-side-tls
  7. Authenticated Encryption with Additional Data, libsodiumdoc.libsodium.org/secret-key_cryptography/aead
  8. Keysets, Tink docsdevelopers.google.com/tink/design/keysets

Picked from the other libraries by shared topics.

Work with me

Stuck on something specific?

Writing only gets you so far. If you want an answer to your situation rather than the general case, book a session and we will work through it together. Sessions are free for approved Sefism members, and a few slots open each week.

  • 1:1 Mentorship
  • Fresh Graduate Mentorship
  • Career Guidance

Brand deals, promos and partnerships: tauseeffayyaz@gmail.com