Skip to main content

PyCrypt

An educational cryptography library in plain Python. Everything is built from scratch so you can read it: the classic ciphers (Caesar, XOR) and the modern building blocks (SHA-256, MD5, HMAC and PBKDF2). The tests check every answer against Python's own hashlib and hmac, and against the official test vectors from each standard.

⚠️ For LEARNING, not real security. The Caesar and XOR ciphers and the homemade-style signatures here are easy to break. Never use PyCrypt to protect real passwords, messages, or secrets -- use a vetted library like cryptography for that.

What Is Cryptography?

Imagine you want to pass a secret note to your friend in class, but you don't want anyone else to read it. You could write it in a secret code that only you and your friend understand. That's cryptography!

Cryptography is the science of keeping information secret and safe. It's used every time you:

  • Send a text message
  • Log in to a website
  • Pay for something online
  • Download an app

PyCrypt teaches you how all of this works by building each piece from scratch.

Examples

Hashing -- One-Way Fingerprints

A hash turns any text into a fixed-size "fingerprint." You can't reverse it, but the same input always gives the same fingerprint.

from pycrypt.hashing import sha256

# Same input always gives the same hash
print(sha256("hello"))  # "2cf24dba5fb0a30e..."
print(sha256("hello"))  # "2cf24dba5fb0a30e..." (identical!)

# Even a tiny change makes a completely different hash
print(sha256("Hello"))  # "185f8db32271fe25..." (totally different!)

Caesar Cipher -- The Secret Decoder Ring

Shift every letter by a number. A becomes D (shift 3), B becomes E, and so on. It wraps around, so Z becomes C.

from pycrypt.caesar import encrypt, decrypt, brute_force

secret = encrypt("HELLO WORLD", shift=3)
print(secret)  # "KHOOR ZRUOG"

original = decrypt(secret, shift=3)
print(original)  # "HELLO WORLD"

# Try all 26 possible shifts to crack it!
for shift, text in brute_force(secret):
    print(f"Shift {shift}: {text}")

XOR Cipher -- Flipping Switches

XOR is its own inverse -- encrypt and decrypt use the exact same operation. Real ciphers like AES use XOR inside, but a short repeating key like ours is easy to break -- for learning only.

from pycrypt.xor import xor_encrypt, xor_encrypt_bytes

encrypted = xor_encrypt("secret message", key="mykey")
print(encrypted)  # looks like random bytes

# Decrypt by XOR-ing again with the same key
decrypted = xor_encrypt_bytes(encrypted, b"mykey").decode("utf-8")
print(decrypted)  # "secret message"

HMAC -- Tamper-Evident Seals

HMAC is like a wax seal on an envelope. If someone changes the message, the seal breaks.

from pycrypt.hmac import hmac_sign, hmac_verify

tag = hmac_sign("transfer $10", key="shared-secret")
print(hmac_verify("transfer $10", tag, key="shared-secret"))   # True
print(hmac_verify("transfer $1000", tag, key="shared-secret"))  # False!

Digital Signatures -- A Shared Wax Seal

A digital signature proves who wrote a message and that it hasn't been changed. Our version is like a wax seal that only you and your friend know how to make: you both share one secret key, and that same key is used to sign and to verify. (Real systems like RSA use a separate private key to sign and a public key anyone can verify with.)

import os
from pycrypt.signatures import sign, verify

# You and your friend share this one secret key.
key = os.urandom(32)

signature = sign("I wrote this!", key)
print(verify("I wrote this!", signature, key))       # True
print(verify("I didn't write this", signature, key))  # False

Features

  • Hashing -- SHA-256 and MD5 fingerprints
  • Caesar Cipher -- Shift-based encryption with brute-force cracking
  • XOR Cipher -- Symmetric encryption using bitwise XOR
  • Key Derivation -- Turn passwords into strong keys with PBKDF2
  • HMAC -- Tamper-proof message authentication codes
  • Digital Signatures -- Sign and verify messages with a shared secret key
  • CLI Tool -- A demo that prints hashing, Caesar, XOR, and HMAC examples
  • 100% Typed -- Full type annotations with strict Pyright checking

Quick Start

# Install with uv
uv add pycrypt

# Or install from source
git clone https://github.com/robcox2910/pycrypt.git
cd pycrypt
uv sync --all-extras

# Run the CLI demo
uv run pycrypt

# Run the tests
uv run pytest

Documentation

Full docs (with kid-friendly explanations of every idea): https://robcox2910.github.io/pycrypt/

Install

PyPI

pip install fromscratch-pycrypt

Or get the whole series at once: pip install fromscratch-pystack.

Releasing

Other projects in the series (PyStack) use a released version of this one from PyPI, not whatever is on main. To publish a change for them:

  1. Merge the change to main.
  2. Release it: gh release create vX.Y.Z --target main --generate-notes (or push a vX.Y.Z tag); the release workflow publishes it to PyPI. A change to the public API bumps the minor version (v0.1.0 → v0.2.0); anything else bumps the patch (v0.1.0 → v0.1.1).
  3. In each project that uses it, raise the fromscratch-pycrypt>=X.Y.Z floor in pyproject.toml, run uv lock --upgrade-package fromscratch-pycrypt, and open a pull request so CI checks the pair together.

This project is one of a series that builds real systems "from scratch in Python" to teach how they work. See the series home for the full story.

Project What it teaches Repository Docs PyPI
PyOS Operating systems py-os Docs fromscratch-pyos
Pebble Compilers and programming languages pebble-lang Docs fromscratch-pebble
PyDB Relational databases pydb Docs fromscratch-pydb
PyKV Key-value stores pykv Docs fromscratch-pykv
PyDocDB Document databases pydoc-db Docs fromscratch-pydocdb
PyGraphDB Graph databases pygraphdb Docs fromscratch-pygraphdb
PyTSDB Time-series databases pytsdb Docs fromscratch-pytsdb
PyVecDB Vector databases pyvecdb Docs fromscratch-pyvecdb
PyColDB Column-family databases pycoldb Docs fromscratch-pycoldb
PyCrypt Cryptography pycrypt Docs fromscratch-pycrypt
PyNet Computer networking pynet Docs fromscratch-pynet
PyWeb HTTP web servers pyweb Docs fromscratch-pyweb
PyGit Version control pygit Docs fromscratch-pygit
PySearch Search engines pysearch Docs fromscratch-pysearch
PyMQ Message queues pymq Docs fromscratch-pymq
PyLLM Language models (the brain) pyllm Docs fromscratch-pyllm
PyScheduler Job scheduling pyscheduler Docs fromscratch-pyscheduler
PyStack Full-stack integration pystack Docs fromscratch-pystack

License

MIT -- see LICENSE for details.

Metadata

Release files for fromscratch-pycrypt 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fromscratch-pycrypt 0.3.0
File Size Uploaded Provenance Actions
fromscratch_pycrypt-0.3.0.tar.gz 79.4 kB With provenance Details for fromscratch_pycrypt-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fromscratch-pycrypt 0.3.0
File Interpreter ABI Platform Actions
fromscratch_pycrypt-0.3.0-py3-none-any.whl Python 3 none any Details for fromscratch_pycrypt-0.3.0-py3-none-any.whl

Total release size: 97.8 kB

Release files / fromscratch_pycrypt-0.3.0.tar.gz

Download URL fromscratch_pycrypt-0.3.0.tar.gz
Size 79.4 kB
Tags Source
SHA-256 checksum
How to use checksums
973e6a0698a896fe17a012ec208f2d5509571f4aa1c6d70749b5ea7da1345c4f
BLAKE2b-256 checksum
How to use checksums
1425cebb39179e83791286227e4fe1f3791f310c9713dd69455639f10e127591
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 4, 2026.

Transparency log

Release files / fromscratch_pycrypt-0.3.0-py3-none-any.whl

Download URL fromscratch_pycrypt-0.3.0-py3-none-any.whl
Size 18.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f8d4aae23fd68bcac06659fe11189974b64b6b186c955ac5cd587ce54e23d065
BLAKE2b-256 checksum
How to use checksums
fd2d345433763bb64de9549440fdc608b5e5e3eb75f9f9d53c31ac50b95693bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 4, 2026.

Transparency log

Security for fromscratch-pycrypt 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for fromscratch_pycrypt-0.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 4, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page