ap-lib / public-id
A small PHP library for generating short, public-friendly integer IDs from internal numeric IDs. Designed to hide system scale and client-specific patterns while keeping IDs compact and reversible.
Requires
- php: ^8.3
Requires (Dev)
- phpunit/phpunit: 10.5.*
This package is auto-updated.
Last update: 2025-03-22 17:58:42 UTC
README
A small PHP library for generating short, public-friendly integer IDs from internal numeric IDs.
Designed to hide system scale and client-specific patterns while keeping IDs compact and reversible.
❗️Not for security purposes.
If clients know you're using
UnsafePublicID
, they might infer ID ranges or volumes. UseSafePublicID
for stronger obfuscation with a mask.It's still reversible and not secure, but the effort required to reverse-engineer it is significantly greater.
Installation
composer require ap-lib/public-id
Features
- Encode numeric IDs into non-sequential public integers
- Keep public-facing IDs short and opaque
- Optional masking layer to prevent reverse-engineering
- Fully reversible with
decode()
Requirements
- PHP 8.3 or higher
Getting started
Basic usage with UnsafePublicID
use AP\PublicID\UnsafePublicID; $digits_divider = 2; $original_id = 1000; $public_id = UnsafePublicID::encode( int: $original_id, digits_divider: $digits_divider ); $decoded_original_id = UnsafePublicID::decode( public: $public_id, digits_divider: $digits_divider ); // $decoded_original_id equal $original_id $public_id_2 = UnsafePublicID::encode( int: $original_id, digits_divider: $digits_divider ); // $public_id equal $public_id_2
Safer encoding with SafePublicID
use AP\PublicID\SafePublicID; $secret_mask_from_envs = 902734092; $safePublicId = new SafePublicID( mask: $secret_mask_from_envs ); $digits_divider = 2; $original_id = 1000; $public_id = $safePublicId->encode( int: $original_id, digits_divider: $digits_divider ); $decoded_original_id = $safePublicId->decode( int: $public_id, digits_divider: $digits_divider ); // $decoded_original_id equal $original_id $public_id_2 = $safePublicId->encode( int: $original_id, digits_divider: $digits_divider ); // $public_id equal $public_id_2
License
This library is open-sourced software licensed under the MIT license.