Hashing
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
A hash function turns any data into a short, fixed-size fingerprint called a digest. Run a nine-line Python program and watch SHA-256 turn hello into a 32-byte digest, see one changed letter flip 125 of its 256 bits, and use that to check a download. Then see why passwords are stored as a salted, deliberately slow hash (PBKDF2 with 600,000 rounds, as OWASP advises), and how a login is checked without ever storing the password.
A fingerprint for data
A hash function turns any input, one word or a whole film, into a short fingerprint called a digest. SHA-256 always gives 256 bits: 32 bytes, written as 64 hex characters (0 to 9, a to f).
hash function any bytes in → fixed-size digest out SHA-256 FIPS 180-4, part of the SHA-2 family digest size 256 bits = 32 bytes = 64 hex characters input size a few bytes or many gigabytes: same size out
اقرأ الدرس خطوة بخطوة
A fingerprint for data
A hash function turns any input, one word or a whole film, into a short fingerprint called a digest. SHA-256 always gives 256 bits: 32 bytes, written as 64 hex characters (0 to 9, a to f).
hash function any bytes in → fixed-size digest out SHA-256 FIPS 180-4, part of the SHA-2 family digest size 256 bits = 32 bytes = 64 hex characters input size a few bytes or many gigabytes: same size outLine 2: hash the bytes
Line 1 loads Python's hashlib module. Line 2 feeds the bytes b"hello" into SHA-256. The b means bytes: hash functions work on bytes, so text must be turned into bytes first.
import hashlib, os standard library, nothing to install a = hashlib.sha256(b"hello") b"hello" 5 bytes: 68 65 6c 6c 6f "hello".encode() the same bytes from a strLine 3: the digest
Line 3 prints the digest in hex: 2cf24dba…9824, 64 characters. Run it on any computer, any day, and hello gives exactly this digest. Hash functions are deterministic: same input, same output.
print(a.hexdigest()) 2cf24dba5fb0a30e26e83b2ac5b9e29e 1b161e5c1fa7425e73043362938b9824 the figure shows the first 8 of the 32 bytes same input → same digest, every timeChecking a download
Pretend hello is a file you downloaded. The maker publishes its SHA-256. You hash your copy and compare. Same digest: the file arrived unchanged. CISA advises downloading only from the vendor's own site.
published 2cf24dba…9824 (maker's site) your copy 2cf24dba…9824 (you hashed it) equal? yes → the bytes are unchanged trust the published value must come from the real vendor site, not the same mirrorOne letter changes everything
Lines 4 and 5 change one letter: Hello. That is one bit of input, yet 125 of the 256 output bits flip. The digest, 185f8db3…1969, looks unrelated and no longer matches. Any change to a file shows up.
h = 0x68 = 0110 1000 H = 0x48 = 0100 1000 1 input bit differs hello 2cf24dba…9824 Hello 185f8db3…1969 125 of 256 digest bits differ (about half) match? no → do not use this fileIt only runs one way
Nobody can run SHA-256 backwards to get hello from its digest; they can only guess inputs and hash each guess. Finding two inputs with the same digest, a collision, must also be out of reach. Hashing is not encryption: there is no key.
preimage find an input for a digest SHA-256: 256-bit strength collision find two inputs, one digest SHA-256: 128-bit strength (NIST CSRC hash functions table) encryption reversible with a key; hashing is notRetired hashes: MD5, SHA-1
Older hashes have failed. Researchers have made real collisions for MD5 and SHA-1, so a forged file could share a digest with a genuine one. NIST says to phase out SHA-1 by 2030. Use SHA-256 or SHA-3.
MD5 collisions found: do not use for security SHA-1 collisions found: NIST phase-out by 2030-12-31 SHA-256 SHA-2 family (FIPS 180-4): use this SHA3-256 SHA-3 family (FIPS 202): also finePasswords: add a salt
A site must never store your password. It stores a hash of it, mixed with a salt: random bytes made fresh for each user. Line 6 makes 16, as NIST advises. Same passwords then hash differently, and precomputed tables become useless.
salt = os.urandom(16) 16 random bytes = 128 bits (NIST SP 800-132: at least 128) one salt per user, stored next to the hash salt is not secret; it only has to be unique figure uses a fixed salt 000102030405…Lines 7–9: make it slow
OWASP warns that plain SHA-256 is too fast for passwords: a thief with the hashes can test guesses quickly. PBKDF2 repeats the hash 600,000 times, OWASP's setting, so every guess costs far more. OWASP's first choice is Argon2id.
key = hashlib.pbkdf2_hmac("sha256", pw, salt, 600_000) password tulip-42 salt 00010203… key 69bcaffe…7bec stored salt + key (never the password) OWASP Argon2id > scrypt > bcrypt > PBKDF2Logging in
At login the site runs lines 7 to 9 again with the typed password and the saved salt, then compares the result with the saved key. Equal means the password was right. Python's hmac.compare_digest does the comparing safely.
typed tulip-42 salt read back from the database result 69bcaffe…7bec saved 69bcaffe…7bec hmac.compare_digest(result, saved) → True (takes the same time however many bytes match)
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01You hash a 5-byte word and a 4 GB video with SHA-256. How long are the two digests?
SHA-256 always outputs 256 bits, which is 32 bytes or 64 hex characters, whatever the size of the input.
02A vendor's official site lists a file's SHA-256 digest. You hash your downloaded copy and get a different digest. What should you conclude?
Hashing is deterministic, so the same bytes always give the same digest. Even one changed bit gives an unrelated digest, so a mismatch means the file changed.
03Why should a website not store passwords as plain SHA-256 hashes?
OWASP says fast hashes like SHA-256 are not suitable for passwords. Use a slow password-hashing function such as Argon2id, scrypt, bcrypt or PBKDF2, with a unique salt.
04What does a salt do in password storage?
A salt is random bytes made fresh for each user and stored next to the hash. It is not secret; it only has to be unique.
05Which hash function should you choose for a new integrity check?
Real collisions have been found for MD5 and SHA-1, and NIST says to phase SHA-1 out by the end of 2030. Use SHA-256 (SHA-2) or SHA-3.
المسرد
- Hash function
- A function that turns any input bytes into a fixed-size output. A cryptographic hash such as SHA-256 is also one-way and collision-resistant.
- Digest
- The output of a hash function, often written in hex. A SHA-256 digest is 32 bytes, shown as 64 hex characters.
- Deterministic
- Always giving the same output for the same input. This is what makes comparing digests useful.
- Preimage and collision
- Finding a preimage means finding an input that gives a chosen digest. A collision is two different inputs with the same digest. A good hash makes both impractical.
- Integrity
- The guarantee that data has not been changed. Comparing a file's digest with a trusted published digest checks integrity.
- Salt
- Random bytes, unique for each stored password, mixed in before hashing. It is stored next to the hash and need not be secret.
- PBKDF2
- Password-Based Key Derivation Function 2: it repeats a keyed hash (here HMAC-SHA256) many times, such as 600,000, to make each password guess slow.
- Constant-time comparison
- Comparing two values in a way that takes the same time however many bytes match, such as Python's hmac.compare_digest.
الملاحظات والمصادر
- The figure shows only the first 8 of the 32 digest bytes, and it uses a fixed example salt (00 01 … 0f) so its numbers can be checked. Real code must use a fresh random salt for every user, as line 6 does.
- OWASP lists Argon2id first, then scrypt, bcrypt and PBKDF2. Python ships PBKDF2 and scrypt in hashlib; Argon2id needs a well-known third-party package. Use a library's own password-hashing function when you can.
- A digest only proves a file matches the published value. If an attacker can change both the file and the page listing its digest, the check passes. That is why the digest must come from a trusted channel, and why software makers also sign their releases.
- Hashing is not encryption: there is no key and no way back. For a check that also proves who made the data, use a keyed hash (HMAC) or a digital signature.
- This course is about defence. It names weak hashes only so you can avoid them, and never shows how to attack a real system.
- NIST FIPS 180-4: Secure Hash Standard (SHS)
- NIST FIPS 202: SHA-3 Standard
- NIST CSRC: Hash Functions project (security strengths of SHA-1, SHA-2 and SHA-3)
- NIST: NIST Retires SHA-1 Cryptographic Algorithm (December 15, 2022)
- NIST SP 800-132: Recommendation for Password-Based Key Derivation, Part 1: Storage Applications
- OWASP Password Storage Cheat Sheet
- CISA: Understanding Patches and Software Updates
- Python documentation: hashlib
- Python documentation: hmac.compare_digest
اختبار يتاح بعد الخطوة الأخيرة.