Hash là gì?
Hash (Hàm băm / Giá trị băm) là kết quả của một hàm toán học một chiều (one-way function) nhận đầu vào dữ liệu bất kỳ kích thước (message, file, password) và tạo ra một chuỗi ký tự cố định độ dài (fixed-length) gọi là hash value / digest / fingerprint / checksum.
- Đặc tính cốt lõi:
- Deterministic: Cùng input → Luôn cùng output.
- Pre-image resistance (Kháng nguyên ảnh): Khó tìm input từ hash (one-way).
- Second pre-image resistance: Khó tìm input khác cho cùng hash.
- Collision resistance (Kháng va chạm): Khó tìm 2 input khác nhau cho cùng hash.
- Avalanche effect: Thay đổi 1 bit input → thay đổi ~50% bit output.
Các thuật toán Hash phổ biến (2024)
| Thuật toán | Độ dài (bits) | Trạng thái | Ứng dụng điển hình |
| MD5 | 128 | ❌ Broken (Collision attack 2004) | Checksum file non-security, legacy only |
| SHA-1 | 160 | ❌ Broken (SHAttered 2017) | Git (chuyển sang SHA-256), legacy cert |
| SHA-256 (SHA-2) | 256 | ✅ An toàn, chuẩn NIST/FIPS | TLS 1.2/1.3, Bitcoin, Git, Certificates, HMAC, KDF |
| SHA-384 / SHA-512 | 384 / 512 | ✅ An toàn | High security, TLS, PGP, Linux /etc/shadow (sha512crypt) |
| SHA-3 (Keccak) | 224/256/384/512 | ✅ An toàn, thiết kế khác SHA-2 | Diversification, Ethereum (Keccak-256), new protocols |
| BLAKE2b / BLAKE3 | 256 / 256 | ✅ Rất nhanh, an toàn | File integrity (b2sum), WireGuard, Noise Protocol, TLS 1.3 (BLAKE3) |
| Argon2id / scrypt / bcrypt / PBKDF2 | Tuỳ chỉnh | ✅ Password Hashing (KDF) | Lưu trữ mật khẩu (slow hash, memory-hard, salt). KHÔNG dùng SHA-256 đơn lẻ cho password! |
Hash vs Encryption (Mã hóa)
| Đặc điểm | Hash (One-way) | Encryption (Two-way) |
| Khả năng đảo ngược | Không thể (theoretically) | Có (với key đúng) |
| Key | Không cần key (public function) | Cần key (Symmetric/Asymmetric) |
| Mục đích | Integrity, Authentication, Fingerprint, KDF | Confidentiality (Bí mật) |
| Output length | Fixed | ~Input length (+ padding/IV) |
Ứng dụng thực tế trong Network Security
- Password Storage: Lưu
hash = Argon2id(password, salt, params) trong DB. Verify: Argon2id.verify(stored_hash, input_password).
- Integrity Verification: File download (SHA-256 checksum), Firmware update (signed hash), Git commit (SHA-1→SHA-256), Container image digest (SHA-256).
- Message Authentication (HMAC):
HMAC-SHA256(key, message) – API authentication, JWT (HS256), TLS record layer, IPsec ESP.
- Digital Signature:
Sign(private_key, hash(message)) – Code signing, Certificate (X.509), Document signing, Blockchain transaction.
- Key Derivation Function (KDF): HKDF, PBKDF2, Argon2 → Tạo key encryption từ master secret/password.
- Proof-of-Work / Consensus: Bitcoin (double SHA-256), Ethereum (Keccak-256).
- Certificate Transparency / Merkle Tree: Log integrity, SCT.
- Deduplication / Content-Addressable Storage: IPFS (CID = SHA-256 multihash), Git, Backup (Borg, Restic).
Best Practices 2024
- ✅ Dùng SHA-256/384/512, SHA-3, BLAKE2/3 cho integrity/checksum.
- ✅ Dùng Argon2id (tối ưu), scrypt, bcrypt, PBKDF2-HMAC-SHA256 cho password hashing – tùy chỉnh cost factor (memory, iterations, parallelism).
- ✅ Luôn dùng Salt ngẫu nhiên (>=16 bytes) cho mỗi password.
- ❌ KHÔNG dùng MD5, SHA-1 cho mục đích bảo mật (cert, signature, password).
- ❌ KHÔNG tự thiết kế crypto (roll your own). Dùng thư viện đã audit:
libsodium, OpenSSL, Bouncy Castle, Go crypto, Rust ring.