Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this hash guaranteed to be unique?

I need to uniquely identify a pair of Facebook user ids. This is how I do that:

NSString *firstId  = @"123456789";
NSString *secondId = @"987654321";

NSUInteger first_hash = [firstId hash];
NSUInteger second_hash = [secondId hash];

NSUInteger combinedHash = first_hash ^ second_hash;
NSUInteger reverseHash  = second_hash ^ first_hash;

NSLog(@"Combined hash %d\nReverse hash %d", combinedHash, reverseHash); // both are equal

Ok, now I know that regardless the order in which the hashes are combined i'm getting the same value. That's good. But is this value guaranteed to be unique? Or it is possible that a combination of ids say 322233322 and 233322233 will produce the same value as for combinedHash? If so then how do I make an unique identifier for a pair of ids?

like image 863
Andrey Chernukha Avatar asked Feb 13 '23 20:02

Andrey Chernukha


2 Answers

Without understanding much of ObjectiveC, it looks like you´re just XOR-ing the values.
This is, of course, NOT unique.
101^100 = 001
001^000 = 001
It´s that easy.

Must it be a irreversible hash or do you need just an unique id?
Latter: Just concatenate, with a unique delimiter between.
Else, depending on the maximal possible input length, a unique hash is probably impossible.
(without inventing a completely new algorithm, which may take time :))

edit, about the two possible orders of concatenation:
Just compare the two numbers before concatenating and put the smaller one first.
That way, any search by ID have not to be done twice.

like image 65
deviantfan Avatar answered Feb 24 '23 02:02

deviantfan


The answer to your first question is no because 1 ^ 1 == 0 ^ 0 and 1 ^ 0 = 0 ^ 1. So if you flip a bit in your first hash, and the same bit in your second hash then your first and second hash will be different but the combined hash will remain the same.

The point of a hash is to identify something with less information than you had in the original object. As you are compacting the information to make comparisons faster it is guaranteed that a hash won't be unique.

Apending 1 ID to the end of the other.

like image 20
James Snook Avatar answered Feb 24 '23 00:02

James Snook