An open API service indexing awesome lists of open source software.

https://github.com/dmhendricks/hash-int

A PHP class to generate an alphanumeric hash of specified length from an integer.
https://github.com/dmhendricks/hash-int

base62 hash hash-functions short-id shorturl tiny url-shortener

Last synced: 4 months ago
JSON representation

A PHP class to generate an alphanumeric hash of specified length from an integer.

Awesome Lists containing this project

README

          

[![Release](https://img.shields.io/github/release/dmhendricks/hash-int.svg?style=flat-square&colorB=663399)](https://github.com/dmhendricks/hash-int/releases)
[![GitHub License](https://img.shields.io/badge/license-MIT-yellow.svg?style=flat-square)](https://raw.githubusercontent.com/dmhendricks/hash-int/master/LICENSE)
[![Downloads](https://img.shields.io/packagist/dt/dmhendricks/hash-int.svg?style=flat-square)](https://packagist.org/packages/dmhendricks/hash-int?utm_source=github.com&utm_medium=referral&utm_content=button&utm_campaign=dmhendricks%2Fhash-int)
[![DigitalOcean](https://img.shields.io/badge/hosting-Digital%20Ocean-green.svg?style=flat-square&label=hosting&colorB=0152FF)](https://m.do.co/t/8a88362f5683?utm_source=github.com&utm_medium=campaign&utm_content=referral&utm_campaign=dmhendricks%2Fhash-int)
[![Twitter](https://img.shields.io/twitter/url/https/github.com/dmhendricks/hash-int.svg?style=social)](https://twitter.com/danielhendricks)
[![BrowserStack](https://img.shields.io/static/v1?style=social&label=BrowserStack&message=Passed&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAMAAABF0y+mAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyhpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNi1jMTQ1IDc5LjE2MzQ5OSwgMjAxOC8wOC8xMy0xNjo0MDoyMiAgICAgICAgIj4gP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GnGf8NMgCcvLLEw5OZOv0yMfvARRfD7v+VSf9lZ/9mOqTs9lvAQ/+EK/+VY7Hr/f+cN0+7Lb2FMuqFG7tuOv9jEOCLLP93JKzj+ZDYwwC9s2LLrQB8k/9wEv+4R/9oD5/g1b3iAACtygCJo//w6MnHyPM5M+h3NuVyfcLPZP9MWKOYXMevu/+Xn5HG06nWAwDG8pzn6//lsZ/l85rik/+def/38fNoILPS3/1tF/+3Zf+sROmuICWPq3edprrG0VTW26jn/EhteD7CN9ibbgC8t/96NPmLQIjPY/9CKSQfIP///////+tAimMAAACAdFJOU/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////8AOAVLZwAAAj9JREFUeNpU0vtb0lAYB/CzCTgeDrQpGwyUyzZwwwyBmTRNELwgkYpQZkUXumktyrJ13eFf9z0CZd8ftufZZ9/znPOcFw1HIWezp+u9Xm/9xaw8nASNXsczfNk0c2VJOjGni2fXkRQ1KWeWdxKBSCSS2DuZZslflDepPX76LrS/P/+7343slWfkMRJque+djGXlIcuo301IH+URFjU+Z97ayAAEIfl8NtxN3GUIxWMNS+aTDS/QysHBij8YXOTC3b21N4DkFIrTH95alt/j+nw+z0owmE32A78UdYhS2JZg0aiV9/hc1zc1C7q4hbolgx0ilgd8vuG1HoK5rkxcT/B+kusHDEFGm5ot5b6kvZbnCh9Nua7/R5ILR9piDP3EVxgdowtPPzRR5I7IIpvi7bSeGSPEvzhCBjUp3rjQo5/H6PO8mt8CbCtxuixvPnut695J1f+1wnHhgAG4pmG7vBu6p6ej367O+cALRa7/SQQsahjzJ8kWaPrl+fmy9yIEVgnUBdhQ7BBj+8+uE9KBa7WaTntcf2EgwFHkAlSxtOpkQ61OpxWqUAuXqoKiyIgwUMVHoFtcpcJRclAJG7AqQUN1DlPlt5OO44A4laUFvi4oYgqujLB1qrjxfnv1JmRp54gvgMGowGXL8blDnpYbDfpPg7dpLy6PZkgVjCrW8Cg8rhpggjqZPjUuGoWmDWQ3CwOB9tR/cyuzoqC0B4OBoVASWfn6UJMUo4gAVBQmRf6beGA1xsQVJc7EVDL5dinAAO2UucfjcPEkAAAAAElFTkSuQmCC)](https://browserstack.com/?utm_source=github.com&utm_medium=referral&utm_content=link&utm_campaign=dmhendricks%2Fhash-int)

# Hash Integer

A PHP class to generate an alphanumeric hash of specified length from an integer.

```
composer require dmhendricks/hash-int
```

### Requirements

- PHP 5.6 or higher
- [BC Math](https://www.php.net/manual/en/book.bc.php): `apt install php7.x-bcmath`

### Usage

```php
$num = 456;

/**
* Create a 7-character hash from integer
*/
$hash = \CloudVerve\HashInt::hash( $num, 7 );

// Unhash the string
$unhashed = \CloudVerve\HashInt::unhash( $hash );

echo 'Hashed: ' . $hash . '
';
echo 'Unhashed: ' . $unhashed;
```

### Result

```
Hashed: p3Wq1aC
Unhashed: 456
```

## Original [Author](https://github.com/KevBurnsJr) Notes

Paraphrased ([source](http://web.archive.org/web/20130727034425/http://blog.kevburnsjr.com/php-unique-hash)):

> 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](https://www.numberempire.com/primenumbers.php?utm_source=github.com&utm_medium=referral&utm_content=link&utm_campaign=dmhendricks%2Fhash-int)
- [Ernesto Preste](https://www.linkedin.com/in/ernesto-preste-4377912b/) for adding [bcmath support](http://web.archive.org/web/20130727034425/http://blog.kevburnsjr.com/php-unique-hash#comment-2792)
- [Padraig Kennedy](https://twitter.com/Padraig) for adding [hash reversibility](http://web.archive.org/web/20130727034425/http://blog.kevburnsjr.com/php-unique-hash#comment-2805)

[![Analytics](https://ga-beacon.appspot.com/UA-126205765-1/dmhendricks/hash-int?flat)](https://ga-beacon.appspot.com/?utm_source=github.com&utm_medium=campaign&utm_content=button&utm_campaign=dmhendricks%2Fhash-int)