Skip to main content

PyCrypt

An educational cryptography library in plain Python. The classic ciphers (Caesar, XOR) are built from scratch; the modern primitives (SHA-256, HMAC, PBKDF2) wrap Python's battle-tested hashlib/hmac standard library so you can see how the pieces fit together.

⚠️ 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/

Releasing

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

  1. Merge the change to main.
  2. Tag it: git tag -a vX.Y.Z -m vX.Y.Z && git push origin vX.Y.Z. 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, edit the tag in [tool.uv.sources], run uv lock --upgrade-package <name>, 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
PyOS Operating systems py-os Docs
Pebble Compilers and programming languages pebble-lang Docs
PyDB Relational databases pydb Docs
PyKV Key-value stores pykv Docs
PyDocDB Document databases pydoc-db Docs
PyGraphDB Graph databases pygraphdb Docs
PyTSDB Time-series databases pytsdb Docs
PyVecDB Vector databases pyvecdb Docs
PyColDB Column-family databases pycoldb Docs
PyCrypt Cryptography pycrypt Docs
PyNet Computer networking pynet Docs
PyWeb HTTP web servers pyweb Docs
PyGit Version control pygit Docs
PySearch Search engines pysearch Docs
PyMQ Message queues pymq Docs
PyLLM Language models (the brain) pyllm Docs
PyScheduler Job scheduling pyscheduler Docs
PyStack Full-stack integration pystack Docs

License

MIT -- see LICENSE for details.

Metadata

Release files for fromscratch-pycrypt 0.2.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.2.0
File Size Uploaded Provenance
fromscratch_pycrypt-0.2.0.tar.gz 70.8 kB With provenance Details

Built distribution (wheel)

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

Total release size: 84.1 kB

Release files / fromscratch_pycrypt-0.2.0.tar.gz

Download URL fromscratch_pycrypt-0.2.0.tar.gz
Size 70.8 kB
Tags Source
SHA-256 checksum
How to use checksums
4497a39b5e6d380e24d3681ee340038bee0ffa507a00b1c70371bcda2d4825fb
BLAKE2b-256 checksum
How to use checksums
5234996a121630ae21aee86381c945bb8ac59a90e5f75b860b29cdcb7e605565
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 Oct 26, 2026.

Transparency log

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

Download URL fromscratch_pycrypt-0.2.0-py3-none-any.whl
Size 13.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a81117449b551abd0cefa3a996ce332607429fdedd4f2580c19b8e42c8cdf70a
BLAKE2b-256 checksum
How to use checksums
1cc67b9929f6d6a3ce4d68b17196d2e15d7c6689ad708d37ac1a59f14b505445
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 Oct 26, 2026.

Transparency log

Security for fromscratch-pycrypt 0.2.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

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for fromscratch_pycrypt-0.2.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 Oct 26, 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.2.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