BravoIDs
Converts IDs (base10) to Hashed IDs (base50 or 62)., (*1)
The BravoIDs class offers two methods : BravoIDs::encrypt and BravoIDs::decrypt., (*2)
BravoIDs::encrypt
Description
<?php
/**
*
* @param int $number The ID you want to convert into a hash
* @param string $passPhrase A passphrase that you must provide
* @param int $minHashLength Specify the minimum length for the output.
* @param bool $safeCharacters Remove all the vowels to generate the output to avoid curse words
*
* @return string The hash of a converted ID.
*/
BravoIDs::encrypt($number, $passPhrase, $minHashLength = 0, $safeCharacters = true);
?>
BravoIDs::encrypt
Description
Returns an ID from a hash., (*3)
Parameters:
- $hash : The hash you want to convert into an ID
- $passPhrase : A passphrase that you must provide
- $minHashLength (optional - default is 0) : Specify the minimum length for the output.
- $safeCharacters (optional - default is true) : Remove all the vowels to generate the output to avoid curse words
NB : in order to decrypt a hash, you must specify the exact same variables for $passPhrase, $minHashLength and $safeCharacters taht you used for BravoID::encrypt., (*4)
Basic Usage:
To simply generate IDS :, (*5)
Advanced Usage:
To increase hash length :, (*6)
NB: if you use a 32-bit builds of PHP you won't be able to set a $minHashLengh > 5. This is explained in the Limitations part., (*7)
To allow all characters including vowels (cursing words could appear) :, (*8)
Limitations
Depending on your build of PHP you will be limited:, (*9)
- 32-bit builds of PHP : Integers can be from -2147483648 to 2147483647
- 64-bit builds of PHP: Integers can be from -9223372036854775808 to 9223372036854775807
I did not run tests on 64-bit build of php., (*10)
On 32-bit, the limitation means that you'll be able to convert ids up to 2 147 483 647 and not one more., (*11)
Moreover, when you use the variable $minHashLength, you reduce the number of available ids., (*12)
A computation is done to find the minimum value that your number should have to reach the required HashLength):, (*13)
For example, to get a hash of at least X characters, you'll need a ~number >= base * (X - 1)~., (*14)
Where base is the number of characters used to convert your numbers (base50 with $safeCharacters = true and base62 with $safeCharacters = false)., (*15)
Once we know what is this minimum value, all your ids are "scaled" to that value, '0' would become 6 250 000 (base50 and $minHashLength = 4)., (*16)
In this case, this has for effect that your 2 147 483 647 has 6 250 000 less value possible ('0' to '6 249 999')., (*17)
Thanks
Thanks to ivanakimov and https://github.com/ivanakimov/hashids.php from which I borrowed the _consistent_shuffle method., (*18)
Thanks to Kevin van Zonneveld who wrote this article from which I took the idea of removing the vowels to get safe Words: http://kvz.io/blog/2009/06/10/create-short-ids-with-php-like-youtube-or-tinyurl/, (*19)
BravoID is named after the function offered on this article which was called AlphaID :-), (*20)