Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php: number only hash?

In php is there a way to give a unique hash from a string, but that the hash was made up from numbers only?

example:

return md5(234); // returns 098f6bcd4621d373cade4e832627b4f6 

but I need

return numhash(234); // returns 00978902923102372190  (20 numbers only) 

the problem here is that I want the hashing to be short.

edit: OK let me explain the back story here. I have a site that has a ID for every registered person, also I need a ID for the person to use and exchange (hence it can't be too long), so far the ID numbering has been 00001, 00002, 00003 etc...

  1. this makes some people look more important
  2. this reveals application info that I don't want to reveal.

To fix point 1 and 2 I need to "hide" the number while keeping it unique.

Edit + SOLUTION:

Numeric hash function based on the code by https://stackoverflow.com/a/23679870/175071

/**  * Return a number only hash  * https://stackoverflow.com/a/23679870/175071  * @param $str  * @param null $len  * @return number  */ public function numHash($str, $len=null) {     $binhash = md5($str, true);     $numhash = unpack('N2', $binhash);     $hash = $numhash[1] . $numhash[2];     if($len && is_int($len)) {         $hash = substr($hash, 0, $len);     }     return $hash; }  // Usage numHash(234, 20); // always returns 6814430791721596451 
like image 359
Timo Huovinen Avatar asked Jul 31 '10 19:07

Timo Huovinen


People also ask

How do you hash something in PHP?

The hash() function returns a hash value for the given data based on the algorithm like (md5, sha256). The return value is a string with hexits (hexadecimal values).

What is the password_ hash in PHP?

password_hash() creates a new password hash using a strong one-way hashing algorithm. The following algorithms are currently supported: PASSWORD_DEFAULT - Use the bcrypt algorithm (default as of PHP 5.5. 0). Note that this constant is designed to change over time as new and stronger algorithms are added to PHP.

How do you generate a hash value?

Hashing involves applying a hashing algorithm to a data item, known as the hashing key, to create a hash value. Hashing algorithms take a large range of values (such as all possible strings or all possible files) and map them onto a smaller set of values (such as a 128 bit number). Hashing has two main applications.

How long is SHA256 hash?

The hash size for the SHA256 algorithm is 256 bits.


1 Answers

An MD5 or SHA1 hash in PHP returns a hexadecimal number, so all you need to do is convert bases. PHP has a function that can do this for you:

$bignum = hexdec( md5("test") ); 

or

$bignum = hexdec( sha1("test") ); 

PHP Manual for hexdec

Since you want a limited size number, you could then use modular division to put it in a range you want.

$smallnum = $bignum % [put your upper bound here] 

EDIT

As noted by Artefacto in the comments, using this approach will result in a number beyond the maximum size of an Integer in PHP, and the result after modular division will always be 0. However, taking a substring of the hash that contains the first 16 characters doesn't have this problem. Revised version for calculating the initial large number:

$bignum = hexdec( substr(sha1("test"), 0, 15) ); 
like image 185
derekerdmann Avatar answered Sep 24 '22 08:09

derekerdmann