Skip to main content

A pure Python implementation of cryptographic primitives, written in a clean, Pythonic, and type-safe way.

Project description

pycrypt

A pure Python implementation of cryptographic primitives, written in a clean, Pythonic, and type-safe way.

Python 3.9+ MIT License Release Build status PyPI version

⚠️ Disclaimer: pycrypt is an educational cryptography library. It is not safe for production use. Use only for learning how cryptographic algorithms work under the hood.

Do not roll your own crypto in production code. Use a safe, audited library that has been vetted by professionals.

Overview

pycrypt implements major cryptographic primitives from scratch in pure Python with minimal dependencies. It is designed for learners and developers interested in the inner workings of cryptography.

Features

Category Algorithm Description
Asymmetric RSA OAEP encryption/decryption, PSS signing/verification
Diffie–Hellman (DH) Modular exponentiation and HKDF-based key derivation
Symmetric AES ECB, CBC, CTR, and GCM modes
Hashing SHA-1, SHA-256 HMAC and HKDF included

Project Structure

pycrypt/
├── asymmetric/
│   ├── dh/
│   │   ├── core.py
│   │   ├── groups.py
│   │   └── keyformat.py
│   └── rsa/
│       ├── core.py
│       ├── keyformat.py
│       └── utils.py
├── hash/
│   ├── sha/
│   │   ├── core.py
│   │   ├── hmac.py
│   │   └── variants.py
├── symmetric/
│   └── aes/
│       ├── core.py
│       ├── modes.py
│       └── utils.py
├── utils/
│   ├── asn1.py
│   ├── padding.py
│   └── utils.py
└── main.py

Installation

pip install pycrypt-lib

Examples

Diffie–Hellman (DH) Key Exchange

from pycrypt.asymmetric import DH

params = DH.generate_parameters(2048)

alice_priv = params.generate_private_key()
bob_priv = params.generate_private_key()

alice_shared = alice_priv.exchange(bob_priv.public_key())
bob_shared = bob_priv.exchange(alice_priv.public_key())

assert alice_shared == bob_shared
print(f"Shared secret: {alice_shared.hex()}")

RSA Encryption and Signing

from pycrypt.asymmetric import RSAKey

key = RSAKey.generate(2048)
message = b"Hello RSA!"

cipher = key.oaep_encrypt(message)
plain = key.oaep_decrypt(cipher)

signature = key.pss_sign(message)
assert key.pss_verify(message, signature)

AES (GCM Mode)

from secrets import token_bytes
from pycrypt.symmetric import AES_GCM

key = token_bytes(16)
nonce = token_bytes(12)

aes = AES_GCM(key)
ciphertext, tag = aes.encrypt(b"Top Secret", nonce=nonce)
plaintext = aes.decrypt(ciphertext, nonce=nonce, tag=tag)

print(plaintext.decode())

SHA-256 Hash

from pycrypt.hash import SHA256

sha = SHA256()
sha.update(b"hello world")
print(sha.hexdigest())

License

pycrypt is licensed under the MIT License.

See LICENSE for the full text.

⚠️ Note: This library is not secure for production use. It is a learning and exploration tool only.

Links

Cryptography Reference Standards

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pycrypt_lib-1.1.4.tar.gz (28.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pycrypt_lib-1.1.4-py3-none-any.whl (35.2 kB view details)

Uploaded Python 3

File details

Details for the file pycrypt_lib-1.1.4.tar.gz.

File metadata

  • Download URL: pycrypt_lib-1.1.4.tar.gz
  • Upload date:
  • Size: 28.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pycrypt_lib-1.1.4.tar.gz
Algorithm Hash digest
SHA256 462ccb797ef7be4459e99f0d56fb9a14b7c5891d01258438f7d6eb04d63cc97d
MD5 82ba271cb20ea69679f49fdc9a8f2041
BLAKE2b-256 e6672e4e9d920fe82c0e6bc513e854e332d5e76698e80ca30e0b17146b60e480

See more details on using hashes here.

File details

Details for the file pycrypt_lib-1.1.4-py3-none-any.whl.

File metadata

  • Download URL: pycrypt_lib-1.1.4-py3-none-any.whl
  • Upload date:
  • Size: 35.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pycrypt_lib-1.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 85220766b60f6c2fdcb9233f8a9961dc06aae21ccdfca374fa66f0b01567b35e
MD5 ffbde08a739e20ee04a746a8f575274b
BLAKE2b-256 3bb64aeb8bd8cebe424ae06f923fe3545264fda8f3a09401b0c23cb0bcee53a3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page