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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With