Hashing
Cryptographic hash functions take arbitrary input and produce a fixed-size output (digest) with properties that make them suitable for security applications. They are one-way functions: computing the hash is easy, but reversing it to find the input is computationally infeasible.
Properties of Cryptographic Hash Functions
- Deterministic: Same input always produces the same output.
- Pre-image resistance: Given a hash, it is infeasible to find the original input.
- Second pre-image resistance: Given an input, it is infeasible to find another input with the same hash.
- Avalanche effect: A single bit change in input produces a drastically different hash.
- Fixed output size: Regardless of input length, output is always the same size.
Common Hash Algorithms
SHA-2 Family
- SHA-256: 256-bit output. Widely used for certificates, signatures, and integrity checks.
- SHA-384 / SHA-512: Longer outputs for higher security margins.
SHA-3 Family
Keccak-based, different construction from SHA-2. Provides an alternative in case SHA-2 vulnerabilities are discovered.
MD5 and SHA-1
- MD5: 128-bit output. Broken—collisions can be generated trivially. Do not use.
- SHA-1: 160-bit output. Broken in practice. Do not use for security.
Password Hashing
Password hashing requires special considerations because passwords have low entropy and are subject to brute-force attacks. Use password hashing algorithms designed to be slow:
- bcrypt: Adaptive, configurable cost factor.
- scrypt: Memory-hard, resistant to GPU/ASIC attacks.
- Argon2: Winner of the Password Hashing Competition. Memory, CPU, and parallelism parameters.
- PBKDF2: Iterative, widely supported, but less resistant to GPUs than scrypt/Argon2.
Never store passwords with fast hashes like SHA-256.
# Generate SHA-256 hash echo -n "password" | sha256sum # Hash password with bcrypt (Python) from bcrypt import hashpw, gensalt hashpw(b"password", gensalt()) # Hash password with Argon2 (command line) argon2 "password" -t 2 -m 65536 -p 4
Salt
A salt is random data added to the input before hashing. Salts ensure that identical passwords produce different hashes, preventing rainbow table attacks and ensuring that hash cracking must be done individually for each user.
- Salt should be unique per user.
- Salt does not need to be secret.
- Store salt alongside the hash.
HMAC (Hash-Based Message Authentication Code)
HMAC combines a hash function with a secret key to provide message authentication and integrity:
echo -n "message" | openssl dgst -sha256 -hmac "secret_key"
HMAC is used in API signatures, JWT tokens, and secure cookies.
File Integrity
Verify downloaded files against published hashes:
sha256sum file.iso sha512sum file.iso
Digital Signatures
Hash functions are the first step in creating digital signatures. The message is hashed, and the hash is encrypted with the sender's private key. The recipient verifies by hashing the message and decrypting the signature with the sender's public key.
Related Articles
- Encryption: Article - Encryption
- Certificates: Article - Certification
- Integrity monitoring: Article - System monitoring