Skip to main content

Fast Format Preserving Encryption (FPE) implementation in Rust

Project description

fastfpe

Fast Format Preserving Encryption (FPE) implementation in Rust with Python bindings.

Format-preserving encryption (FPE) is a cryptographic method that encrypts data while preserving its format. For example, encrypting a credit card number yields another valid-looking credit card number, making it useful for data protection while maintaining compatibility with existing systems.

Features

  • FF1 and FF3-1 Format Preserving Encryption algorithms
  • Fast Rust implementation with Python bindings
  • Support for custom alphabets
  • Thread-safe

[!Note]: As of late 2025, FF3/FF3-1 are no longer recommended in some security guidance due to design concerns. FF1 is fully supported here and recommended for new deployments. FF3-1 remains available for compatibility and migration.

Installation

pip install fastfpe

Usage (FF3-1)

>>> from fastfpe import ff3_1
>>>
>>> key = "3eaa133d22a7ee2432fb8ecfde1e97d9106dcf26b9edaa52b3ed4acd9a9b8445"
>>> tweak = "5be49f26c1dbb7"  # 7 bytes (14 hex chars)
>>> alphabet = "abcdef0123456789"
>>> plaintext = "024587931578"
>>>
>>> ciphertext = ff3_1.encrypt(key, tweak, alphabet, plaintext)
>>> ciphertext
'd756b8704a2d'
>>> ff3_1.decrypt(key, tweak, alphabet, ciphertext)
'024587931578'

Usage (FF1)

>>> from fastfpe import ff1
>>>
>>> # 128/192/256-bit AES keys supported (16/24/32 bytes -> 32/48/64 hex chars)
>>> key = "2b7e151628aed2a6abf7158809cf4f3c"  # 128-bit key
>>> tweak = ""  # FF1 tweak may be empty or longer (length impacts security domain sizing)
>>> alphabet = "0123456789"
>>> plaintext = "0123456789"
>>>
>>> ciphertext = ff1.encrypt(key, tweak, alphabet, plaintext)
>>> ciphertext
'2433477484'
>>> ff1.decrypt(key, tweak, alphabet, ciphertext)
'0123456789'

Notes:

  • FF1 tweak can be empty; size limits depend on NIST SP 800-38G constraints (radix and length bounds enforced internally).
  • Provide a non-empty tweak for domain separation across contexts.
  • Both algorithms return ValueError with descriptive messages on invalid inputs.

Performance

As expected, fastfpe is much faster than the reference python implementation. The gains are more pronounced with larger plaintexts.

The following benchmarks were performed on an Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz.

Running 10,000 iterations, 5 times each, 12-byte plaintext
--------------------------------------------------

Python implementation: 0.110 ms/op (± 0.001 ms)
Rust implementation:   0.008 ms/op (± 0.000 ms)
Rust is 13.2x faster

Running 10,000 iterations, 5 times each, 16-byte plaintext
--------------------------------------------------

Python implementation: 0.133 ms/op (± 0.002 ms)
Rust implementation:   0.009 ms/op (± 0.000 ms)
Rust is 14.5x faster

Running 10,000 iterations, 5 times each, 20-byte plaintext
--------------------------------------------------

Python implementation: 0.161 ms/op (± 0.005 ms)
Rust implementation:   0.010 ms/op (± 0.002 ms)
Rust is 15.9x faster

Running 10,000 iterations, 5 times each, 24-byte plaintext
--------------------------------------------------

Python implementation: 0.189 ms/op (± 0.004 ms)
Rust implementation:   0.011 ms/op (± 0.000 ms)
Rust is 16.8x faster

Support Matrix

Component Versions / Platforms
Python CPython 3.8 – 3.14 (abi3 single wheel per platform)
Operating Systems Linux (x86_64, aarch64 manylinux), macOS (universal2), Windows (x64, arm64)
Algorithms FF1 (recommended), FF3-1 (compatibility)
Key Sizes 128 / 192 / 256-bit AES
Tweak FF1: variable length (can be empty). FF3-1: exactly 7 bytes
Alphabet Custom per call; size defines radix (validated)

If you need other targets (e.g., musllinux, ppc64le) open an issue or PR.

License

Licensed under the MIT License. See LICENSE for more information.

Uses the rust-fpe implementation by johntyner, under the MIT License.

Uses the ff3 python package by mysto as a reference implementation, under the Apache 2.0 License.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

fastfpe-0.2.0-cp38-abi3-win_arm64.whl (158.3 kB view details)

Uploaded CPython 3.8+Windows ARM64

fastfpe-0.2.0-cp38-abi3-win_amd64.whl (169.2 kB view details)

Uploaded CPython 3.8+Windows x86-64

fastfpe-0.2.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (305.9 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ x86-64

fastfpe-0.2.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (289.0 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

fastfpe-0.2.0-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (523.5 kB view details)

Uploaded CPython 3.8+macOS 10.12+ universal2 (ARM64, x86-64)macOS 10.12+ x86-64macOS 11.0+ ARM64

File details

Details for the file fastfpe-0.2.0-cp38-abi3-win_arm64.whl.

File metadata

  • Download URL: fastfpe-0.2.0-cp38-abi3-win_arm64.whl
  • Upload date:
  • Size: 158.3 kB
  • Tags: CPython 3.8+, Windows ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fastfpe-0.2.0-cp38-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 98342050249d384a143a3788a60a5c3785bde54a37141260c1350c90dc3e0faf
MD5 34e1f706c22f8f53f9c26ab1eaa687dc
BLAKE2b-256 a9dc3ce3eb518d1db19f4c886546028dee5a7b41ac692e350d860eab56afae02

See more details on using hashes here.

File details

Details for the file fastfpe-0.2.0-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: fastfpe-0.2.0-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 169.2 kB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fastfpe-0.2.0-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 d463080109e688bd14b674dddda71b937de3f6ff59b4aa1a9204c75aab2e4fc5
MD5 54842eea4b6551f5962267a87eac9e30
BLAKE2b-256 fa67eb8a653809638b1494c9dcc96567436136013f9cafb7615ccdf381a180d3

See more details on using hashes here.

File details

Details for the file fastfpe-0.2.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for fastfpe-0.2.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9f98d3fd50477d1d6c2f9c907a9caecff8e309a72a05f76c48e78c0b277ccc02
MD5 f8d1418164d74615d1a49cf10aabf742
BLAKE2b-256 065cc70222943b12b4274c18a0df9ae76ff14ea882b2cc7a1ac435882107fbf8

See more details on using hashes here.

File details

Details for the file fastfpe-0.2.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for fastfpe-0.2.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 aee9a74bd7eb9bea3dfba96a5636e85ba924682a68df662ebf5e27d2eacf957d
MD5 483d6f556e0dc7036154cff94f44ef16
BLAKE2b-256 ce18021d82354e9aa173c301ac14770224cb6cc6c13665bb5022fab9ca60703d

See more details on using hashes here.

File details

Details for the file fastfpe-0.2.0-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl.

File metadata

File hashes

Hashes for fastfpe-0.2.0-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 8434a5020587249d4081e9fb38927d8b2c215a2f81034d53aed2b96e5b4cc4f0
MD5 e7e2be4bd413a1c444e6e074f8b472ee
BLAKE2b-256 69d35be2730de4b3bc57898e521d2e3fe95586ab08ecb8c96c6ccb079d1f2579

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