Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mindplay-dk/keypack
Pack/unpack integer and GUID keys to shorter strings
https://github.com/mindplay-dk/keypack
Last synced: 3 months ago
JSON representation
Pack/unpack integer and GUID keys to shorter strings
- Host: GitHub
- URL: https://github.com/mindplay-dk/keypack
- Owner: mindplay-dk
- Created: 2016-08-15T13:33:50.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-08-15T13:56:34.000Z (over 8 years ago)
- Last Synced: 2023-07-15T11:47:51.746Z (over 1 year ago)
- Language: PHP
- Size: 3.91 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
mindplay/keypack
================This library packs integer, UUID and GUID keys to shorter strings, e.g. for use in user-facing URLs.
[![PHP Version](https://img.shields.io/badge/php-5.4%2B-blue.svg)](https://packagist.org/packages/mindplay/keypack)
[![Build Status](https://travis-ci.org/mindplay-dk/keypack.svg?branch=master)](https://travis-ci.org/mindplay-dk/keypack)#### Usage
Three classes, `IntPacker`, `UUIDPacker` and `GUIDPacker` are provided, each capable of packing/unpacking
a particular type of ID. Each class has essentially the same interface, but accepting/returning different
types of keys.Here's an example of packing/unpacking a UUID to a shorter string:
```php
$packer = new UUIDPacker();echo $packer->pack("7eb6de1e-65ef-4fb7-baff-c0732c1c4614"); // => "py6dWN6dgKR8cGVz73zDiT"
echo $packer->unpack("py6dWN6dgKR8cGVz73zDiT"); // => "7eb6de1e-65ef-4fb7-baff-c0732c1c4614"
```You can pack keys using different notations. The default is `legible`, which produces human-readable
and reasonably short keys - [see here for more options](https://github.com/mindplay-dk/nbase).You can also add redundancy to packed keys, to prevent typos make it harder to guess a packed key.
Here's an example packing an integer key to `base64` with 4 bytes of redundancy:
```php
$packer = new IntPacker('base64');$packer->setRedundancy(4, 'super secret salt');
echo $packer->pack(123456); // => "7yg6HR"
echo $packer->unpack("7yg6HR"); // => (int) 123456
```Note that `unpack()` will return `null` if the redundancy check fails.