Hash Function คืออะไร
ฟังก์ชันคณิตศาสตร์ที่แปลงข้อมูลขนาดใดก็ได้ ให้เป็นค่าคงที่ (Fixed-length) เรียกว่า Hash Value, Hash Code, หรือ Message Digest เป็น One-way Function (ย้อนกลับไม่ได้)
คุณสมบัติสำคัญ
- Deterministic: Input เดียวกัน → Hash เดียวกันเสมอ
- Fast Computation: คำนวณเร็ว
- Pre-image Resistance: หา Input จาก Hash ไม่ได้
- Collision Resistance: หา Input 2 ตัวที่ Hash เท่ากันยากมาก
- Avalanche Effect: เปลี่ยน Input นิดเดียว → Hash เปลี่ยนมาก
อัลกอริทึมยอดนิยม
- MD5 (แตกแล้ว ไม่ควรใช้)
- SHA-1 (แตกแล้ว)
- SHA-256 / SHA-512 (แนะนำ)
- SHA-3 (ล่าสุด)
- bcrypt, scrypt, Argon2 (สำหรับ Password Hashing)
การประยุกต์ใช้ใน Security
- Password Storage: เก็บ Hash แทน Plaintext
- Data Integrity: ตรวจสอบไฟล์ไม่ถูกแก้ไข
- Digital Signature: Hash + Private Key
- Certificate Verification
- Blockchain: Merkle Tree, Proof of Work
- Message Authentication Code (HMAC)
Hashing vs Encryption
Hashing: One-way, ไม่มี Key, ใช้ตรวจสอบความสมบูรณ์
Encryption: Two-way, มี Key, ใช้ปกป้องความลับ