Hashing - Always get unique hash for a given pair of user ids [duplicate]
Possible Duplicate:
Generate a unique value for a combination of two numbers
Is there a way to hash two user ids (integers), and to always get a unique hash for a given pair of user ids?
For example:
a = hash( x , y );
and
b = hash( y , x );
In the above example, a and b must always be equivalent for any given pair of ids in the range of an INT(11) (MySQL).
Plain PHP is the programming environment.
Any suggestions welco开发者_如何学编程me guys...
Summing the numbers doesn't make a unique hash. For instance, 1+3 == 2+2. This does:
function myHash( $id1, $id2 ) {
$ids = array( $id1, $id2 ); // or func_get_args() to support variable number
sort( $ids );
return md5( implode( '-', $ids ); // md5() == any other hashing function
}
myHash( x, y ) == myHash( y, x );
use http://php.net/manual/en/function.md5.php
like this:
$hash = md5(sprintf("%05d/%05d", $id1, $id2));
精彩评论