northlands / permuteseq
A small PHP library to obfuscate numbers.
Requires
- php: ^8.1|^8.0
Requires (Dev)
- pestphp/pest: ^1.21
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2025-01-14 14:55:53 UTC
README
A small PHP library to obfuscate numbers. Use it when you don't want to expose your database numeric ids to users.
The permutations are collision-free and fully deterministic. The random-looking effect is due to encryption, not to a PRNG. The same boundary range with the same secret key will always produce the same output.
Installation
You can install the package via composer:
composer require northlands/permuteseq
Usage
$permuteseq = new Permuteseq(123456789012345, 1000, 9999); // Range 1000-9999 $encoded = $permuteseq->encode(1000); // 1446 $decoded = $permuteseq->decode($encoded); // 1000
Testing
composer test
In order to ensure compability with the Postgres extension, an integration test is included:
vendor/bin/pest --group=integration
The integration test requires a running Postgres database with the extension installed:
docker build -t postgres tests docker run --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=local -d postgres psql> CREATE EXTENSION permuteseq;
Changelog
Please see CHANGELOG for more information on what has changed recently.
Disclaimer
The library should not be considered cryptographically strong or used to sensitive data.
Credits
-
https://github.com/dverite/permuteseq
The PostgreSQL extension by Daniel Vérité. -
https://czep.net/21/obfuscate.html
An article on how to obfuscate primary keys in databases.
Alternatives
-
https://github.com/ioleo/cryptomute
Another Format Preserving Encryption library supporting multiple ciphers (DES, AES and Camellia) but slower performance. -
https://github.com/vinkla/hashids
Generates YouTube-like ids from numbers. Can be limited to digit-alphabet, but returns strings like"09284"
instead of safe integers. -
https://github.com/jenssegers/optimus
Super-fast number obfuscation based on Knuth's integer hash. However, range can only be defined by 4-62 bits.
License
The MIT License (MIT). Please see License File for more information.