Core Concepts

Crypto Basics: Hashing, Encryption, and Certificates

25 min

Prerequisites

Lesson 1, for confidentiality and integrity. The bytes lesson in Computing Basics helps.

Three tools that are often confused

Cryptography is the maths that protects data. Beginners (and many headlines) mix up three very different tools:

Tool Can you get the original back? What it protects
Encoding (e.g. base64) Yes, anyone can Nothing. It only changes the format.
Hashing (e.g. SHA-256) No Integrity: proves data has not changed
Encryption (e.g. AES) Yes, with the key Confidentiality: only key holders can read it

Hashing: a fingerprint for data

A hash function turns any amount of data into a short, fixed-length fingerprint. SHA-256 always gives 64 hex characters, whether the input is one letter or a whole movie. A good hash has three properties:

  • The same input always gives the same hash.
  • Changing even one bit of the input changes the hash completely.
  • You cannot work backward from the hash to the input.

That makes hashes perfect for integrity. If the hash of a file today matches the hash from yesterday, the file has not changed. It is also why websites store a hash of your password, not the password: when you log in, they hash what you typed and compare. A stolen database of hashes is much less useful to an attacker than a list of passwords. (Real systems use special slow password hashes, but the idea is the same.)

Encryption: locking data with a key

Encryption scrambles data so only someone with the right key can unscramble (decrypt) it. Two kinds:

  • Symmetric encryption uses one key to lock and unlock, like a house key. It is fast, so it protects large amounts of data, such as an encrypted disk. The hard part is sharing the key safely.
  • Asymmetric (public-key) encryption uses a key pair. The public key can be shared with anyone; the private key never leaves its owner. Data locked with the public key can only be opened with the private key. The private key can also sign data, and anyone with the public key can check the signature.

Real systems use both: asymmetric to agree on a key safely, then symmetric for speed.

Certificates: proving who a website is

When you open an https:// website, your browser and the site use TLS to encrypt the connection. But encryption alone is not enough: you also need to know you are talking to the real site, not an impostor.

That is the job of a certificate. It contains the site’s name and public key, signed by a certificate authority (CA) that your browser already trusts. If the signature checks out and the name matches, the browser shows the padlock. A warning about the certificate means one of those checks failed. Do not click through it on a site you care about.

Hands-on: hashes and encoding on your own machine

Create a practice folder and a small file:

mkdir -p ~/security-concepts && cd ~/security-concepts
echo "Meet at 10:00" > note.txt
shasum -a 256 note.txt

Now change one character, 10:00 to 10:01, and hash again:

echo "Meet at 10:01" > note.txt
shasum -a 256 note.txt

The two hashes share nothing recognizable, even though one character changed. That is what makes tampering easy to detect. (Windows and macOS/Linux may give different hashes for the ā€œsameā€ text, because they save line endings differently: the bytes really are different.)

Next, see that base64 is not encryption. Encode a fake password, then decode it, with no key at all:

echo -n "password123" | base64
echo "cGFzc3dvcmQxMjM=" | base64 --decode; echo

Anyone can reverse it. Attackers use base64 to hide commands from a quick glance (remember -EncodedCommand in Windows Foundations), but it protects nothing.

Hands-on: verify a download

This site shows a SHA-256 next to every lab file. Download this sample log (you will use it again in the Python track):

Sample SSH log (auth.log)auth.log Ā· 5 KBSHA-256: 2dad2fcfdd2aec7f7f1bd1005a9fc640d1623ebde43b4328af168e09f09bfe0c

Hash the downloaded file with the command for your OS (the file is usually in your Downloads folder), and compare the result with the hash shown above. If they match, the file arrived exactly as published.

Finally, open any https:// site you use, click the padlock (or the icon next to the address), and find the certificate details: who it was issued to, who issued it, and when it expires.

What to notice

  • Encoding hides nothing, hashing proves integrity, and encryption protects confidentiality with a key.
  • One changed character gives a completely different hash. That is how tampering gets caught.
  • Public keys can be shared; private keys never can. Certificates tie a website’s name to its public key.
  • Verifying a download’s hash is a small habit that catches corrupted and tampered files.
Stuck, or just want to talk it through? Ask in the Discord, no need to get stuck here alone.

Reflect on your own

  • Why should a website store a hash of your password instead of the password itself?
  • Someone says a secret is "encrypted in base64". What is wrong with that sentence?
  • Why does verifying a download's hash protect you even if the website itself was hacked? When does it not?

Go further

Spotted a mistake, or a command that did not work for you? Report it on GitHub or by email.