The crypto module provides cryptographic functionality for hashing, encryption, and secure operations.
Hashing#
HMAC#
Password Hashing#
Symmetric Encryption#
Asymmetric Encryption#
Digital Signatures#
Random Values#
Key Derivation#
Diffie-Hellman Key Exchange#
Practical Examples#
Best Practices#
Algorithms:
✓ SHA-256 or SHA-512 for hashing
✓ AES-256-GCM for encryption
✓ scrypt or Argon2 for passwords
✓ Ed25519 for signatures
Security:
✓ Use timing-safe comparison
✓ Generate IVs/nonces randomly
✓ Store salts with hashes
✓ Use authenticated encryption
Key Management:
✓ Generate strong random keys
✓ Rotate keys periodically
✓ Secure key storage
✓ Use key derivation
Avoid:
✗ MD5 or SHA1 for security
✗ ECB mode encryption
✗ Hardcoded secrets
✗ Reusing IVs/nonces
Conclusion#
Node.js crypto module provides comprehensive cryptographic tools. Use appropriate algorithms for each task: SHA-256+ for hashing, AES-GCM for encryption, scrypt for passwords, and modern signature algorithms. Always use secure random generation, proper key derivation, and timing-safe comparisons. Keep security practices current as cryptographic recommendations evolve.