Skip to main content

Algorithm-routing crypto provider for Swarmauri

Project description

Swarmauri Logo

PyPI - Downloads Hits PyPI - Python Version PyPI - License PyPI - swarmauri_crypto_composite


Swarmauri Crypto Composite

CompositeCrypto is an algorithm-routing crypto provider that delegates encryption operations to the first child provider that advertises support for the requested algorithm. Its behaviour is defined entirely by the wrapped providers:

  • Aggregates each provider's supports() capabilities and removes duplicates.
  • Normalises requested algorithms before routing so stylistic variants still match.
  • Requires at least one child provider to be supplied at construction time.
  • Exposes the full asynchronous ICrypto surface area (encrypt, wrap, seal, etc.).

Installation

Choose the tool that best fits your workflow:

pip install swarmauri_crypto_composite
poetry add swarmauri_crypto_composite
uv add swarmauri_crypto_composite

Usage

"""Route crypto operations to the provider that supports the requested algorithm."""
import asyncio

from swarmauri_crypto_composite import CompositeCrypto
from swarmauri_core.crypto.ICrypto import ICrypto
from swarmauri_core.crypto.types import (
    AEADCiphertext,
    ExportPolicy,
    KeyRef,
    KeyType,
    KeyUse,
)


class DummyCrypto(ICrypto):
    def __init__(self, name: str, alg: str) -> None:
        self._name = name
        self._alg = alg

    def supports(self):
        return {"encrypt": (self._alg,)}

    async def encrypt(self, key, pt, *, alg=None, aad=None, nonce=None):
        return AEADCiphertext(
            kid="dummy",
            version=1,
            alg=alg or self._alg,
            nonce=b"",
            ct=self._name.encode(),
            tag=b"",
        )

    async def decrypt(self, key, ct, *, aad=None):  # pragma: no cover - demo only
        raise NotImplementedError

    async def wrap(self, kek, *, dek=None, wrap_alg=None, nonce=None):  # pragma: no cover - demo only
        raise NotImplementedError

    async def unwrap(self, kek, wrapped):  # pragma: no cover - demo only
        raise NotImplementedError

    async def seal(self, recipient, pt, *, alg=None):  # pragma: no cover - demo only
        raise NotImplementedError

    async def unseal(self, recipient_priv, sealed, *, alg=None):  # pragma: no cover - demo only
        raise NotImplementedError


async def main() -> None:
    # Compose two providers that advertise different algorithms.
    chacha = DummyCrypto("chacha", "CHACHA20-POLY1305")
    aes = DummyCrypto("aes", "A256GCM")
    composite = CompositeCrypto([chacha, aes])

    key = KeyRef(
        kid="k",
        version=1,
        type=KeyType.SYMMETRIC,
        uses=(KeyUse.ENCRYPT, KeyUse.DECRYPT),
        export_policy=ExportPolicy.SECRET_WHEN_ALLOWED,
        material=b"\x00" * 32,
    )

    ciphertext = await composite.encrypt(key, b"payload", alg="A256GCM")
    print(f"Selected provider: {ciphertext.ct.decode()}")


if __name__ == "__main__":
    asyncio.run(main())

Entry point

The provider is registered under the swarmauri.cryptos entry-point as CompositeCrypto.

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_composite-0.2.0.dev11.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file swarmauri_crypto_composite-0.2.0.dev11.tar.gz.

File metadata

  • Download URL: swarmauri_crypto_composite-0.2.0.dev11.tar.gz
  • Upload date:
  • Size: 8.1 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_composite-0.2.0.dev11.tar.gz
Algorithm Hash digest
SHA256 d2dfb7dbaa507b8c6aeeb4a5d3999bb613cbc44e5ac64c5fff7ba520288cc435
MD5 d3644e234308f2f47591fc6a98cc4372
BLAKE2b-256 3c2447d7db968baaf0b56c662d3cb559741631be42e45cf94f0786859dcbbf20

See more details on using hashes here.

File details

Details for the file swarmauri_crypto_composite-0.2.0.dev11-py3-none-any.whl.

File metadata

  • Download URL: swarmauri_crypto_composite-0.2.0.dev11-py3-none-any.whl
  • Upload date:
  • Size: 9.2 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_composite-0.2.0.dev11-py3-none-any.whl
Algorithm Hash digest
SHA256 d09fb64991fafaae65adfd79c6a8408360f7e3fd886020e24fc14d7850423d67
MD5 d44cf0be53fc7eaa5cad9ad6d05f6f1b
BLAKE2b-256 622702da6bcccc8939f717cc86901b778026b9ab1840cedc2a244c528c2c0e1b

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