Hash (ฟังก์ชันแฮช) ในความปลอดภัยเครือข่ายคืออะไร?
Hash Function (ฟังก์ชันแฮช) เป็นอัลกอริทึมทางคณิตศาสตร์ที่รับข้อมูลนำเข้า (Input) ขนาดไม่จำกัด แปลงเป็นค่าคงที่ (Fixed-length Output) เรียกว่า Hash Value, Hash Code, Digest, หรือ Checksum
คุณสมบัติสำคัญของ Cryptographic Hash Function
| คุณสมบัติ | คำอธิบาย |
| Deterministic | Input เดียวกัน ต้องได้ Hash เดียวกันทุกครั้ง |
| Pre-image Resistance (One-way) | ไม่สามารถย้อนกลับจาก Hash หา Input ต้นฉบับได้ (Computationally Infeasible) |
| Second Pre-image Resistance | หา Input อีกตัวที่ให้ Hash เดียวกันได้ยาก |
| Collision Resistance | หา Input 2 ตัวที่ให้ Hash เดียวกันได้ยาก (Birthday Attack) |
| Avalanche Effect | เปลี่ยน Input เพียง 1 บิต → Hash เปลี่ยนประมาณ 50% ของบิต |
| Fixed Output Length | ไม่ว่าขนาด Input จะใหญ่แค่ไหน Output คงที่ (เช่น SHA-256 = 256 บิต = 64 Hex chars) |
อัลกอริทึมแฮชยอดนิยม
| อัลกอริทึม | ขนาด Output | สถานะ | ใช้งาน |
| MD5 | 128-bit (32 hex) | แตกแล้ว (Broken) - ห้ามใช้งานด้านความปลอดภัย | Checksum ไฟล์ (Non-security), Legacy |
| SHA-1 | 160-bit (40 hex) | อ่อนแอ (Deprecated) - พบ Collision แล้ว | Git, TLS Certs (เก่า), Legacy |
| SHA-256 | 256-bit (64 hex) | มาตรฐานปัจจุบัน (Recommended) | TLS, Bitcoin, Password Hashing (กับ Salt), Digital Signatures, Blockchain |
| SHA-384 | 384-bit | เข้มกว่า SHA-256 | High Security, TLS 1.3 |
| SHA-512 | 512-bit | เข้มกว่า SHA-384 | High Security, Password Hashing |
| SHA-3 (Keccak) | 224/256/384/512 | รุ่นใหม่ โครงสร้างต่างจาก SHA-2 | Future-proof, Ethereum |
| BLAKE2/3 | Variable | เร็วกว่า SHA-2/3 มาก | High Performance, Crypto Currencies |
การใช้งาน Hash ในความปลอดภัย
- Password Storage: เก็บ Hash(Password + Salt) แทน Plaintext ใช้ Bcrypt, Argon2, PBKDF2, Scrypt (Slow Hash)
- Data Integrity: ตรวจสอบไฟล์ดาวน์โหลด (Checksum), Git Commits, Blockchain Transactions
- Digital Signatures: Hash(Document) + Sign with Private Key
- Certificate Fingerprints: SHA-256 ของ Certificate ใช้ Verify
- HMAC (Hash-based MAC): Message Authentication + Integrity (API Authentication, JWT)
- Key Derivation: HKDF, PBKDF2 สร้าง Key จาก Password/Secret
- Proof of Work: Bitcoin Mining (SHA-256d)
Hash vs Encryption
| คุณสมบัติ | Hash (One-way) | Encryption (Two-way) |
| 可逆性 (Reversibility) | ไม่สามารถย้อนกลับได้ | สามารถถอดรหัสได้ด้วย Key |
| Key | ไม่ใช้ Key (Unkeyed) *ยกเว้น HMAC | ต้องใช้ Key (Symmetric/Asymmetric) |
| Output Length | คงที่ | ขึ้นกับ Input + Padding |
| ใช้งาน | Integrity, Authentication, Password | Confidentiality (ความลับ) |
ตัวอย่าง Hash Value (SHA-256)
Input: "Hello World"
SHA-256: a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e
Input: "Hello World!" (เพิ่ม ! เดียว)
SHA-256: 7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069
สรุป
Hash เป็นรากฐานของความปลอดภัยดิจิทัล ใช้รับรอง Integrity (ความสมบูรณ์) และ Authentication (ความแท้จริง) ไม่ใช่สำหรับ Confidentiality (ความลับ) ต้องเลือกอัลกอริทึมที่เหมาะสม (SHA-256 ขึ้นไป) และใช้ Salt/Slow Hash สำหรับรหัสผ่าน