My question probably doesn't have an exact answer, but I am interest in others' opinion on the matter. I am interested in generating a unique id from two strings. The reason for this is to make it easy to coordinate with a third party within our application. They will be able to write unique records to our database by using whatever information they decide to combine. This becomes particularly convenient when performing updates to our system because they won't have to store our index key in their database. Please, if you answer, don't reply saying that it's a stupid idea. It may in fact be a stupid idea, but that's not the point of this exercise.
I've devised two ways to perform this hash calculation. My plan is to use the hash as the _id in an elasticsearch db.
Hash(Hasha+Hashb)
function generateId(a, b) {
var foo = crypto.createHash('md5').update(a).digest('hex');
var bar = crypto.createHash('md5').update(b).digest('hex');
return crypto.createHash('md5').update(foo+bar).digest('hex');
}
Hash(a+'|'+b)
function generateId(a, b) {
var foo = crypto.createHash('md5').update(a+'|'+b).digest('hex');
return foo;
}
The first method uses only hash functions. The second method inserts a | character ensuring that 123 and 456 won't hash to the same value as 12 and 3456. Both work, but I'm curious to know if:
- Anyone has a better idea
- Which of the two would be preferable
via Bill Butler
No comments:
Post a Comment