Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Encryption and Secrets  ›  Lesson

Hashing: SHA-256 and bcrypt

Cryptographic Hashing 12 minA cryptographic hash is a one-way fingerprint: fast hashes like SHA-256 check that files are unchanged, while slow salted hashes like bcrypt or Argon2 store passwords.

Hashing: SHA-256 and bcrypt

Two jobs land on Tiffin's desk in the same week. The pipeline downloads a scanner tool and must be sure the file was not swapped. And the login system must store customer passwords so that a leaked database does not leak the passwords. Both jobs use hashing, but the right hash is different for each.

The rule

A hash is a one-way fingerprint: fast for files, slow and salted for passwords.

A cryptographic hash function turns any input into a fixed-size value, the digest. Good hashes have three properties:

  • Deterministic: the same input always gives the same digest.
  • One-way: you cannot work backwards from the digest to the input.
  • Collision-resistant: finding two inputs with the same digest is infeasible. Change one bit of input and the digest changes completely.

Hashing is not encryption. There is no key and no "decrypt".

For files, use a fast hash like SHA-256. Pin the expected digest in your repository and compare:

# the expected digest is pinned in the repo, reviewed like any other code
echo "$SCANNER_SHA256  scanner.tar.gz" | sha256sum --check

For passwords, use a slow, salted password hash such as Argon2, bcrypt or scrypt. People pick guessable passwords. If the user table leaks, a fast hash lets an attacker try billions of guesses per second; a slow hash, tuned to take a noticeable fraction of a second, cuts that to a trickle. A salt is a random value stored with each hash, so two customers with the same password get different hashes and precomputed tables of common passwords stop working.

import bcrypt
stored = bcrypt.hashpw(b"lunch-lover-42", bcrypt.gensalt())   # salt is inside `stored`
bcrypt.checkpw(b"lunch-lover-42", stored)                      # True

The shortcut

Ask one question: "Am I checking a thing, or storing a secret someone typed?" A thing → SHA-256. A typed secret → Argon2 or bcrypt with a salt.

Tiffin's two hashing jobsStep 1 of 4
  1. The downloaded scanner

    Checking a thing. Pin its SHA-256 digest in the repo and fail the build if sha256sum --check fails.

Drills

Answer fast — trust the rule.

quick drill

What is the key difference between hashing and encryption?

quick drill

Which is the best choice for storing Tiffin customers' passwords?

quick drill

What does a salt achieve?

quick drill

The pipeline downloads scanner.tar.gz. Where should the expected SHA-256 digest come from?

Remember

  • A hash is a one-way fingerprint: fast for files, slow and salted for passwords.
  • Checking a thing → SHA-256; storing a typed secret → Argon2 or bcrypt.
  • Hashing is not encryption, and a checksum is only as trustworthy as where it came from.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainFast hash or slow hash?

A teammate proposes storing Tiffin customers' passwords as SHA-256 hashes, and also checking the downloaded scanner tool in the pipeline with SHA-256. Explain which half of the proposal is right, which is wrong, and what you would use instead.