sha256shasha2

Convert hash-256 generated from an string of digits to 64/32 bit unique number in Java


I have an account number as string ex: "98932849294322" on which I perform a 256 Hash calling DigestUtils.sha256Hex(value) which returns a 64 char encoded string. Now I want to convert the 256 value into a number either from the 64-byte hex or raw binary from a sha-256. My purpose is to have a new number that I can use to refer to the original account number which should map one-to-one to the new number. My two questions are:

1- What function I need to call to get the number from the value returned from a sha256 call.

2- Would it be a collision from the number obtained in 1). Let say I have two accounts "1245" and "6543" would they ever generate the same hash therefore getting the same number in 1) from two different account numbers?

Thanks


Solution

  • Yes and No. No. The problem is that two different strings can render the same SHA value. Therefore only using SHA this is not possible. Yes. However if I perform extra algorithmic operations on the original string and given that the string is of limited length (12) I can generate a 32 Bit number and use something like a shift operator and then pad with the result of my custom algorithm on the string and pad the 32 extra bits to generate a unique 64 bit number.