Skip to main content

High-performance token and crypto utilities written in Rust with PyO3

Project description

CipherToken

CipherToken is a modern, high-performance Python library for cryptography and JWT (JSON Web Token) management.
It is implemented in Rust using PyO3, providing both speed and security for generating, encoding, decoding, and verifying tokens.

The library is designed to be modular, organized into submodules for secret management, time utilities, algorithms, and token handling.
It supports both synchronous and asynchronous operations


Library Overview

CipherToken is structured into the following main parts:

1. secret Module

Handles key generation for symmetric (HMAC) and asymmetric (RSA) algorithms:

  • secret_key() – Generate a random HMAC key (default 32 bytes)
  • secret_key_with_size(size: int) – Generate HMAC key with a custom size
  • generate_hmac_secret(size: int) – Generate an HMAC secret key
  • generate_hmac_secret_async(size: int) – Async version of HMAC key generation
  • generate_rsa_keypair(bits: Optional[int] = 2048) – Generate RSA private/public key pair

2. time Module

Provides utility functions for time conversion and timestamps:

  • now() – Current UNIX timestamp
  • seconds(n: int) – Convert n seconds to seconds
  • minutes(n: int) – Convert n minutes to seconds
  • hours(n: int) – Convert n hours to seconds
  • days(n: int) – Convert n days to seconds
  • weeks(n: int) – Convert n weeks to seconds

3. utils Module

Contains useful constants:

  • DEFAULT_SECRET_SIZE – Default key size
  • MIN_SECRET_SIZE – Minimum allowed key size
  • TOKEN_ACCESS – Access token type
  • TOKEN_REFRESH – Refresh token type

4. algorithms Module

Defines algorithm constants for token signing:

  • HMAC: HS256, HS384, HS512
  • RSA: RS256, RS384, RS512
  • ECDSA: ES256, ES384
  • RSA-PSS: PS256, PS384, PS512
  • Edwards Curve: EDDSA

5. CipherToken Class

Main class for managing JWTs:

  • Constructor:
from ciphertoken import CipherToken

CipherToken(secret: str, algorithm: str, access_ttl: int, refresh_ttl: int)

Synchronous methods:

access(user_id: int, extra_payload: Optional[dict] = None) -> str
refresh(user_id: int, extra_payload: Optional[dict] = None) -> str
decode(token: str) -> dict
verify(token: str) -> bool
rotation(refresh_token: str, extra_payload: Optional[dict] = None) -> Tuple[str, str]
inspect(token: str) -> dict
remaining_time(token: str) -> Optional[int]
algorithm_name() -> str

Asynchronous methods:

await access_async(user_id: int, extra_payload: Optional[dict] = None) -> str
await refresh_async(user_id: int, extra_payload: Optional[dict] = None) -> str
await decode_async(token: str) -> dict
await verify_async(token: str) -> bool
await rotation_async(refresh_token: str, extra_payload: Optional[dict] = None) -> Tuple[str, str]

Helper Functions:

is_jwt_format(token: str) – Check if a string is a valid JWT

validate_jwt_format(token: str) – Validate JWT structure

Installation

CipherToken is available via PyPI. Install it with:

pip install ciphertoken


Usage Example

from ciphertoken.secret import secret_key
from ciphertoken import CipherToken
from ciphertoken.time import minutes, days
from ciphertoken.utils import TOKEN_REFRESH
from ciphertoken.algorithms import HS256

# Generate a secret key
key = secret_key()

# Create a CipherToken instance
token = CipherToken(secret=key, algorithm=HS256, access_ttl=minutes(10), refresh_ttl=days(7))

# Generate access and refresh tokens
access_token = token.access(user_id=123)
refresh_token = token.refresh(user_id=123)

# Verify and decode tokens
payload = token.decode(access_token)
is_valid = token.verify(access_token)

# Async usage
import asyncio

async def generate_async_token():
    access_token = await token.access_async(user_id=123)
    payload = await token.decode_async(access_token)
    return payload

asyncio.run(generate_async_token())

Implementation Notes

  • Built in Rust using PyO3 for maximum speed and memory safety

  • Supports synchronous and asynchronous workflows

  • Fully modular: secret, time, utils, algorithms, and CipherToken class

  • Secure key handling with HMAC and RSA support

  • Easily extendable for additional algorithms

Summary

CipherToken is a modern, Rust-backed Python library for authentication and JWT management. It is ideal for web APIs, microservices, or any system that requires secure token generation, verification, and rotation.

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

ciphertoken-0.1.2.tar.gz (3.5 MB view details)

Uploaded Source

Built Distributions

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

ciphertoken-0.1.2-cp312-cp312-win_amd64.whl (1.0 MB view details)

Uploaded CPython 3.12Windows x86-64

ciphertoken-0.1.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

ciphertoken-0.1.2-cp312-cp312-macosx_11_0_arm64.whl (996.5 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

ciphertoken-0.1.2-cp310-cp310-win_amd64.whl (1.0 MB view details)

Uploaded CPython 3.10Windows x86-64

ciphertoken-0.1.2-cp310-cp310-manylinux_2_34_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.34+ x86-64

ciphertoken-0.1.2-cp310-cp310-macosx_11_0_arm64.whl (997.2 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

Details for the file ciphertoken-0.1.2.tar.gz.

File metadata

  • Download URL: ciphertoken-0.1.2.tar.gz
  • Upload date:
  • Size: 3.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ciphertoken-0.1.2.tar.gz
Algorithm Hash digest
SHA256 58440d539326fcb760c2fc802bda60d7f80831f9cdccb812d67f29b5f0cd8baf
MD5 90c374fd5dc026b658f462963818fd3d
BLAKE2b-256 75c2775d023a931cd3a147780b671b5e652248e7d5f20884cffdb7afc7e73e8a

See more details on using hashes here.

Provenance

The following attestation bundles were made for ciphertoken-0.1.2.tar.gz:

Publisher: build.yml on cipherunits/CipherToken

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ciphertoken-0.1.2-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for ciphertoken-0.1.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 12c1c3a1ec9dce3c9b70f091c3d158de20f791cd80bdeee94309ef4ee0e60c48
MD5 4f8a6fe7a5015cdd93b64ea05a0b06e4
BLAKE2b-256 11a1458648583c86e11718393c48e36bd60841d6afb1e48e2ac6b8738f701eb3

See more details on using hashes here.

Provenance

The following attestation bundles were made for ciphertoken-0.1.2-cp312-cp312-win_amd64.whl:

Publisher: build.yml on cipherunits/CipherToken

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ciphertoken-0.1.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for ciphertoken-0.1.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bcb2f51d3a44747cb1ee80e1c21dd050d328d3703d4d81278735db2e615573ea
MD5 6736594a03847d52a35e0c9a9e91a2ee
BLAKE2b-256 059ee5e66ec0e380091c75a6ea833ea3a7e81e709ae097ea9e6f7c50ef25f5ac

See more details on using hashes here.

Provenance

The following attestation bundles were made for ciphertoken-0.1.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build.yml on cipherunits/CipherToken

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ciphertoken-0.1.2-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ciphertoken-0.1.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 46c226d0ca2db9baf6aff150ad826ca7c72a20303d5185d424d7f7914f43c912
MD5 a03a45bbd6221bc7cc8d7229450ecc09
BLAKE2b-256 a20cdb41a019bda75ffe953f5b1960fa47c4c5f8ac28a444af25564734ddaafc

See more details on using hashes here.

Provenance

The following attestation bundles were made for ciphertoken-0.1.2-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: build.yml on cipherunits/CipherToken

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ciphertoken-0.1.2-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for ciphertoken-0.1.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 f9d320a45dd6acebcc3034446e00e066f925eba1b018da852bdb8cef73da5896
MD5 0401e99eb3b3de0722493fefc71c3636
BLAKE2b-256 9405c79161a607ce5fe0488c0d415fac376294cdca5fdba5cdfbafd42dc791f8

See more details on using hashes here.

Provenance

The following attestation bundles were made for ciphertoken-0.1.2-cp310-cp310-win_amd64.whl:

Publisher: build.yml on cipher-unit/CipherToken

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ciphertoken-0.1.2-cp310-cp310-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for ciphertoken-0.1.2-cp310-cp310-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 da58687abb616c289e5466271bf72bfec79ec8f3a3272976c80c6ef503ae0277
MD5 c72889fcfbb7d5b1cc7b71ebf16c331f
BLAKE2b-256 e5579143df59908966fba69eebd76d0c53d57b93f9df4f1a11cb6f947d5f21d3

See more details on using hashes here.

Provenance

The following attestation bundles were made for ciphertoken-0.1.2-cp310-cp310-manylinux_2_34_x86_64.whl:

Publisher: build.yml on cipher-unit/CipherToken

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ciphertoken-0.1.2-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ciphertoken-0.1.2-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 35df04f0cd83f43f6ccab68225fb9aebab7ab3b762b751fff618f37458877513
MD5 7091457a9d39e1d397988a0b80c2ecb4
BLAKE2b-256 bae53c9b645ea67eb159469a7d90dc9c3fc6a5242a62debe14a1a861eb35edb7

See more details on using hashes here.

Provenance

The following attestation bundles were made for ciphertoken-0.1.2-cp310-cp310-macosx_11_0_arm64.whl:

Publisher: build.yml on cipher-unit/CipherToken

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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