Provides OpenPGP facilities using Sequoia-PGP library
Project description
PySequoia
Note: This is a work in progress. The API is not stable!
Building:
set -euxo pipefail
python3 -m venv .env
source .env/bin/activate
pip install maturin
maturin develop
Now open the console with python
and import the library:
from pysequoia import Cert
Assuming these keys and cards exist:
# generate a key with password
gpg --batch --pinentry-mode loopback --passphrase hunter22 --quick-gen-key passwd@example.com
gpg --batch --pinentry-mode loopback --passphrase hunter22 --export-secret-key passwd@example.com > passwd.pgp
# generate a key without password
gpg --batch --pinentry-mode loopback --passphrase '' --quick-gen-key no-passwd@example.com future-default
gpg --batch --pinentry-mode loopback --passphrase '' --export-secret-key no-passwd@example.com > no-passwd.pgp
# initialize dummy OpenPGP Card
sh /start.sh
echo 12345678 > pin
/root/.cargo/bin/opgpcard admin --card 0000:00000000 --admin-pin pin import no-passwd.pgp
Available functions
encrypt
Signs and encrypts a string to one or more recipients:
from pysequoia import encrypt
s = Cert.from_file("passwd.pgp")
r = Cert.from_bytes(open("wiktor.asc", "rb").read())
encrypted = encrypt(signer = s.signer("hunter22"), recipients = [r], content = "content to encrypt")
print(f"Encrypted data: {encrypted}")
sign
Signs the data and returns armored output:
from pysequoia import sign
s = Cert.from_file("signing-key.asc")
signed = sign(s.signer(), "data to be signed")
print(f"Signed data: {signed}")
decrypt
Decrypts data:
from pysequoia import decrypt
sender = Cert.from_file("no-passwd.pgp")
receiver = Cert.from_file("passwd.pgp")
content = "Red Green Blue"
encrypted = encrypt(signer = sender.signer(), recipients = [receiver], content = content)
print(f"Encrypted data: {encrypted}")
decrypted = decrypt(decryptor = receiver.decryptor("hunter22"), data = encrypted)
assert content == decrypted.content;
merge
Merges data from old certificate with new packets:
old = Cert.from_file("wiktor.asc")
new = Cert.from_file("wiktor-fresh.asc")
merged = old.merge(new)
print(f"Merged, updated cert: {merged}")
minimize
Note: This function is experimental and may be removed in the future.
Discards expired subkeys and User IDs:
from pysequoia import minimize
cert = Cert.from_file("wiktor.asc")
minimized = minimize(cert)
print(f"Minimized cert: {minimized}")
generate
Creates new general purpose key with given User ID:
alice = Cert.generate("Alice <alice@example.com>")
fpr = alice.fingerprint
print(f"Generated cert with fingerprint {fpr}:\n{alice}")
Newly generated certificates are usable in both encryption and signing contexts:
alice = Cert.generate("Alice <alice@example.com>")
bob = Cert.generate("Bob <bob@example.com>")
encrypted = encrypt(signer = alice.signer(), recipients = [bob], content = "content to encrypt")
print(f"Encrypted data: {encrypted}")
Certificate management
WKD
Fetching certificates via Web Key Directory:
from pysequoia import WKD
import asyncio
async def fetch_and_display():
cert = await WKD.search(email = "test-wkd@metacode.biz")
print(f"Cert found via WKD: {cert}")
assert cert.fingerprint == "5b7abe660d5c62a607fe2448716b17764e3fcaca"
asyncio.run(fetch_and_display())
Key server
Fetching certificates via HKPS protocol:
from pysequoia import KeyServer
import asyncio
async def fetch_and_display():
ks = KeyServer("hkps://keys.openpgp.org")
cert = await ks.get("653909a2f0e37c106f5faf546c8857e0d8e8f074")
print(f"Cert found via HKPS: {cert}")
assert cert.fingerprint == "653909a2f0e37c106f5faf546c8857e0d8e8f074"
asyncio.run(fetch_and_display())
CertD integration
The library exposes OpenPGP Certificate Directory integration which allows storing and retrieving OpenPGP certificates in a persistent way directly in the file system.
Note that this will not allow you to read GnuPG-specific key directories. Cert-D does not allow certificate removal.
from pysequoia import Store
cert = Cert.from_file("wiktor.asc")
s = Store("/tmp/store")
s.put(cert)
assert s.get(cert.fingerprint) != None
The certificate is now stored in the given directory and can be retrieved later by its fingerprint:
s = Store("/tmp/store")
assert s.get("653909a2f0e37c106f5faf546c8857e0d8e8f074") != None
OpenPGP Cards
There's an experimental feature allowing communication with OpenPGP Cards (like Yubikey or Nitrokey).
from pysequoia import Card
# enumerate all cards
all = Card.all()
# open card by card ident
card = Card.open("0000:00000000")
print(f"Card ident: {card.ident}")
print(f"Cardholder: {card.cardholder}")
Cards can be used for signing data:
signer = card.signer("123456")
signed = sign(signer, "data to be signed")
print(f"Signed data: {signed}")
As well as for decryption:
decryptor = card.decryptor("123456")
sender = Cert.from_file("passwd.pgp")
receiver = Cert.from_file("no-passwd.pgp")
content = "Red Green Blue"
encrypted = encrypt(signer = sender.signer("hunter22"), recipients = [receiver], content = content)
print(f"Encrypted data: {encrypted}")
decrypted = decrypt(decryptor = decryptor, data = encrypted)
assert content == decrypted.content;
Note that while this package allows using cards for signing and decryption the provisioning process is not supported. OpenPGP card tools can be used to initialize the card.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pysequoia-0.1.14.tar.gz
.
File metadata
- Download URL: pysequoia-0.1.14.tar.gz
- Upload date:
- Size: 3.2 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.14.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5bd3c0cb5df582bbff5539f8bb28ed6c81d7220d2130d33e1f0a2945f3cd93b9 |
|
MD5 | 50ee7bd5f081121ed4e20bdaa2032aa8 |
|
BLAKE2b-256 | 81cc1715890da389202811006ead5a402ebe37e04252f679972af9d449d99847 |
File details
Details for the file pysequoia-0.1.14-cp310-cp310-manylinux_2_34_x86_64.whl
.
File metadata
- Download URL: pysequoia-0.1.14-cp310-cp310-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 6.0 MB
- Tags: CPython 3.10, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/0.14.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 41a9a6717bf793f4fbdf35f24be09cdd4f0e8c0ec6da294052bab8ffc212341d |
|
MD5 | 8f589adec7d57168433d760d7b2fc1d0 |
|
BLAKE2b-256 | 077d85dea4a3820754e22abc058dc659fe5516d1d89144a996b172cfed008f92 |