15
Lesson 43 of 44

Hashing, encoding and basic security

Every program handles data that someone could tamper with or steal. In this lesson you will learn the basic tools of the standard library: hash functions to detect changes, HMAC to sign messages, key derivation with salt to store passwords, Base64, URL and HTML to encode data, and secure randomness and UUID. And, above all, the difference between encoding and encrypting.

  • 5 theory articles
  • 10 practice exercises
Start lesson

Fundamentals concepts 5

Hash functions with hashlib

A hash function takes data of any size and returns a fixed-size fingerprint . It is used to detect changes, index data and…

Theory
Read more

Integrity and signatures with HMAC

A hash detects accidental changes, but anyone can recompute it after tampering with the data. To check also who generated it you…

Theory
Read more

Storing passwords correctly

If a database leaks, whatever is stored in it is exposed. Passwords must be stored so that, even with the database in an…

Theory
Read more

Encoding is not encryption

Encoding changes the representation of some data so a channel can carry it; it hides nothing. Encrypting makes it unreadable…

Theory
Read more

Secure randomness and UUID

The random module generates pseudo-random numbers: they look random, but with the same seed they always produce the same…

Theory
Read more

Practice exercises 10

Hash fingerprint of a text

A hash function turns any data into a fixed-size fingerprint. The same text always gives the same fingerprint, and changing a…

Exercise Easy
Solve it

Check the integrity of a message

Fingerprints are used to detect that data has changed: the fingerprint is published with the data and whoever receives it…

Exercise Medium
Solve it

Encode and decode in Base64

Base64 represents any bytes with 64 printable characters, so they can be sent through channels that only accept text (email…

Exercise Medium
Solve it

Toy XOR cipher

Applying XOR with a repeated key is a very weak “cipher”, but it illustrates an important idea: XOR is its own inverse, so the…

Exercise Medium
Solve it

Sign messages with HMAC

A normal hash can be computed by anyone. An HMAC combines the message with a secret key : only someone who knows the key can…

Exercise Hard
Solve it

Store passwords with salt

A password must never be stored as is or with a simple hash. What is stored is the result of a slow derivation function such as…

Exercise Hard
Solve it

Take a URL apart

URLs have a precise structure, and splitting them with split or with home-made regular expressions is a classic source of bugs…

Exercise Medium
Solve it

Escape HTML against injections

If you insert a user's text into an HTML page without escaping it, that text can contain tags or scripts ( XSS injection )…

Exercise Medium
Solve it

Identifiers with UUID

A UUID is a practically unique 128-bit identifier. Version 4 ones are random; version 5 ones ( uuid5 ) are computed with a hash…

Exercise Medium
Solve it

Break a weak hash

A fast hash and a small secret are a bad combination: if there are only 10,000 possible PINs, an attacker tries them all in…

Exercise Hard
Solve it