I'm trying to convert a data field to something tokenized(that's to something not-sensitive). The current scenario is, I've got a data field (assume user-id) which is unique for each user, I don't want to store that, rather I want to run a function on it and then store the output value. All the user-ids are unique(no two users have the same id).
Now, I want the following properties:
It should be secure, that is nobody should be able to derive the original user-id from the output. (This hints towards using a hash function, preferably SHA-2 or SHA-3 as they are more secure than MD-5 & SHA-1)
The output should be the same each time for a user i.e. if I run the function hundreds time on a user-id, the output should remain the same.
It should be computationally efficient.
To achieve the same, I'm thinking to use the following procedure:
- Compute the hash of the hash of user-id & salt concatenated with the salt value on both sides. It will help up to some extent in preventing birthday attack, and length extension attack. I'll be using sufficiently large salt to counter birthday attack.
- The hash output size of SHA-512, as the name suggests is, 512-bits or 64-bytes. I'll remove the first 4-bytes of the output and store the rest in the database. As the output is still long(60-bytes), the probability of collision will be almost negligible and for my current use-case, it doesn't matter if I have some collisions. This way, even if the database gets compromised, there is no way to know for sure that the given hash_output belongs to the particular user.
According to my use-case, it is computationally efficient. But is this procedure secure?
PS: In case this detail matters, all of this will be happening on the back-end. Once I get the data field, the information is never shown to the user again.