Score:0

Looking for a hash function (not necessarily cryptographic) where similar inputs return dissimilar outputs

in flag

I have the need to take a string and return a color. One requirement is that the same name always returns the same color. Another is that similar names should not return similar colors.

I understand that a property of cryptographic hash functions is that small changes to the input result in large changes to the output (diffusion). This seems like the property I need. However, I don't require the hashing function to be cryptographic is any other sense (it can be easily reversible).

Does anyone have suggestions for how to write a function with this property? Are there existing cryptographic functions that I could use? Thank you.

kr flag
MD5 (despite i is not recommended for cryptographic purposes, it suites your requirements), SHA-256, SHA-512, BLAKE2, Whirlpool - they all suite requirements you have described.
solowt avatar
in flag
That's great, thank you. Do you know if there are non cryptographic hash functions that have the properties I need?
Mark avatar
ng flag
@solowt there are, but it is unclear if they would be easier to use. There are at least two relevant concepts: One is that of a "chaotic map" --- small changes in the input lead to large changes in the output. The other is of a random walk that "mixes quickly", e.g. (like a cryptographic hash) one can expect the output after a few iterations will look uniform. The only reason I would not use a hash function though was if I already had a working implementation, and had noticed the hash function calls needed optimizing. Cryptographic hash functions are surprisingly fast though.
Mark avatar
ng flag
it's also worth mentioning that I don't know enough about "off the shelf" impls of the two concepts I described. There are many for hash functions of course.
kr flag
@solowt: MD5 is not cryptographically secure. It was designed for cryptographic purposes. But it hast weak collision resistance and is not used in any serious cryptographic use cases.
jthill avatar
cn flag
Just crc32 does pretty much exactly what you're asking for.
solowt avatar
in flag
Thanks everyone. I'll look into crc32 and md5. Appreciate the help.
kelalaka avatar
in flag
I’m voting to close this question because This is not about Cryptographic hash functions. This better suits to CS.
solowt avatar
in flag
Thanks for your help KELALAKA, appreciate it, keep up the good work.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.