开发者

Hashing - Always get unique hash for a given pair of user ids [duplicate]

This question already has answers here: Closed 10 years ago.

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));

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜