ENع
→Hashing
الدرس 06 · التشفير

Hashing

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

12 دقيقة10 خطوات5 أسئلةIntermediate
عن هذا الدرس

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.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 6 · تفاعلي
01 / 10

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
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. 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
  2. Line 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 str
  3. Line 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 time
  4. Checking 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 mirror
  5. One 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 file
  6. It 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 not
  7. Retired 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 fine
  8. Passwords: 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…
  9. 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 > PBKDF2
  10. Logging 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)

اختبار يتاح بعد الخطوة الأخيرة.