全面解析密碼學雜湊演算法的核心概念、技術演進與實際應用。從經典的 MD5、SHA 系列到現代的 bcrypt、Argon2,深入了解各演算法的特性、安全性考量與正確使用方式。A comprehensive analysis of cryptographic hash algorithms' core concepts, technical evolution, and practical applications. From classic MD5 and SHA series to modern bcrypt and Argon2, learn about each algorithm's characteristics, security considerations, and proper usage.
演算法安全性比較表
什麼是雜湊演算法?What is a Hash Algorithm?
雜湊演算法(Hash Algorithm)是一種將任意長度的輸入資料轉換為固定長度輸出的數學函數。這個輸出被稱為「雜湊值」(Hash Value)或「摘要」(Digest)。A Hash Algorithm is a mathematical function that converts input data of arbitrary length into a fixed-length output. This output is called a "hash value" or "digest".
雜湊函數的三大特性Three Key Properties of Hash Functions
單向性(One-way):從輸入計算雜湊值很容易,但從雜湊值反推原始輸入在計算上不可行。One-way: It's easy to compute the hash from input, but computationally infeasible to reverse-engineer the original input from the hash.
確定性(Deterministic):相同的輸入永遠產生相同的雜湊值。Deterministic: The same input always produces the same hash value.
雪崩效應(Avalanche Effect):輸入的微小變化會導致雜湊值產生巨大差異。Avalanche Effect: Small changes in input cause dramatically different hash values.
雪崩效應示範Avalanche Effect Demonstration
輸入 1: "password"Input 1: "password"
SHA-256: 5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8
輸入 2: "Password"(只改一個字母大小寫)Input 2: "Password" (only one letter changed to uppercase)
SHA-256: e7cf3ef4f17c3999a94f2c6f612e8a888e5b1026878e4e19398b23bd38ec221a
結果:完全不同的雜湊值Result: Completely different hash values
密碼雜湊最佳實踐
雜湊 vs 加密:核心差異Hash vs Encryption: Key Differences
許多人混淆雜湊和加密,但它們的目的和特性完全不同:Many people confuse hashing and encryption, but their purposes and characteristics are completely different:
雜湊像是「數位指紋」,用來識別和驗證資料;加密像是「保險箱」,用來保護資料內容不被看見。密碼儲存應使用雜湊而非加密!Hashing is like a "digital fingerprint" used to identify and verify data; Encryption is like a "safe" used to protect data content from being seen. Password storage should use hashing, not encryption!
⚠️ MD5 存在嚴重的碰撞漏洞,攻擊者可以製造出相同雜湊值的不同檔案。僅適合用於非安全用途,如檔案校驗。⚠️ MD5 has severe collision vulnerabilities. Attackers can create different files with the same hash. Only suitable for non-security purposes like file verification.
這是雜湊最常見的應用。系統不應儲存明文密碼,而是儲存密碼的雜湊值:This is the most common application of hashing. Systems should never store plain-text passwords, but rather store password hashes:
使用者註冊:
1. 使用者輸入密碼: "MyP@ssw0rd"
2. 系統加鹽並雜湊: bcrypt("MyP@ssw0rd" + salt)
3. 儲存雜湊值到資料庫
使用者登入:
1. 使用者輸入密碼: "MyP@ssw0rd"
2. 從資料庫取得儲存的雜湊值
3. 使用相同方法雜湊輸入密碼
4. 比較兩個雜湊值是否一致User Registration:
1. User enters password: "MyP@ssw0rd"
2. System salts and hashes: bcrypt("MyP@ssw0rd" + salt)
3. Store hash value in database
User Login:
1. User enters password: "MyP@ssw0rd"
2. Retrieve stored hash from database
3. Hash input password using same method
4. Compare if both hash values match
2. 資料完整性驗證2. Data Integrity Verification
確保檔案或資料在傳輸過程中未被竄改:Ensure files or data haven't been tampered with during transmission:
找到兩個不同的輸入產生相同的雜湊值。MD5 和 SHA-1 已被證明存在實用的碰撞攻擊。Finding two different inputs that produce the same hash value. MD5 and SHA-1 have been proven to have practical collision attacks.
防禦方法:使用更強的演算法(SHA-256、SHA-512)。Defense: Use stronger algorithms (SHA-256, SHA-512).
預先計算大量密碼的雜湊值並建立查詢表,快速破解未加鹽的密碼雜湊。Pre-computing hash values for many passwords and building lookup tables to quickly crack unsalted password hashes.
彩虹表範例:
password → 5f4dcc3b5aa765d61d8327deb882cf99 (MD5)
123456 → e10adc3949ba59abbe56e057f20f883e (MD5)
qwerty → d8578edf8458ce06fbc5bb76a58c5ca4 (MD5)
攻擊者只需查表即可反查密碼Rainbow Table Example:
password → 5f4dcc3b5aa765d61d8327deb882cf99 (MD5)
123456 → e10adc3949ba59abbe56e057f20f883e (MD5)
qwerty → d8578edf8458ce06fbc5bb76a58c5ca4 (MD5)
Attackers can simply look up the password in the table
防禦方法:加鹽(Salting)- 在密碼後附加隨機字串再雜湊。Defense: Salting - append a random string to the password before hashing.
3. 暴力破解(Brute Force Attack)3. Brute Force Attack
系統化地嘗試所有可能的密碼組合。現代 GPU 可以每秒嘗試數十億次 MD5/SHA 雜湊。Systematically trying all possible password combinations. Modern GPUs can attempt billions of MD5/SHA hashes per second.
防禦方法:使用慢速雜湊函數(bcrypt、Argon2)增加計算成本。Defense: Use slow hash functions (bcrypt, Argon2) to increase computation cost.
加鹽技術深入解析Deep Dive into Salting
不加鹽(危險):
使用者 A 密碼: "password" → Hash: 5f4dcc3b...
使用者 B 密碼: "password" → Hash: 5f4dcc3b...(相同!)
→ 攻擊者破解一個,所有相同密碼都被破解
加鹽(安全):
使用者 A:
密碼: "password"
鹽值: "a8b3c2d1"
雜湊: bcrypt("password" + "a8b3c2d1") → $2a$10$a8b3c2d1...
使用者 B:
密碼: "password"
鹽值: "x9y7z5w3"
雜湊: bcrypt("password" + "x9y7z5w3") → $2a$10$x9y7z5w3...
→ 即使密碼相同,雜湊值完全不同Without Salt (Dangerous):
User A password: "password" → Hash: 5f4dcc3b...
User B password: "password" → Hash: 5f4dcc3b... (same!)
→ Attacker cracks one, all identical passwords are compromised
With Salt (Secure):
User A:
Password: "password"
Salt: "a8b3c2d1"
Hash: bcrypt("password" + "a8b3c2d1") → $2a$10$a8b3c2d1...
User B:
Password: "password"
Salt: "x9y7z5w3"
Hash: bcrypt("password" + "x9y7z5w3") → $2a$10$x9y7z5w3...
→ Even with same password, hash values are completely different
使用者註冊:
1. 驗證密碼強度(長度、複雜度)
2. 使用 bcrypt/Argon2 雜湊密碼
3. 儲存雜湊值到資料庫
4. 記錄使用的演算法和成本因子
使用者登入:
1. 接收使用者輸入
2. 從資料庫查詢雜湊值
3. 使用相同演算法驗證
4. 如果成功且演算法過舊,重新雜湊並更新
5. 實施登入嘗試次數限制
密碼重設:
1. 生成隨機 token(使用安全隨機函數)
2. 雜湊 token 並設定過期時間
3. 發送 token 到使用者信箱
4. 驗證 token 並允許設定新密碼
5. 使雜湊後的新密碼,使所有舊 session 失效User Registration:
1. Validate password strength (length, complexity)
2. Hash password using bcrypt/Argon2
3. Store hash value in database
4. Record the algorithm and cost factor used
User Login:
1. Receive user input
2. Query hash value from database
3. Verify using the same algorithm
4. If successful and algorithm is outdated, rehash and update
5. Implement login attempt rate limiting
Password Reset:
1. Generate random token (using secure random function)
2. Hash token and set expiration time
3. Send token to user's email
4. Verify token and allow setting new password
5. Hash new password, invalidate all old sessions
結論與建議Conclusion and Recommendations
雜湊演算法是現代資訊安全的基石。正確選擇和使用雜湊演算法,能有效保護使用者資料和系統安全。Hash algorithms are the cornerstone of modern information security. Choosing and using hash algorithms correctly can effectively protect user data and system security.
核心建議總結Key Recommendations Summary
✅ 密碼儲存:優先選擇 Argon2,次選 bcrypt✅ Password Storage: Prefer Argon2, bcrypt as second choice
✅ 資料完整性:使用 SHA-256 或 SHA-512✅ Data Integrity: Use SHA-256 or SHA-512
✅ 訊息認證:使用 HMAC-SHA-256✅ Message Authentication: Use HMAC-SHA-256
❌ 避免:MD5 和 SHA-1 用於安全用途❌ Avoid: MD5 and SHA-1 for security purposes
想要快速測試不同雜湊演算法?試試我們的 線上雜湊產生器,支援 MD5、SHA-1、SHA-256、SHA-512、HMAC 等多種演算法,完全本地處理保護隱私。Want to quickly test different hash algorithms? Try our Online Hash Generator, supporting MD5, SHA-1, SHA-256, SHA-512, HMAC and more algorithms, with complete local processing to protect your privacy.