Skip to main content

FAST format-preserving encryption cipher in pure Python

Project description

FAST format-preserving cipher for Python

A pure Python implementation of the FAST cipher, a format-preserving encryption (FPE) scheme designed for tokenizing API keys, credentials, and other structured secrets.

For prefix-based tokens (GitHub, AWS, Stripe, etc.), encrypted output keeps the exact same format (length, prefix, character set) as the originals, so they pass through systems that validate token formats. Heuristic tokens (Fastly, AWS secret keys) are wrapped in a tagged marker since they have no distinguishing prefix.

Fully interoperable with the C, Zig, Go, and JavaScript implementations.

Installation

pip install fast-cipher

Or with uv:

uv add fast-cipher

Quick start

The most common use case is encrypting tokens and API keys found inside a block of text. TokenEncryptor handles scanning, encrypting, and decrypting automatically:

import os
from fast_cipher.tokens import TokenEncryptor

key = os.urandom(32)  # AES-128, AES-192, or AES-256
encryptor = TokenEncryptor(key)

text = """
Here are the credentials for the staging environment:
GitHub token: ghp_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghij
AWS access key: AKIAIOSFODNN7EXAMPLE
Stripe key: sk_live_ABCDEFGHIJKLMNOPQRSTUVWXab
"""

encrypted, mappings = encryptor.encrypt(text)

encrypt() returns a tuple: the encrypted text and a list of TokenMapping objects that record which tokens were found and what they became. Each mapping has plaintext, ciphertext, and pattern_name fields, which is useful for provenance tracking (only reversing tokens you actually encrypted).

For prefix-based tokens the result still looks like valid tokens: same prefixes, same lengths, same character sets, but the secret parts have been replaced with ciphertext. Decryption restores the original text exactly:

decrypted = encryptor.decrypt(encrypted)
assert decrypted == text

Tweaks

A tweak is optional context data that gets mixed into the encryption. The same plaintext encrypted with different tweaks produces different ciphertext, which is useful for binding tokens to a specific user, session, or tenant:

enc_alice, _ = encryptor.encrypt(text, tweak=b"user-alice")
enc_bob, _ = encryptor.encrypt(text, tweak=b"user-bob")

assert enc_alice != enc_bob

# Each can only be decrypted with the matching tweak
assert encryptor.decrypt(enc_alice, tweak=b"user-alice") == text
assert encryptor.decrypt(enc_bob, tweak=b"user-bob") == text

Filtering by token type

If you only want to encrypt certain kinds of tokens and leave the rest as-is, pass a types list with the pattern names you care about:

text = "ghp_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghij and AKIAIOSFODNN7EXAMPLE"

encrypted, _ = encryptor.encrypt(text, types=["github-pat"])
# The GitHub token is encrypted, but the AWS key is untouched

Supported token types

The following patterns are detected and encrypted out of the box:

Provider Pattern name(s) Prefix
Anthropic anthropic sk-ant-api03-
AWS aws-access-key AKIA
Datadog datadog ddapi_
GitHub github-pat, github-oauth, github-user, github-server, github-refresh ghp_, gho_, ghu_, ghs_, ghr_
GitLab gitlab glpat-
Google google-api AIza
Grafana grafana glc_
HuggingFace huggingface hf_
npm npm npm_
OpenAI openai, openai-legacy sk-proj-, sk-
PyPI pypi pypi-
SendGrid sendgrid SG.
Slack slack-bot, slack-user xoxb-, xoxp-
Stripe stripe-secret-live, stripe-publish-live, stripe-secret-test, stripe-publish-test sk_live_, pk_live_, sk_test_, pk_test_
Supabase supabase sbp_
Twilio twilio SK
Vercel vercel vercel_
Fastly fastly (heuristic, no prefix)
AWS Secret Key aws-secret-key (heuristic, no prefix)

Heuristic patterns don't rely on a prefix. They look for strings with high entropy and mixed character classes, which is how secrets like Fastly tokens and AWS secret keys are typically formatted. Because there is no distinguishing prefix, encrypted output is wrapped in an [ENCRYPTED:<name>] marker. decrypt() will attempt to unwrap anything matching that marker pattern, so avoid feeding text containing literal [ENCRYPTED:...] strings that were not produced by encrypt().

Custom token patterns

You can register your own patterns for tokens that aren't covered by the built-in set. A SimpleTokenPattern works for anything that has a fixed prefix followed by a body with a known alphabet:

from fast_cipher.tokens import ALPHANUMERIC, TokenEncryptor
from fast_cipher.tokens.types import SimpleTokenPattern

my_pattern = SimpleTokenPattern(
    name="myapp-api-key",
    prefix="myapp_",
    body_regex="[A-Za-z0-9]{32}",
    body_alphabet=ALPHANUMERIC,
    min_body_length=32,
)

key = os.urandom(32)
encryptor = TokenEncryptor(key)
encryptor.register(my_pattern)

text = "key: myapp_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef"
encrypted, _ = encryptor.encrypt(text)
decrypted = encryptor.decrypt(encrypted)
assert decrypted == text

Registered patterns take priority over built-in ones.

The available alphabets are DIGITS, HEX_LOWER, ALPHANUMERIC_UPPER, ALPHANUMERIC_LOWER, ALPHANUMERIC, BASE64, and BASE64URL. You can also create your own with Alphabet(name="my-abc", chars="abc...").

Low-level cipher

TokenEncryptor is built on top of FastCipher, which you can use directly when you need format-preserving encryption for arbitrary data. It works on sequences of integers in a given radix (base).

For example, to encrypt an 8-digit decimal number:

from fast_cipher import FastCipher, calculate_recommended_params

params = calculate_recommended_params(radix=10, word_length=8)
key = os.urandom(32)
cipher = FastCipher(params, key)

plaintext = [1, 2, 3, 4, 5, 6, 7, 8]
ciphertext = cipher.encrypt(plaintext)

# Result is still 8 digits, each between 0 and 9
assert len(ciphertext) == 8
assert all(0 <= d < 10 for d in ciphertext)

decrypted = cipher.decrypt(ciphertext)
assert decrypted == plaintext

For raw bytes, use radix 256 with the encrypt_bytes/decrypt_bytes convenience methods:

params = calculate_recommended_params(radix=256, word_length=16)
cipher = FastCipher(params, key)

ciphertext = cipher.encrypt_bytes(b"sensitive data!!")
plaintext = cipher.decrypt_bytes(ciphertext)
assert plaintext == b"sensitive data!!"

Cleanup

When you're done with an encryptor or cipher, call destroy() to invalidate the instance:

encryptor.destroy()

After destroy(), any further calls to encrypt() or decrypt() will raise a RuntimeError. The method overwrites key references with zero-filled placeholders and clears internal state, but Python's garbage collector may retain copies of the original key bytes in memory. For applications that require guaranteed memory scrubbing, use a C-level implementation instead.

Cross-implementation compatibility

Ciphertext produced by any FAST implementation (C, Zig, Go, JavaScript, Python) can be decrypted by any other, as long as the key, radix, word length, and tweak match. This library is tested against the Go test vectors and cross-validated against the JavaScript implementation.

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

fast_cipher-0.2.2.tar.gz (26.0 kB view details)

Uploaded Source

Built Distribution

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

fast_cipher-0.2.2-py3-none-any.whl (23.3 kB view details)

Uploaded Python 3

File details

Details for the file fast_cipher-0.2.2.tar.gz.

File metadata

  • Download URL: fast_cipher-0.2.2.tar.gz
  • Upload date:
  • Size: 26.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for fast_cipher-0.2.2.tar.gz
Algorithm Hash digest
SHA256 3f0e56bfb50c62e5aaff5b5b96ce0a0eb96e8d69fbe9c2da23076dad682cf98c
MD5 1d92859a7f876b37d32b03a8853a3dc5
BLAKE2b-256 2c8393e2b2648beca7049f0e9dfee794dd628134733c379b2af2abee905a82ba

See more details on using hashes here.

File details

Details for the file fast_cipher-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: fast_cipher-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 23.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for fast_cipher-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 81e99ecc807034c8268632e4e8033dfdae47f20c07b7f634fbb3d1f745213aa1
MD5 91b63da9635e75c63b80dbd9980da177
BLAKE2b-256 1ef848d831d2098061329b5f6bf64cc1169665583b7180aedd33b7b309a0eb50

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