Computer >> Hướng Dẫn Máy Tính >  >> Kết Nối Mạng >> An Ninh Mạng

Hash (Hàm băm) trong an ninh mạng: Khái niệm, vai trò và ứng dụng

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
MD5128❌ Broken (Collision attack 2004)Checksum file non-security, legacy only
SHA-1160❌ Broken (SHAttered 2017)Git (chuyển sang SHA-256), legacy cert
SHA-256 (SHA-2)256✅ An toàn, chuẩn NIST/FIPSTLS 1.2/1.3, Bitcoin, Git, Certificates, HMAC, KDF
SHA-384 / SHA-512384 / 512✅ An toànHigh security, TLS, PGP, Linux /etc/shadow (sha512crypt)
SHA-3 (Keccak)224/256/384/512✅ An toàn, thiết kế khác SHA-2Diversification, Ethereum (Keccak-256), new protocols
BLAKE2b / BLAKE3256 / 256✅ Rất nhanh, an toànFile integrity (b2sum), WireGuard, Noise Protocol, TLS 1.3 (BLAKE3)
Argon2id / scrypt / bcrypt / PBKDF2Tuỳ 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ểmHash (One-way)Encryption (Two-way)
Khả năng đảo ngượcKhông thể (theoretically)Có (với key đúng)
KeyKhông cần key (public function)Cần key (Symmetric/Asymmetric)
Mục đíchIntegrity, Authentication, Fingerprint, KDFConfidentiality (Bí mật)
Output lengthFixed~Input length (+ padding/IV)

Ứng dụng thực tế trong Network Security

  1. Password Storage: Lưu hash = Argon2id(password, salt, params) trong DB. Verify: Argon2id.verify(stored_hash, input_password).
  2. Integrity Verification: File download (SHA-256 checksum), Firmware update (signed hash), Git commit (SHA-1→SHA-256), Container image digest (SHA-256).
  3. Message Authentication (HMAC): HMAC-SHA256(key, message) – API authentication, JWT (HS256), TLS record layer, IPsec ESP.
  4. Digital Signature: Sign(private_key, hash(message)) – Code signing, Certificate (X.509), Document signing, Blockchain transaction.
  5. Key Derivation Function (KDF): HKDF, PBKDF2, Argon2 → Tạo key encryption từ master secret/password.
  6. Proof-of-Work / Consensus: Bitcoin (double SHA-256), Ethereum (Keccak-256).
  7. Certificate Transparency / Merkle Tree: Log integrity, SCT.
  8. 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.