Download the PHP package dmhendricks/hash-int without Composer
On this page you can find all versions of the php package dmhendricks/hash-int. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dmhendricks/hash-int
More information about dmhendricks/hash-int
Files in dmhendricks/hash-int
Package hash-int
Short Description A PHP class to generate a short hash from an integer.
License MIT
Homepage https://github.com/dmhendricks/hash-int
Informations about the package hash-int
Hash Integer
A PHP class to generate an alphanumeric hash of specified length from an integer.
Requirements
- PHP 5.6 or higher
- BC Math:
apt install php7.x-bcmath
Usage
Result
Original Author Notes
Paraphrased (source):
I wanted a short, unique, alphanumeric hash with a sequence that is difficult to deduce. I could create it with md5 and trim the first n chars, but storing a truncated checksum in a unique field means that the frequency of collisions will increase geometrically as the number of unique keys for a base 62-encoded integer approaches 62^n. I'd rather do it right than create a time bomb.
Base 62
Hashes are base 62 encoded base 10 integers. 1=1, 10=a, 36=Z, 61=z, 62=10, 72=1a, etc.
62, 3844, 238328, 14776336, 916132832, 56800235584, 3521614606208
1 character = 62 permutations, 2 characters = 3844 permutations, etc.
41, 2377, 147299, 9132313, 566201239, 35104476161, 2176477521929
41 = next highest prime from golden mean of 62.
2377 = next highest prime from golden mean of 3844.
Uniqueness
I chose to use primes to ensure hash uniqueness. Any prime greater than one half 62^n will do, but if you use a prime near 62^n or 62^n/2 or 2*62^n/3 etc, you will detect a linearity in the sequence at certain points in the ring.
Appearance of Randomness
I chose primes near the golden ratio to maximize the appearance of randomness. Given a small set of hashes (even with the associated id) it would be difficult for anyone to guess the next hash.
:warning: Minimum-Security Technique
This is a thin rotation and base re-encoding obfuscation algorithm, not an encryption algorithm. Don't use this to encrypt sensitive information - use it to obfuscate integer IDs or create short reference keys (as an example, for short URLs).
Special Thanks
- Prime Numbers Generator and Checker
- Ernesto Preste for adding bcmath support
- Padraig Kennedy for adding hash reversibility