2

I need to one way hash alphanumeric + special chars(ascii) strings of variable length (10-20 chars). The output should be of variable length but max 25 chars long, alphanumeric and case insensitive.

Also I do not want to produce collisions so I need something collision free or at least not proven(yet?) to produce collisions.

Chris L.
  • 23
  • 4
  • I thought the whole point of a hash was to produce something smaller than the original data that you can use as a unique enough identifier. So collisions will always happen. I think you want something other than a hash. – johnnycrash Jun 03 '12 at 04:24
  • @johnnycrash the hash should be smaller, but that doesn't mean it's not useful to control the size the hash. Bloom filters require changing the length of the hash. – Justin Tanner Dec 04 '14 at 03:41

1 Answers1

1

Here is a lot of good stuff about different hash functions. I don't think any do what you are asking though. They all will have collisions.

Maybe you should check out some simple encryption algorithms.

Here is a simple encryption technique that might do what you want:

char szInput = "hash me", szOutput[20], szKey = "foo";
int i, cbKey = strlen(szKey), cbInput = strlen(szInput);

for (i=0 ; i<cbInput ; ++i)
  szOutput[i] = szInput[i]^szKey[i%cbKey];  // xor with a differnt char from the key

You won't recognize the output and it wont collide since it's reversible.

Another way that is harder to decipher, is to use the current char in the key as the count of calls you should make to rand(). Xor using the result of the last call to rand(). Since rand() always produces the same stream of numbers for a given seed, your "hash" wont't wont collide and can be decrypted.

If you want the hash to be "one way"...then throw away the key!

johnnycrash
  • 5,184
  • 5
  • 34
  • 58
  • Thank you! This is a great starting point. I guess I was searching in the wrong direction as you mentioned in your comment. – Chris L. Jun 29 '12 at 11:13