RFC 7516/7518 compliant JWE crypto provider for Swarmauri
Project description
Swarmauri Crypto JWE
JSON Web Encryption (JWE) provider implementing RFC 7516 and RFC 7518 compliant encryption and decryption helpers.
Features
- Asynchronous API for compact JWE serialization returning strings.
- Accepts
JWAAlgenums fromswarmauri_core.crypto.typesfor algorithms. - Supports
dir,RSA-OAEP,RSA-OAEP-256, andECDH-ESkey management algorithms. - Supports
A128GCM,A192GCM, andA256GCMcontent encryption. - Optional compression (
zip=DEF) and Additional Authenticated Data (AAD). - Returns structured decrypt results that include both the protected header and plaintext.
- Registers with the Swarmauri PluginManager via the
swarmauri.cryptosentry point.
Installation
pip install swarmauri_crypto_jwe
# or
poetry add swarmauri_crypto_jwe
# or, with uv
uv add swarmauri_crypto_jwe
[!TIP]
uvcan be installed withpip install uvor by following the instructions at astral.sh/uv. Once installed, runuv add swarmauri_crypto_jwefrom your project directory to add the dependency.
Usage
The helpers are asynchronous and return compact JWE strings that can be decrypted back into their original plaintext. A typical flow is:
- Generate or load the key material for the chosen algorithm.
- Instantiate
JweCrypto. - Call
encrypt_compactwith the payload, algorithm, and key details. - Call
decrypt_compactwith the resulting JWE and the corresponding private key.
import asyncio
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
from swarmauri_core.crypto.types import JWAAlg
from swarmauri_crypto_jwe import JweCrypto
async def main() -> None:
crypto = JweCrypto()
sk = rsa.generate_private_key(public_exponent=65537, key_size=2048)
pk_pem = sk.public_key().public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo,
)
jwe = await crypto.encrypt_compact(
payload=b"secret",
alg=JWAAlg.RSA_OAEP_256,
enc=JWAAlg.A256GCM,
key={"pub": pk_pem},
)
result = await crypto.decrypt_compact(
jwe,
rsa_private_pem=sk.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption(),
),
)
assert result.plaintext == b"secret"
asyncio.run(main())
Loading via PluginManager
from swarmauri.plugin import PluginManager
pm = PluginManager()
crypto = pm.load("swarmauri.cryptos", "JweCrypto")
Parameters
alg–JWAAlgmember describing the key management algorithm (JWAAlg.RSA_OAEP_256,JWAAlg.DIR, etc.).enc–JWAAlgmember describing the content encryption algorithm (JWAAlg.A256GCM,JWAAlg.A128GCM, etc.).key– mapping containing the key material used for encryption:{"k": bytes}for direct symmetric keys (dir).{"pub": rsa_public_key}for RSA OAEP, where the public key may be PEM bytes or anRSAPublicKeyinstance.{"pub": ec_public_key}for ECDH-ES with PEM, JWK, or key objects.
- Optional
header_extravalues are merged into the protected header (usezip="DEF"to enable compression). - Decryption requires the matching private key via
dir_key,rsa_private_pem/rsa_private_password, orecdh_private_key. expected_algsandexpected_encsconstrain acceptable algorithms during decryption, andaadmust match the authenticated data provided at encryption time.
Entry point
The provider is registered under the swarmauri.cryptos entry point as JweCrypto.
Want to help?
If you want to contribute to swarmauri-sdk, read up on our guidelines for contributing that will help you get started.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file swarmauri_crypto_jwe-0.3.0.dev4.tar.gz.
File metadata
- Download URL: swarmauri_crypto_jwe-0.3.0.dev4.tar.gz
- Upload date:
- Size: 11.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7c4b7afac70e9e18c0564b6a49e38b87c863c0b96e00ff9622d287c93da8ead0
|
|
| MD5 |
f29405f75f18e442feb5ab6d2e495184
|
|
| BLAKE2b-256 |
34616c5b37027a1a8c74fd8edd73bdfb09a99d2a0f91b8d4dd5873f9243772b2
|
File details
Details for the file swarmauri_crypto_jwe-0.3.0.dev4-py3-none-any.whl.
File metadata
- Download URL: swarmauri_crypto_jwe-0.3.0.dev4-py3-none-any.whl
- Upload date:
- Size: 12.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a28fe85eb23345e03a65990272b722d721f4d3e4155e3aaf96e641217d22ee1f
|
|
| MD5 |
8156215b1444db37989a2dfe7d0d833b
|
|
| BLAKE2b-256 |
ee94e2fbb939b090bce6d8c853689b3f7407cca5acf35af0088b93a1dbf97822
|