Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A bi-directional hashing algorithm?

Tags:

hash

Is there an algorithm that will yield the same hash for two numbers, no matter what order they are in?

For example, hashing 3268 and 2642 should yield the same result as hashing 2642 and 3268.

Is this possible?

like image 600
Kristina Brooks Avatar asked Feb 17 '26 12:02

Kristina Brooks


2 Answers

Of course, XOR does that.

 3268^2642 == 2642^3268

There's a lot more (addition, multiplication, basically any commutative operation), but XOR is usually used for hashing anyway (because it's easy to "unhash").

like image 50
Blindy Avatar answered Feb 19 '26 02:02

Blindy


Hash the two numbers separately (using an integer-to-integer hash of your choice), and then either add or xor the results.

like image 30
hmakholm left over Monica Avatar answered Feb 19 '26 00:02

hmakholm left over Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!