0

I need to create a unique numerical value for every alpha-numeric string input. Size of output numerical value can be of any size like 32, 64 128 bits. The input string will always be a mongoID type if that helps. Is there any hashing algorithm available for such purpose.

Bhavay Anand
  • 111
  • 2

1 Answers1

1

If you don't limit the length, there are infinitely many alphanumeric strings. The number of even 128-bit numbers is finite, so there's no way to assign a unique 128-bit number to every possible string.

That said, if you don't mind a very small risk of collisions, a cryptographic hash function like SHA-2 or SHA-3 truncated to your desired output length should be fine, as long as that output length is sufficiently large. With e.g. a 128-bit output size, the average number of strings you'd need to hash before observing even a single collision is around $2^{64}$, probably far more than your software will ever see. If you're really feeling paranoid, bump the length up to 256 bits.

(A point I should probably clarify is that hash functions like SHA-2 and SHA-3 output a fixed-length block of bits. While this output is often represented in hexadecimal for convenience, it's really just a large binary number and could just as well be displayed in any other base, e.g. in the plain old decimal system we humans like to use.)

Ilmari Karonen
  • 46,120
  • 5
  • 105
  • 181