Consider the following scenario:
- Users enter unique codes (say something like a gift card) on a website.
- The code corresponds to an object in the database which must be retrieved.
- The code is a secret and cannot be stored as plain text.
- Instead, the code will be hashed and stored in the database. The hash algorithm will be sha-512 or bcrypt combined with some salting strategy.
In order to look up the code, a hash of the user entered code must be taken. Typically, in the case of password authentication, the identity of the user is already known and thus the salt can be retrieved from the database before computing the hash. In the above scenario though it's not possible to load the salt associated with the code since we don't know which object in the database the code corresponds to. This seems to imply there is no such salting strategy for this scenario for which the salts can be random.
I would like input on the following ideas:
Can we hash (say sha2) of the user entered code to act as the salt?
salt = sha2(code)
hashedCode = hash(code + salt)
If there are vulnerabilities of the above, can including some additional global secret as part of the hash help alleviate the risk?
salt = sha2(code + globalSecret)
hashedCode = hash(code + salt)
Thanks!