php: number only hash?

2019-01-11 02:42发布

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

7条回答
小情绪 Triste *
2楼-- · 2019-01-11 03:11

Try hashid.
It hash a number into format you can define. The formats include how many character, and what character included.
Example:
$hashids->encode(1);
Will return "28630" depends on your format,

查看更多
时光不老,我们不散
3楼-- · 2019-01-11 03:14

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) );
查看更多
▲ chillily
4楼-- · 2019-01-11 03:20

You can try crc32(). See the documentation at: http://php.net/manual/en/function.crc32.php

$checksum = crc32("The quick brown fox jumped over the lazy dog.");
printf("%u\n", $checksum); // prints 2191738434 

With that said, crc should only be used to validate the integrity of data.

查看更多
Anthone
5楼-- · 2019-01-11 03:20

Just use my manual hash method below:

Divide the number (e.g. 6 digit) by prime values, 3,5,7.

And get the first 6 values that are in the decimal places as the ID to be used. Do a check on uniqueness before actual creation of the ID, if a collision exists, increase the last digit by +1 until a non collision.
E.g. 123456 gives you 771428 123457 gives you 780952 123458 gives you 790476.

查看更多
狗以群分
6楼-- · 2019-01-11 03:29

First of all, md5 is basically compromised, so you shouldn't be using it for anything but non-critical hashing. PHP5 has the hash() function, see http://www.php.net/manual/en/function.hash.php.

Setting the last parameter to true will give you a string of binary data. Alternatively, you could split the resulting hexadecimal hash into pieces of 2 characters and convert them to integers individually, but I'd expect that to be much slower.

查看更多
Evening l夕情丶
7楼-- · 2019-01-11 03:32

The problem of cut off the hash are the collisions, to avoid it try:

return  hexdec(crc32("Hello World"));

The crc32():

Generates the cyclic redundancy checksum polynomial of 32-bit lengths of the str. This is usually used to validate the integrity of data being transmitted.

That give us an integer of 32 bit, negative in 32 bits installation, or positive in the 64 bits. This integer could be store like an ID in a database. This don´t have collision problems, because it fits into 32bits variable, once you convert it to decimal with the hexdec() function.

查看更多
登录 后发表回答