Download the PHP package atk14/packer without Composer
On this page you can find all versions of the php package atk14/packer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package packer
Short Description Converts any PHP variable into a URL-safe string with HMAC signing, optional AES-256-CBC encryption, and optional gzip compression.
License MIT
Homepage https://github.com/atk14/Packer
Informations about the package packer
Packer
Converts any PHP variable into a URL-safe string — and back. Supports HMAC signing, AES-256-CBC encryption, and gzip compression.
The Packer is particularly useful for:
- inserting a value into a URL,
- inserting a value into a hidden form field,
- inserting a value into a cookie.
In all cases, the value itself is securely hidden from the user and protected against unauthorized tampering.
Installation
Basic usage
Every packed string is signed with HMAC-SHA256. Tampered or forged strings are rejected on unpack.
Configuration
Set a secret before using the library — either via a constant or SetSalt():
Important: Never use the default salt (
"Put_Some_Secret_Text_Here") in production. Anyone with access to the source code could forge valid packed strings.
Global defaults can also be set via constants:
| Constant | Default | Description |
|---|---|---|
PACKER_CONSTANT_SECRET_SALT |
"Put_Some_Secret_Text_Here" |
Secret for signing and encryption |
PACKER_ENABLE_ENCRYPTION |
false |
Enable AES-256-CBC encryption |
PACKER_USE_COMPRESS |
false |
Enable gzip compression |
PACKER_USE_JSON_SERIALIZATION |
true |
Use JSON (true) or PHP serialize() (false) |
PACKER_SIGNATURE_LENGTH |
16 |
Number of Base64URL characters used as the HMAC-SHA256 signature (8–43) |
Encryption
Each call to Pack() with encryption generates a random IV, so the same input produces a different output every time.
Per-call password (extra_salt)
Useful when different users or sessions need isolated packed values:
Compression
Compression is detected automatically on unpack — no need to pass any option.
Decode() — inline unpacking
Decode() is a shorthand for Unpack() that returns the value directly instead of using an output parameter:
To distinguish a failed unpack from a legitimately packed null, use the optional second parameter:
Method reference
Packer::Pack($variable, $options = []): string
Packs a variable into a URL-safe string.
| Option | Type | Default | Description |
|---|---|---|---|
enable_encryption |
bool | PACKER_ENABLE_ENCRYPTION |
Encrypt with AES-256-CBC |
use_compress |
bool | PACKER_USE_COMPRESS |
Compress with gzip |
use_json_serialization |
bool | PACKER_USE_JSON_SERIALIZATION |
JSON vs PHP serialize |
extra_salt |
string | "" |
Additional secret for signing and encryption |
Packer::Unpack($packed, &$out, $options = []): bool
Unpacks a string. Returns true on success, false if the string is invalid or tampered.
Accepts the same options as Pack(), except use_compress (auto-detected from the packed string).
Packer::Decode($packed, &$decoded = false): mixed
Shorthand for Unpack(). Returns the unpacked value, or null on failure. Sets $decoded to true/false.
Packer::SetSalt($salt): string
Sets the runtime secret salt. Returns the previous salt.
Security notes
- Signing: Every packed string is signed with HMAC-SHA256. Any modification to the string is detected on unpack.
- Encryption: AES-256-CBC with a random IV. The encryption key is derived from
PACKER_CONSTANT_SECRET_SALT+ runtime salt (set viaSetSalt()) +extra_saltusing SHA-256. - Deserialization: When using PHP
serialize()mode, objects are never instantiated on unpack (allowed_classes => false).
Requirements
- PHP >= 5.6
- Extensions:
openssl,zlib(only needed if compression is used)
License
MIT