Hash Function (ฟังก์ชันแฮช) คืออะไร?
Hash Function เป็นอัลกอริทึมทางคณิตศาสตร์ที่รับข้อมูล input ขนาดไหนก็ได้ (Message, File, Password) และสร้างค่า output ขนาด คงที่ (Fixed-length) เรียกว่า Hash Value, Hash Code, Digest, หรือ Checksum
คุณสมบัติสำคัญของ Cryptographic Hash Function
| คุณสมบัติ | ความหมาย | ความสำคัญ | |
|---|---|---|---|
| Deterministic | Input เดียวกัน → Hash เดียวกันเสมอ | ตรวจสอบความสอดคล้องได้ | |
| Fast Computation | คำนวณ Hash เร็วสำหรับ Input ใดๆ | ประสิทธิภาพ | |
| Pre-image Resistance | จาก Hash หา Input ย้อนกลับไม่ได้ (One-way) | ป้องกันการค้นหา Password/Message ต้นฉบับ | |
| Second Pre-image Resistance | จาก Input1 หา Input2 ที่ Hash ตรงกันไม่ได้ | ป้องกันการปลอมแปลงไฟล์ | |
| Collision Resistance | หา Input 2 ชุดที่ Hash ตรงกันยากมาก | ป้องกันการสร้าง Certificate/Document ปลอม | |
| Avalanche Effect | เปลี่ยน Input 1 bit → Hash เปลี่ยน ~50% bits | ป้องกันการวิเคราะห์รูปแบบ |
| Algorithm | Output Size | สถานะ | ใช้งาน/หมายเหตุ |
|---|---|---|---|
| MD5 | 128-bit (32 hex) | 🚫 Broken | Collision Attack เร็วมาก ห้ามใช้งาน Security Purpose |
| SHA-1 | 160-bit (40 hex) | 🚫 Deprecated | SHAttered Collision (2017) ห้ามใช้ TLS Cert, Code Signing |
| SHA-256 | 256-bit (64 hex) | ✅ Standard | Bitcoin, TLS Cert, Git, Password Hashing (with salt/iteration) |
| SHA-384 | 384-bit (96 hex) | ✅ Standard | High Security, TLS 1.2/1.3 Cipher Suites |
| SHA-512 | 512-bit (128 hex) | ✅ Standard | High Security, 64-bit Systems เร็วกว่า SHA-256 |
| SHA-3 (Keccak) | 224/256/384/512 | ✅ Modern | NIST Standard ใหม่ โครงสร้างต่างจาก SHA-2 (Sponge) |
| BLAKE2/3 | Variable | ✅ Modern/Fast | เร็วกว่า MD5/SHA-1 แต่ปลอดภัย ใช้ใน WireGuard, Restic, Cargo |
การประยุกต์ใช้ Hash ใน Security (Use Cases)
1. Password Storage (ไม่เก็บ Plaintext!) 🔐
❌ ห้ามเก็บ Hash แบบ Raw (SHA-256(password)) - เร็วเกินไป Rainbow Table/GPU Crack ง่าย
✅ ใช้ Password Hashing Functions (Slow, Salted, Memory-hard):
- Argon2id (แนะนำสุด - PHC Winner): Memory-hard, Parallelism, Time cost
- bcrypt: Blowfish-based, Cost factor (work factor), Battle-tested
- scrypt: Memory-hard, ใช้ใน Litecoin, Key Derivation
- PBKDF2: HMAC-based, NIST Standard, ใช้ใน LUKS, 1Password (Legacy)
Format มาตรฐาน: $algorithm$parameters$salt$hash (Modular Crypt Format)
2. File Integrity Verification (File Checksum)
- Download ISO/Software → ตรวจ SHA-256 Hash ตรงกับเว็บไซต์ → ไม่ถูก改竄/Corrupt
- Tools:
sha256sum(Linux),Get-FileHash -Algorithm SHA256(PowerShell), VirusTotal - System: Tripwire, AIDE, OSSEC/Wazuh, systemd-homed Monitor ไฟล์ระบบ
3. Digital Signatures & Certificates (PKI)
- Hash ข้อมูล (Document, Code, Cert TBSCertificate) → Sign ด้วย Private Key (RSA/ECDSA/EdDSA)
- Verify: Hash ข้อมูล → Verify Signature ด้วย Public Key → ตรง = Authentic + Intact
- TLS Certificates, Code Signing (Microsoft Authenticode, Apple Notarization), Document Signing
4. Blockchain & Cryptocurrency
- Bitcoin: Double SHA-256 (SHA256d) สำหรับ Block Header & Transaction ID
- Ethereum: Keccak-256 (SHA-3 variant) สำหรับ Address, Transaction Hash, State Root
- Merkle Tree: Hash Tree สำหรับ Verify Membership/Integrity แบบ Logarithmic
5. Message Authentication Code (HMAC)
HMAC(K, m) = H((K ⊕ opad) || H((K ⊕ ipad) || m))- ใช้ Shared Secret Key K + Hash Function → ป้องกัน Tampering + Verify Sender
- JWT (HS256/HS384/HS512), API Authentication, TLS Record Layer, IPsec
6. Key Derivation Function (KDF)
- HKDF (HMAC-based KDF): Extract-then-Expand → Derive Multiple Keys จาก Master Secret
- TLS 1.3, Signal Protocol, Age Encryption, WireGuard
Hash vs Encryption vs Encoding
| คุณสมบัติ | Hash | Encryption | Encoding |
|---|---|---|---|
| Reversible | ❌ No (One-way) | ✅ Yes (with Key) | ✅ Yes (Public Algorithm) |
| Purpose | Integrity, Fingerprint, Commitment | Confidentiality | Data Representation/Transport |
| Key Required | No (ยกเว้น HMAC/KDF) | Yes (Sym/Asym) | No |
| Output Size | Fixed | ≈ Input Size (+padding/IV) | Depends (Base64: 4/3x) |
| ตัวอย่าง | SHA-256, Argon2, BLAKE3 | AES-GCM, ChaCha20-Poly1305, RSA-OAEP | Base64, Hex, URL Encoding, UTF-8 |
Best Practices
- 🔐 Password: Argon2id > bcrypt > scrypt > PBKDF2 (อย่าใช้ SHA-256/MD5 ตรงๆ)
- 🔐 File Integrity: SHA-256 / SHA-512 / BLAKE2/3 (อย่า MD5/SHA-1)
- 🔐 Digital Sig: SHA-256/384/512 + RSA-PSS/ECDSA/EdDSA
- 🔐 HMAC: HMAC-SHA256/512 (Key length ≥ Hash output)
- 🔐 KDF: HKDF-SHA256 (RFC 5869)
- 🔐 Always Salt Passwords (Unique per password, ≥ 16 bytes random)
- 🔐 Constant-time Comparison ป้องกัน Timing Attack (
hmac.compare_digest,subtle.ConstantTimeCompare)
สรุป
Hash Function เป็น "นิ้วมือดิจิทัล" ของข้อมูล - ใช้ยืนยันความสมบูรณ์ (Integrity) ไม่ใช่ความลับ (Confidentiality) เลือก Algorithm ให้ถูก Use Case: Password → Argon2id/bcrypt, File/Integrity → SHA-256/BLAKE3, TLS/PKI → SHA-256/384, Crypto → SHA-256/Keccak และต้องอัปเดตเมื่อมีช่องโหว่ใหม่ (MD5, SHA-1 → Deprecated)