1

I have a new user stored in my DB

Email (username) is encrypted using openssl_encrypt()

Password is hashed using password_hash()

The problem I have that I cannot easily look the user up since the email is encrypted using random IV and password hashed using a random salt.

The only option I see here is to add a new column "email_hashed"

Now I have to: fetch through all DB records :-(. When I find a match between email_hashed and hashed user email input using password_verify() I can check the password again with password_verify()

I understand the situation is simple when the "username" is stored as plain text but it is not my case. I want my customers data to be encrypted including the e-mail address

Is there a more efficient way than going through all DB records to authenticate the user when email is used as a login-name?

Hope the question is not too dumb :-)

3
  • If you want to store the username encrypted I would recommend using bcrypt with a salt. The salt should also be part of the stored string. That way you would be able to re-create the username hash and use it for the lookup. Commented Aug 23, 2018 at 15:33
  • @LazerBass lets say I have 3000 customers saved. Even though I have the salt stored with the string (each salt randomly generated I suppose?) I still need to fetch all records, take all salts stored and add it to user email input to go back to DB and find the correct record .... am I missing something? Commented Aug 25, 2018 at 12:26
  • You are right. I missed that. Commented Aug 25, 2018 at 14:26

1 Answer 1

0

Salting prevents that hashes are retrievable when searching the database, so does the IV/nonce used for encryption.

This leaves only a second best method for storing the user identification in retrievable form, it can be hashed but using a key instead of a salt. One could e.g. calculate a PBKDF2 or a BCrypt hash but the same salt has to be used for all hashes and it must not be stored in the database as part of the hash-string. This is of course suboptimal, and we should choose a high cost factor so it needs a lot of time to build a rainbow table.

This is a similar problem messengers like Signal have, when they want to offer anonymity, but allow to make contact with users in the adress book, where the telephone number is the user identification. Maybe you can find better ideas when you study the way Signal uses to solve this problem.

P.S. This question would fit in https://security.stackexchange.com/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.