0

I need to generate a unique 8 character string from a sequential integer (0, 1, 2, 3, etc).

I tried hashing the int with md5/sha256/sha512 and then shortening it to 8 characters but there are quite a lot of collisions which I want to try and avoid if possible.

I've looked into algorithms such as crc32 but the hash produced from that contains too many numbers for my liking.

Can anybody suggest an alternative method of doing what I need?

Ryan
  • 957
  • 5
  • 16
  • 34
  • 2
    You only need it to be unique? If you don't need real hashing you can use the binary representation of the integer (padded with zeros). It's unique – Haile Apr 15 '14 at 20:28
  • Yeah, it's certainly unique. The only issue is that i'd prefer if the hash at least looked somewhat random, take the video ID of a youtube video, for example. I know this is a bit of a weird request for sure. – Ryan Apr 15 '14 at 20:33
  • However it's quite suspicious that you find lots of collisions when you truncate the output of a real hash function like SHA256 and take 56 bits. I don't think this is supposed to happen.. unless you hash like billions of integers. – Haile Apr 15 '14 at 20:47
  • I get ~12k collisions when I hash 10 million integers (with sha256) – Ryan Apr 15 '14 at 20:51
  • How are you trying to use the string? I.e. If its like youtube, you're going to store that string somewhere for a reverse lookup table. If that's the case, you could just generate random characters and test if they are in the table and if so try another set. – Mike Apr 17 '14 at 12:43

0 Answers0