Best Practices

Cipher selection

  • Prefer AES-GCM or ChaCha20-Poly1305 for authenticated encryption.

  • Prefer AES-256-CBC or ChaCha20 (with separate MAC) if AEAD is not available.

  • Avoid ECB mode entirely; in this library ECB selector is mapped to CTR with zero IV only for compatibility.

  • Avoid DES/3DES; only use for legacy interoperability with strict key/IV handling.

Asymmetric

  • Use RSA-OAEP (SHA-256+) to encrypt small payloads such as session keys; do not use it for large data.

  • Use X25519 or ECDH P-256/P-384 for key exchange; derive symmetric keys via HKDF.

Nonce/IV management

  • AES-GCM requires a 12-byte nonce. Never reuse a nonce with the same key.

  • AES CBC/CTR require a 16-byte IV. Never reuse an IV with the same key.

  • ChaCha20 and ChaCha20-Poly1305 require a 12-byte nonce. Never reuse a nonce with the same key.

  • DES/3DES CBC/CTR use an 8-byte IV; treat reuse as catastrophic.

Security Best Practices

This guide covers essential security best practices when using cryptographer.js in production applications.

Algorithm Selection

Hash Functions

Password Hashing

Encryption

Key Management

Generate Strong Keys

Key Derivation

Key Storage

Salt Management

Use Unique Salts

Salt Length

Parameter Selection

Argon2 Parameters

PBKDF2 Parameters

bcrypt Parameters

Timing Attacks

Use Timing-Safe Comparison

Constant-Time Operations

Input Validation

Validate Input Parameters

Sanitize Input

Error Handling

Don't Expose Sensitive Information

Handle Errors Gracefully

Memory Management

Clear Sensitive Data

Secure Communication

Authenticated Encryption

Secure Key Exchange

Audit and Monitoring

Log Security Events

Monitor Performance

Compliance and Standards

FIPS Compliance

GDPR Compliance

Testing Security

Test for Common Vulnerabilities

Penetration Testing

Summary

Follow these security best practices:

  1. Use recommended algorithms (SHA-256, Argon2id, AES-256)

  2. Generate strong, random keys and salts

  3. Use timing-safe comparisons to prevent timing attacks

  4. Validate all inputs before processing

  5. Handle errors securely without exposing sensitive information

  6. Clear sensitive data from memory when done

  7. Use authenticated encryption for secure communication

  8. Monitor and log security events

  9. Test for vulnerabilities regularly

  10. Stay compliant with relevant standards

Remember: Security is an ongoing process, not a one-time implementation.

Last updated