Hash Function (ฟังก์ชันแฮช) คืออะไร
Hash Function เป็นอัลกอริทึมทางคณิตศาสตร์ที่รับข้อมูลขนาดใดก็ได้ (Input/Message) และสร้างค่าที่มีขนาดคงที่ (Fixed-length Output) เรียกว่า Hash Value / Hash Digest / Checksum / Fingerprint
คุณสมบัติสำคัญ (Properties)
| คุณสมบัติ | คำอธิบาย |
| Deterministic | Input เดียวกัน → Hash เดียวกันเสมอ |
| Fast Computation | คำนวณ Hash ได้รวดเร็ว |
| Pre-image Resistance | จาก Hash หา Input ย้อนกลับไม่ได้ (One-way) |
| Second Pre-image Resistance | หา Input อีกตัวที่ให้ Hash เดียวกันได้ยาก |
| Collision Resistance | หา Input 2 ตัวที่ให้ Hash เดียวกันได้ยาก |
| Avalanche Effect | เปลี่ยน Input 1 bit → Hash เปลี่ยนมาก (~50% bits) |
การใช้งานใน Network Security
1. Password Storage (ไม่เก็บ Plaintext)
User registers: Password → Hash(password + salt) → Store (hash, salt)
User logs in: Input Password → Hash(input + stored_salt) == stored_hash ?
- ป้องกัน: DB Leak → Attacker ไม่ได้ Password จริง
- ต้องใช้ Salt (Random per user) ป้องกัน Rainbow Table
- ต้องใช้ Slow Hash (KDF): Argon2id, bcrypt, scrypt, PBKDF2 — ไม่ใช่ SHA-256/MD5 ตรงๆ
2. Data Integrity Verification
- File Downloads: ตรวจสอบ SHA-256 Checksum จากเว็บไซต์
- Software Updates: Code Signing + Hash Verification
- Configuration Management: ตรวจสอบ Config File ไม่ถูกแก้ไข
- Blockchain/Timestamping: Merkle Trees, Certificate Transparency
3. Digital Signatures & Certificates
Sign: Hash(message) → Encrypt with Private Key → Signature
Verify: Decrypt Signature with Public Key → Hash' == Hash(message) ?
- TLS/SSL Certificates (X.509)
- Code Signing, Document Signing, Email Signing (S/MIME, PGP)
4. Message Authentication Codes (MAC / HMAC)
HMAC(key, message) = Hash((key ⊕ opad) || Hash((key ⊕ ipad) || message))
- API Authentication (AWS SigV4, JWT HS256)
- TLS Record Layer Integrity
- IPsec ESP/AH Integrity
5. Network Protocols & Security
- TLS 1.2/1.3: HKDF (HMAC-based Key Derivation), Certificate Verify
- IPsec: HMAC-SHA256/384/512 สำหรับ Integrity
- SSH: Key Exchange (KDF), Host Key Fingerprint (SHA-256)
- DNSSEC: Hash ของ DNS Records (NSEC/NSEC3)
- Bitcoin/Blockchain: SHA-256 (PoW, Merkle Root, Address Generation)
6. Malware Analysis & Threat Intelligence
- File Identification: MD5/SHA-1/SHA-256 เป็น Unique ID ของไฟล์
- IOC Sharing: Hash = Indicator of Compromise (MISP, STIX, OpenIOC)
- YARA Rules: Hash-based Conditions
- VirusTotal / Hybrid Analysis: Query ด้วย Hash
อัลกอริทึมแฮชยอดนิยม
| Algorithm | Output Size | สถานะ | การใช้งานแนะนำ |
| MD5 | 128-bit | ❌ Broken (Collision) | ห้ามใช้งานด้าน Security (Legacy Only) |
| SHA-1 | 160-bit | ❌ Broken (Collision - SHAttered 2017) | ห้ามใช้งานด้าน Security (Legacy/TLS Deprecated) |
| SHA-256 | 256-bit | ✅ Secure | General Purpose, TLS, Certificates, Bitcoin, HMAC |
| SHA-384 | 384-bit | ✅ Secure | High Security, TLS 1.3, IPsec |
| SHA-512 | 512-bit | ✅ Secure | High Security, 64-bit Systems |
| SHA-3 (Keccak) | 224-512-bit | ✅ Secure (Newer Standard) | Future-proof, Different Construction (Sponge) |
| BLAKE2/3 | 256/512-bit | ✅ Secure, Fast | High Performance, Replacing SHA-2 ในบางโครงการ |
Password Hashing: Special Algorithms (KDFs)
Hash ปกติ (SHA-256) เร็วเกินไป สำหรับ Password → Attacker ทดสอบได้พันล้าน/วินาที (GPU)
| KDF | 특징 | แนะนำ |
| Argon2id | Memory-hard, Time-hard, Parallelism-configurable, Winner PHC 2015 | 🥇 Best Choice ปัจจุบัน |
| bcrypt | Blowfish-based, Cost Factor (Work Factor), Battle-tested | 🥈 ดีมาก รองรับกว้างขวาง |
| scrypt | Memory-hard, Sequential Memory-hard | 🥉 ดี แต่ Parameter ทั้งซับซ้อน |
| PBKDF2 | HMAC-based, Iterations, FIPS/NIST Approved | Legacy/Compliance เท่านั้น (เร็วกว่า GPU) |
ตัวอย่างการใช้ Argon2id (Python)
import argon2
ph = argon2.PasswordHasher()
hash = ph.hash("my_secure_password")
# $argon2id$v=19$m=65536,t=3,p=4$saltsalt$hashhashhash
ph.verify(hash, "my_secure_password") # True/False
Hash vs Encryption vs Encoding
| Hash | Encryption | Encoding |
| Reversible | ❌ No (One-way) | ✅ Yes (Two-way) | ✅ Yes (No Key) |
| Key Required | No (Optional: Salt/Key for HMAC/KDF) | Yes (Symmetric/Asymmetric) | No |
| Purpose | Integrity, Fingerprint, Password Storage | Confidentiality | Data Representation (Base64, Hex, URL) |
| Output Size | Fixed | ~Input Size + Padding | ~Input Size |
การโจมตี Hash และการป้องกัน
| การโจมตี | คำอธิบาย | การป้องกัน |
| Collision Attack | หา Input 2 ตัว Hash เท่ากัน | ใช้ SHA-256+/SHA-3, หลีกเลี่ยง MD5/SHA-1 |
| Length Extension | SHA-256(key||msg) → คำนวณ key||msg||pad||ext | ใช้ HMAC / SHA-3 / BLAKE |
| Rainbow Table | Pre-computed Hash Tables | Salt (Unique per Password) + Slow KDF |
| Brute-force / Dictionary | ทดสอบ Password ทีละตัว | Slow KDF (Argon2id), Rate Limiting, MFA |
| Side-channel (Timing) | เปรียบเทียบ String แบบ Early Exit | Constant-time Comparison |
Best Practices สรุป
- Passwords: Argon2id (memory=64MB, time=3, parallelism=4) + Unique Salt
- General Integrity: SHA-256 (หรือ SHA-3, BLAKE3)
- HMAC: HMAC-SHA256 (Key ≥ 256-bit)
- Digital Signatures: RSA-PSS / ECDSA / EdDSA with SHA-256+
- Certificate/Key Fingerprints: SHA-256 (แสดงเป็น Hex/Base64)
- Deprecate: MD5, SHA-1 ทุก Use Case ด้าน Security
- Crypto Agility: ออกแบบให้สลับ Algorithm ได้ (Algorithm Identifier ใน Protocol)
สรุป
Hash Function เป็น พื้นฐานของความเชื่อถือทางดิจิทัล ใช้สำหรับ Integrity, Authentication, Password Protection, และ Cryptographic Protocols การเลือกอัลกอริทึมที่เหมาะสม (Argon2id สำหรับ Password, SHA-256+ สำหรับ General) และการนำไปใช้อย่างถูกต้อง (Salt, Constant-time, Key Management) เป็นกุญแจสำคัญของ Network Security