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.

  • 1
    $\begingroup$ What's wrong with SHA-whatever? $\endgroup$ – fkraiem Apr 22 at 17:50
  • $\begingroup$ it'll generate an alpha-numeric output, I need a numeric value. $\endgroup$ – Bhavay Anand Apr 22 at 18:20
  • $\begingroup$ A bitstring can be interpreted as an integer in a natural way. $\endgroup$ – fkraiem Apr 22 at 18:39
  • $\begingroup$ See also crypto.stackexchange.com/questions/64194/… and its duplicate. $\endgroup$ – Ilmari Karonen Apr 22 at 18:47

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.)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.