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

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

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

Hash Function เป็นอัลกอริทึมทางคณิตศาสตร์ที่รับข้อมูล input ขนาดไหนก็ได้ (Message, File, Password) และสร้างค่า output ขนาด คงที่ (Fixed-length) เรียกว่า Hash Value, Hash Code, Digest, หรือ Checksum

คุณสมบัติสำคัญของ Cryptographic Hash Function

Hash Algorithms ยอดนิยม และสถานะปัจจุบัน

คุณสมบัติความหมายความสำคัญ
DeterministicInput เดียวกัน → 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ป้องกันการวิเคราะห์รูปแบบ
AlgorithmOutput Sizeสถานะใช้งาน/หมายเหตุ
MD5128-bit (32 hex)🚫 BrokenCollision Attack เร็วมาก ห้ามใช้งาน Security Purpose
SHA-1160-bit (40 hex)🚫 DeprecatedSHAttered Collision (2017) ห้ามใช้ TLS Cert, Code Signing
SHA-256256-bit (64 hex)✅ StandardBitcoin, TLS Cert, Git, Password Hashing (with salt/iteration)
SHA-384384-bit (96 hex)✅ StandardHigh Security, TLS 1.2/1.3 Cipher Suites
SHA-512512-bit (128 hex)✅ StandardHigh Security, 64-bit Systems เร็วกว่า SHA-256
SHA-3 (Keccak)224/256/384/512✅ ModernNIST Standard ใหม่ โครงสร้างต่างจาก SHA-2 (Sponge)
BLAKE2/3Variable✅ 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

คุณสมบัติHashEncryptionEncoding
Reversible❌ No (One-way)✅ Yes (with Key)✅ Yes (Public Algorithm)
PurposeIntegrity, Fingerprint, CommitmentConfidentialityData Representation/Transport
Key RequiredNo (ยกเว้น HMAC/KDF)Yes (Sym/Asym)No
Output SizeFixed≈ Input Size (+padding/IV)Depends (Base64: 4/3x)
ตัวอย่างSHA-256, Argon2, BLAKE3AES-GCM, ChaCha20-Poly1305, RSA-OAEPBase64, Hex, URL Encoding, UTF-8

Best Practices

  1. 🔐 Password: Argon2id > bcrypt > scrypt > PBKDF2 (อย่าใช้ SHA-256/MD5 ตรงๆ)
  2. 🔐 File Integrity: SHA-256 / SHA-512 / BLAKE2/3 (อย่า MD5/SHA-1)
  3. 🔐 Digital Sig: SHA-256/384/512 + RSA-PSS/ECDSA/EdDSA
  4. 🔐 HMAC: HMAC-SHA256/512 (Key length ≥ Hash output)
  5. 🔐 KDF: HKDF-SHA256 (RFC 5869)
  6. 🔐 Always Salt Passwords (Unique per password, ≥ 16 bytes random)
  7. 🔐 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)