Question

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.

Was it helpful?

Solution

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top