Skip to main content

RFC 7516/7518 compliant JWE crypto provider for Swarmauri

Project description

Swarmauri Logo

PyPI - Downloads Hits PyPI - Python Version PyPI - License PyPI - swarmauri_crypto_jwe


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 JWAAlg enums from swarmauri_core.crypto.types for algorithms.
  • Supports dir, RSA-OAEP, RSA-OAEP-256, and ECDH-ES key management algorithms.
  • Supports A128GCM, A192GCM, and A256GCM content 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.cryptos entry point.

Installation

pip install swarmauri_crypto_jwe
# or
poetry add swarmauri_crypto_jwe
# or, with uv
uv add swarmauri_crypto_jwe

[!TIP] uv can be installed with pip install uv or by following the instructions at astral.sh/uv. Once installed, run uv add swarmauri_crypto_jwe from 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:

  1. Generate or load the key material for the chosen algorithm.
  2. Instantiate JweCrypto.
  3. Call encrypt_compact with the payload, algorithm, and key details.
  4. Call decrypt_compact with 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

  • algJWAAlg member describing the key management algorithm (JWAAlg.RSA_OAEP_256, JWAAlg.DIR, etc.).
  • encJWAAlg member 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 an RSAPublicKey instance.
    • {"pub": ec_public_key} for ECDH-ES with PEM, JWK, or key objects.
  • Optional header_extra values are merged into the protected header (use zip="DEF" to enable compression).
  • Decryption requires the matching private key via dir_key, rsa_private_pem/rsa_private_password, or ecdh_private_key.
  • expected_algs and expected_encs constrain acceptable algorithms during decryption, and aad must 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


Download files

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

Source Distribution

swarmauri_crypto_jwe-0.2.0.dev31.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

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

swarmauri_crypto_jwe-0.2.0.dev31-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file swarmauri_crypto_jwe-0.2.0.dev31.tar.gz.

File metadata

  • Download URL: swarmauri_crypto_jwe-0.2.0.dev31.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.3 {"installer":{"name":"uv","version":"0.10.3","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 swarmauri_crypto_jwe-0.2.0.dev31.tar.gz
Algorithm Hash digest
SHA256 a8f7107cf7ef0ee73cc5ad12533d7b4d43b0e6ebabaf227ee58d8cb9e80b7ff7
MD5 5106b8ff05e7ef5c2d344df5ea69fccf
BLAKE2b-256 01a35d7500f24d8a9ef9b79a790d9a8679824c8872de6e1137427439f77ce233

See more details on using hashes here.

File details

Details for the file swarmauri_crypto_jwe-0.2.0.dev31-py3-none-any.whl.

File metadata

  • Download URL: swarmauri_crypto_jwe-0.2.0.dev31-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.3 {"installer":{"name":"uv","version":"0.10.3","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 swarmauri_crypto_jwe-0.2.0.dev31-py3-none-any.whl
Algorithm Hash digest
SHA256 42318c6c3fadae174d2ccb23945e49f78d9e61c9d5bd81cea8649a628b863500
MD5 06b4a860c5e2b5be2566d92e35560da0
BLAKE2b-256 96c17e5b1f7a161f41db216aef6f5aa8c2cb84ca07ff4571c68374866b83d695

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