Skip to main content

CryptoServe SDK - Zero-config cryptographic operations with auto-registration and local key caching

Project description

CryptoServe SDK

Zero-config cryptographic operations with managed keys and auto-registration.

Installation

pip install cryptoserve

Quick Start (Recommended)

# One-time login (stores credentials locally)
cryptoserve login
from cryptoserve import CryptoServe

# Initialize - auto-registers your app on first use
crypto = CryptoServe(
    app_name="my-service",
    team="platform",
    environment="development"
)

# Encrypt/Decrypt
encrypted = crypto.encrypt(b"sensitive data", context="user-pii")
decrypted = crypto.decrypt(encrypted, context="user-pii")

# Sign/Verify
signature = crypto.sign(b"document", key_id="signing-key")
is_valid = crypto.verify_signature(b"document", signature, key_id="signing-key")

# Hash and MAC
hash_hex = crypto.hash(b"data", algorithm="sha256")
mac_hex = crypto.mac(b"message", key=secret_key, algorithm="hmac-sha256")

CryptoServe Class

The CryptoServe class provides:

Method Description
encrypt(plaintext, context) Encrypt binary data
decrypt(ciphertext, context) Decrypt binary data
encrypt_string(text, context) Encrypt string (returns base64)
decrypt_string(ciphertext, context) Decrypt to string
encrypt_json(obj, context) Encrypt JSON object
decrypt_json(ciphertext, context) Decrypt to JSON
sign(data, key_id) Create digital signature
verify_signature(data, signature, key_id) Verify signature
hash(data, algorithm) Compute cryptographic hash
mac(data, key, algorithm) Compute MAC
health_check() Verify connection
cache_stats() Get cache performance stats
invalidate_cache(context) Clear cached keys

Performance Features

CryptoServe SDK includes built-in performance optimizations:

Local Key Caching

Keys are cached locally to reduce network round-trips:

Metric Value
Server round-trip ~90ms
Cached operation ~0.3ms avg
Min latency 0.009ms
Speedup ~250x
Cache hit rate 90%+ (after warmup)
from cryptoserve import CryptoServe

# Enable caching (default: enabled)
crypto = CryptoServe(
    app_name="my-service",
    team="platform",
    enable_cache=True,   # Default: True
    cache_ttl=300.0,     # 5 minutes (default)
    cache_size=100,      # Max cached keys (default)
)

# First call fetches key from server and caches it
encrypted = crypto.encrypt(b"data", context="user-pii")  # ~90ms

# Subsequent calls use cached key (local AES-256-GCM)
encrypted = crypto.encrypt(b"more data", context="user-pii")  # ~0.3ms

Cache Statistics

Monitor cache performance:

stats = crypto.cache_stats()
print(f"Hit rate: {stats['hit_rate']:.1%}")
print(f"Hits: {stats['hits']}, Misses: {stats['misses']}")
print(f"Cache size: {stats['size']}")

Cache Invalidation

Invalidate cached keys (e.g., after key rotation):

# Invalidate specific context
crypto.invalidate_cache("user-pii")

# Invalidate all cached keys
crypto.invalidate_cache()

Health Check

from cryptoserve import CryptoServe

crypto = CryptoServe(app_name="my-service")

if crypto.health_check():
    print("Connected!")
else:
    print("Connection failed")

FastAPI Integration

from cryptoserve import CryptoServe
from cryptoserve.fastapi import configure, EncryptedStr
from pydantic import BaseModel

# Configure once at startup
crypto = CryptoServe(app_name="my-api", team="platform")
configure(crypto)

class User(BaseModel):
    name: str
    email: EncryptedStr["user-pii"]  # Automatically encrypted

SQLAlchemy Integration

from cryptoserve import CryptoServe
from cryptoserve.fastapi import configure, EncryptedString
from sqlalchemy import Column, Integer

# Configure once at startup
crypto = CryptoServe(app_name="my-api", team="platform")
configure(crypto)

class User(Base):
    id = Column(Integer, primary_key=True)
    email = Column(EncryptedString(context="user-pii"))

Auto-Protect (Third-Party Libraries)

from cryptoserve import auto_protect

auto_protect(encryption_key=key)

# Now all outbound requests are automatically protected
import requests
requests.post(url, json={"email": "user@example.com"})  # Auto-encrypted

CLI

# Interactive context wizard
python -m cryptoserve wizard

# Verify SDK health
python -m cryptoserve verify

# Show identity info
python -m cryptoserve info

Package Architecture

CryptoServe uses a modular architecture for flexibility:

Package Purpose Install
cryptoserve Full SDK with managed keys pip install cryptoserve
cryptoserve-core Pure crypto primitives pip install cryptoserve-core
cryptoserve-client API client only pip install cryptoserve-client
cryptoserve-auto Auto-protect libraries pip install cryptoserve-auto

Use cases:

  • Most users: Install cryptoserve for the full experience
  • Bring your own keys: Install cryptoserve-core only
  • Custom integration: Install cryptoserve-client for API access
  • Dependency protection: Add cryptoserve-auto for automatic protection

Error Handling

from cryptoserve import (
    CryptoServe,
    AuthenticationError,
    AuthorizationError,
    ContextNotFoundError,
)

crypto = CryptoServe(app_name="my-app", team="platform")

try:
    ciphertext = crypto.encrypt(data, context="user-pii")
except AuthenticationError:
    # Token expired or invalid
    pass
except AuthorizationError:
    # Not allowed to use this context
    pass
except ContextNotFoundError:
    # Context doesn't exist
    pass

License

Apache 2.0

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

cryptoserve-1.0.0.tar.gz (74.4 kB view details)

Uploaded Source

Built Distribution

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

cryptoserve-1.0.0-py3-none-any.whl (70.8 kB view details)

Uploaded Python 3

File details

Details for the file cryptoserve-1.0.0.tar.gz.

File metadata

  • Download URL: cryptoserve-1.0.0.tar.gz
  • Upload date:
  • Size: 74.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.12

File hashes

Hashes for cryptoserve-1.0.0.tar.gz
Algorithm Hash digest
SHA256 ca5a23e92f6e2170b0f12d6c71c32f578ec12de7446085e68d140e2bfa6ad228
MD5 35c2274028e6afbbaa1f090a31f98f66
BLAKE2b-256 a8c99c53992b8049957e75c501c43be47ce9e696f5dd06ff0c0669989f7d7d42

See more details on using hashes here.

File details

Details for the file cryptoserve-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: cryptoserve-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 70.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.12

File hashes

Hashes for cryptoserve-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bb3576d0ff1fe199fcb987a70371f5750100b69a21768c3db3fad23954c6a48d
MD5 005044e2fd6cd5f3b99c148441fd569c
BLAKE2b-256 74ca0afa186740fc782a95e1317a0f58ded7ec7ba979451a89476f6d65005ff5

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