Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ระบบเครือข่าย >> ความปลอดภัยเครือข่าย

Hash Function ในความปลอดภัยเครือข่าย: หลักการ การใช้งาน และอัลกอริทึม

Hash Function (ฟังก์ชันแฮช) คืออะไร

Hash Function เป็นอัลกอริทึมทางคณิตศาสตร์ที่รับข้อมูลขนาดใดก็ได้ (Input/Message) และสร้างค่าที่มีขนาดคงที่ (Fixed-length Output) เรียกว่า Hash Value / Hash Digest / Checksum / Fingerprint

คุณสมบัติสำคัญ (Properties)

การใช้งานใน 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

อัลกอริทึมแฮชยอดนิยม

คุณสมบัติคำอธิบาย
DeterministicInput เดียวกัน → 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)

Password Hashing: Special Algorithms (KDFs)

Hash ปกติ (SHA-256) เร็วเกินไป สำหรับ Password → Attacker ทดสอบได้พันล้าน/วินาที (GPU)

AlgorithmOutput Sizeสถานะการใช้งานแนะนำ
MD5128-bit❌ Broken (Collision)ห้ามใช้งานด้าน Security (Legacy Only)
SHA-1160-bit❌ Broken (Collision - SHAttered 2017)ห้ามใช้งานด้าน Security (Legacy/TLS Deprecated)
SHA-256256-bit✅ SecureGeneral Purpose, TLS, Certificates, Bitcoin, HMAC
SHA-384384-bit✅ SecureHigh Security, TLS 1.3, IPsec
SHA-512512-bit✅ SecureHigh Security, 64-bit Systems
SHA-3 (Keccak)224-512-bit✅ Secure (Newer Standard)Future-proof, Different Construction (Sponge)
BLAKE2/3256/512-bit✅ Secure, FastHigh Performance, Replacing SHA-2 ในบางโครงการ

ตัวอย่างการใช้ 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

KDF 특징แนะนำ
Argon2idMemory-hard, Time-hard, Parallelism-configurable, Winner PHC 2015🥇 Best Choice ปัจจุบัน
bcryptBlowfish-based, Cost Factor (Work Factor), Battle-tested🥈 ดีมาก รองรับกว้างขวาง
scryptMemory-hard, Sequential Memory-hard🥉 ดี แต่ Parameter ทั้งซับซ้อน
PBKDF2HMAC-based, Iterations, FIPS/NIST ApprovedLegacy/Compliance เท่านั้น (เร็วกว่า GPU)
HashEncryptionEncoding
Reversible❌ No (One-way)✅ Yes (Two-way)✅ Yes (No Key)
Key RequiredNo (Optional: Salt/Key for HMAC/KDF)Yes (Symmetric/Asymmetric)No
PurposeIntegrity, Fingerprint, Password StorageConfidentialityData Representation (Base64, Hex, URL)
Output SizeFixed~Input Size + Padding~Input Size

การโจมตี Hash และการป้องกัน

Best Practices สรุป

  1. Passwords: Argon2id (memory=64MB, time=3, parallelism=4) + Unique Salt
  2. General Integrity: SHA-256 (หรือ SHA-3, BLAKE3)
  3. HMAC: HMAC-SHA256 (Key ≥ 256-bit)
  4. Digital Signatures: RSA-PSS / ECDSA / EdDSA with SHA-256+
  5. Certificate/Key Fingerprints: SHA-256 (แสดงเป็น Hex/Base64)
  6. Deprecate: MD5, SHA-1 ทุก Use Case ด้าน Security
  7. 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

การโจมตีคำอธิบายการป้องกัน
Collision Attackหา Input 2 ตัว Hash เท่ากันใช้ SHA-256+/SHA-3, หลีกเลี่ยง MD5/SHA-1
Length ExtensionSHA-256(key||msg) → คำนวณ key||msg||pad||extใช้ HMAC / SHA-3 / BLAKE
Rainbow TablePre-computed Hash TablesSalt (Unique per Password) + Slow KDF
Brute-force / Dictionaryทดสอบ Password ทีละตัวSlow KDF (Argon2id), Rate Limiting, MFA
Side-channel (Timing)เปรียบเทียบ String แบบ Early ExitConstant-time Comparison