Application developers usually need to choose and use cryptographic tools correctly, not invent new algorithms. The difficult work is identifying the property a feature requires and preserving the assumptions of a reviewed implementation. Encryption, password hashing, signatures, and transport security solve different problems. This guide connects those tools to ordinary application decisions, with particular attention to key ownership, authentication, and what happens when protected data must be recovered or replaced.

Key takeaways

  • Encryption, hashing, signatures, authorization, and transport security solve different problems.
  • Use maintained password-hashing and authenticated-encryption APIs; preserve their key, nonce, and verification requirements.
  • Key access, rotation, and recovery are part of the cryptographic design, not tasks to postpone until after release.

Choose the property before the primitive

Confidentiality prevents an unauthorized reader from understanding content. Integrity and authenticity help establish that protected content has not been altered and was produced by an authorized party under the chosen model. Availability and authorization remain separate application responsibilities. Encrypting a record does not decide which logged-in user should receive it, and signing a message does not hide its contents. State the actual threat and desired property before selecting an API.

Encoding is another distinct concept. Base64 can represent bytes conveniently but provides no secrecy. A general-purpose hash can identify a value or participate in a protocol without being suitable for storing passwords. When reviewing a design, ask what an attacker can observe or modify and where verification happens. Those questions expose category mistakes earlier than a discussion focused only on fashionable algorithm names or key lengths.

Store passwords with a password-hashing API

Passwords need a dedicated, deliberately expensive password-hashing mechanism with a unique salt, not reversible encryption or a fast digest such as plain SHA-256. Use the maintained framework or library API and follow current parameter guidance, with realistic capacity testing. Argon2id is a common recommended choice where supported. Account for the server cost under concurrent login attempts rather than choosing a work factor solely from one successful local benchmark.

Let the password-hashing library manage the encoded format and verify existing hashes using its supported comparison function. Plan parameter upgrades through the framework's rehash mechanism when a user successfully authenticates. Password reset and session revocation deserve separate attention; strong storage does not prevent an unsafe recovery flow. Keep authentication endpoints bounded so an attacker cannot turn expensive verification into uncontrolled resource consumption.

Use authenticated encryption

For application data requiring secrecy, prefer a high-level authenticated-encryption API with a reviewed construction. Authentication failures must reject the value rather than returning partially decrypted content. Store the format version and the metadata required for decryption alongside the ciphertext, following the library's format. Libsodium's secretbox API is one example of a construction that combines confidentiality and integrity, but the right integration depends on the application's platform and threat model.

Where an API supports associated data, it can authenticate context that should remain visible, such as a record type or protocol version. That context needs a stable, unambiguous representation. Do not confuse an authenticated context value with a fresh authorization check: moving a valid ciphertext into another request still requires deciding whether the caller should access it. Preserve the separation between cryptographic validation and the application's business permissions.

Respect randomness and nonce rules

Use the platform or cryptographic library's secure random generator for secret keys, tokens, and other unpredictable material. A timestamp, a general-purpose random function, or a user identifier is not an adequate substitute. Follow the selected construction's nonce requirements precisely. A nonce often does not need to be secret, but reusing it with the same key can be disastrous for constructions that require uniqueness. Different APIs have different lengths and generation rules.

Consider restarts, parallel workers, restored backups, and key rotation when designing uniqueness. A counter that appears safe in one process can repeat after restoring an older state or starting another writer. High-level libraries can reduce these risks when their recommended interfaces fit the use case. Avoid combining primitives into a custom protocol merely to reduce a few stored bytes or avoid using an established message format.

Manage keys as operational assets

Identify who can obtain keys, how the application receives them, where backups live, and how access is audited. A dedicated secret or key-management service can help, but the surrounding access policy still matters. Avoid placing keys in source control, browser bundles, logs, or the same broadly accessible export as the protected data. The useful boundary is the attacker's resulting access, not simply the fact that two values are stored under different filenames.

Plan rotation and recovery before accumulating large amounts of encrypted data. A versioned key identifier can help select the correct decryption key without guessing, while a controlled migration process re-encrypts data where required. Keep access to old keys only as long as the recovery and retention requirements justify it. Losing a necessary key can make legitimate data unrecoverable; retaining every historical key indefinitely creates a different long-term exposure.

Test the protocol and its failure paths

Test successful round trips, modified ciphertext, a wrong key, incorrect associated data, and missing metadata. Verify that errors do not reveal plaintext or secrets through logs and that callers cannot treat verification failure as an ordinary empty value. Use the library's documented interoperability and test material where available. Transport security remains necessary: encrypting one application field does not protect the rest of a request or authenticate the network endpoint by itself.

Keep the cryptographic boundary small enough to review. Document the construction, key source, format, and recovery assumptions rather than scattering encryption decisions throughout business code. Revisit the design when the threat model changes or the underlying library publishes important guidance. Good application cryptography is mostly disciplined use of established tools, precise failure handling, and key management that remains workable after the first implementation has shipped.

Action notes

  1. Document the threat model, chosen construction, key source, stored format, and recovery assumptions for each protected value.
  2. Test modified ciphertext, a wrong key, incorrect associated data, and missing metadata; reject verification failures safely.
  3. Review logs, backups, release artifacts, and browser bundles for accidental key exposure, then rehearse controlled key rotation.

References & further reading