Lab 07: Cryptography & Security

Objective

Apply production security patterns using Python's standard library: secure hashing with hashlib, HMAC message authentication, secrets for cryptographic tokens, PBKDF2 password hashing, base64 encoding for API payloads, and data integrity with checksums.

Background

Security is not optional. Every web application needs: password hashing (never store plaintext), API authentication (HMAC or tokens), data integrity verification (checksums), and secure randomness (secrets, not random). Python's stdlib covers all of these without third-party libraries.

Time

30 minutes

Prerequisites

  • Lab 06 (ctypes/Binary)

Tools

  • Docker: zchencow/innozverse-python:latest


Lab Instructions

Step 1: Hashing — SHA-256, SHA-3, BLAKE2

💡 Never use MD5 or SHA-1 for security — they're broken for collision resistance. Use SHA-256 or BLAKE2b for general hashing, PBKDF2/bcrypt/Argon2 for passwords. The secrets module uses OS-level cryptographic randomness (/dev/urandom), which is much stronger than random.random() which is deterministic given a known seed.

📸 Verified Output:


Step 2: HMAC — Message Authentication

📸 Verified Output:


Steps 3–8: Secure Tokens, Password Hashing, JWT-style Tokens, Rate Limiting, Audit Log, Capstone

📸 Verified Output:


Summary

Need
Tool
Why

Hash data

hashlib.sha256

Fingerprint, integrity

Hash passwords

hashlib.pbkdf2_hmac

Slow by design, salted

Authenticate messages

hmac.new + compare_digest

Constant-time HMAC

Generate tokens

secrets.token_hex/urlsafe

OS-level randomness

Generate OTPs

secrets.randbelow(10**6)

Cryptographically secure

Sign payloads

HMAC-SHA256

API webhooks, JWT

Integrity chain

Hash-linked audit log

Tamper evidence

Further Reading

Last updated