Continuing from our study of encryption, we must realize that secrecy is useless if the data is altered during transit. Hashing provides a 'digital fingerprint' of data, ensuring that not a single bit has been flipped by a malicious actor or a noisy network cable.
A secure hash function must be deterministic (same input always equals same output), one-way (impossible to reverse), and collision-resistant (two different inputs should not produce the same output). While MD5 and SHA-1 were once standard, they are now broken and should never be used in new protocols.
๐ก SHA-2 (e.g., SHA-256) is the current industry standard. SHA-3 is a newer alternative based on the 'Keccak' sponge construction, providing a different mathematical approach to protect against potential vulnerabilities in the SHA-2 family.
The command above produces a fixed-length string regardless of the input size. However, a simple hash does not prove *who* sent the data, only that the data hasn't changed. This leads to the need for keyed hashes.
HMAC (Hash-based Message Authentication Code) combines a cryptographic hash function with a secret key. This prevents 'length extension attacks' where an attacker could append data to a hashed message without knowing the original content.
โ ๏ธ Do not simply concatenate a key and a message (e.g., Hash(key + message)). This is vulnerable to specific attacks. Always use the official HMAC construction: Hash((K XOR opad) || Hash((K XOR ipad) || message)).
import hmac
import hashlib
key = b'secret-shared-key'
message = b'Action: Transfer $100'
hmac_obj = hmac.new(key, message, hashlib.sha256)
print(f'HMAC: {hmac_obj.hexdigest()}')| Algorithm | Type | Security Status | Primary Risk |
|---|---|---|---|
| MD5 | Hash | Broken | Collision Attacks |
| SHA-1 | Hash | Weak | Collision Attacks |
| SHA-256 | Hash | Secure | Length Extension (if not used in HMAC) |
| SHA-3 | Hash | Secure | Low / New Standard |
In a production environment, the most critical defense is the immediate deprecation of legacy algorithms. When auditing a protocol, if you see MD5 or SHA-1 used for security-critical signatures, it should be flagged as a High severity finding.
Hashing is NOT encryption. You cannot 'decrypt' a hash. If you need to retrieve the original data, use symmetric encryption.
Verify exercises to earn โ 130 XP and unlock next lab level.