krb
krb

Reputation: 16325

A bi-directional hashing algorithm?

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?

Upvotes: 3

Views: 3014

Answers (3)

Matt Ball
Matt Ball

Reputation: 359846

You could add or XOR the two numbers before hashing them.

Upvotes: 2

Blindy
Blindy

Reputation: 67378

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").

Upvotes: 5

hmakholm left over Monica
hmakholm left over Monica

Reputation: 23332

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

Upvotes: 4

Related Questions