Hash function that maps similar inputs to similar outputs?

1.5k views Asked by At

Is there a hash function where small changes in the input result in small changes in the output? For example, something like:

hash("Foo") => 9e107d9d372bb6826bd81d3542a419d6
hash("Foo!") => 9e107d9d372bb6826bd81d3542a419d7 <- note small difference
4

There are 4 answers

0
MSalters On

A trivial solution would be be to XOR all bytes module N. E.g. for a 64 bits hash, you'd XOR (input[0] ^ input[8] ^ input[16]) + 256*(input[1] ^ input[9] ^ input[17]) etc. So, "Foo" hashes to "Foo\0\0\0\0\0" and "Foo!" hashes to "Foo!\0\0\0\0".

0
Chris Judge On

I wouldn't call this a hash because the point of a hash is exactly the opposite. However, with your stated goal of small changes in input producing small changes in output, I would look at using either a soundex function or the Ratcliff algorithm.

0
Jeff Kubina On

I would recommend the simhash, algorithm by Mark Manasse.

0
gkcn On

Locality-sensitive hashing (LSH) reduces the dimensionality of high-dimensional data. LSH hashes input items so that similar items map to the same “buckets” with high probability:

https://en.wikipedia.org/wiki/Locality-sensitive_hashing

Also see: https://en.wikipedia.org/wiki/Perceptual_hashing

Here is a nice example of perceptual hashing on DNA sequences:

http://arxiv.org/pdf/1412.5517.pdf