# 2 comparable hashes generated from one string

Assume a website where people log in with their password.

The password is now stored in sql database as:

md5(password + random_salt)


We are adding the possibility for clients to stay logged in and I am trying to find out the best way to do that.

• Passwords should stay stored in the DB as hashes using some hash function (with the use of a generated salt) - first hash
• When client logs in with his password, a different hash of his password should be generated (using the previously generated salt) and stored in the relation "second hash".
• These two hashes should be comparable to each other, so that I could check if the second hash (generated during login) originates from the same string as the first one (generated during registration).

The second hash (the "login" one) would also be used to generate a key to encrypted sensitive user files (probably using AES, or is there a better option?).

I am quite new to this and I'm not sure if I am not being too naive, so I am asking. Is there a way to achieve this? Is there any source where I could read about these "comparable hashes"? I tried searching but I don't really know what to look for.

-
Quick observation: MD5 is not an appropriate password hash. See How to securely hash passwords? for how you should hash passwords. –  CodesInChaos Oct 10 '13 at 10:32
I would go with random tokens that are simply random values stored in a different database table. –  CodesInChaos Oct 10 '13 at 10:35
@CodesInChaos Better is random tokens that are issued to the browser, but hashed before being stored in the database. When a user presents the token to the server, hash it and compare to the stored value. This prevents the tokens column from being used to compromise user accounts in the event of a database breach. –  Stephen Touset Oct 10 '13 at 18:24
Cryptographic hashes are specifically designed to prevent the kind of relationship you are looking for, which would probably be considered a partial collision. I believe your premise of 2 different hashes is inherently flawed. –  Richie Frame Oct 10 '13 at 18:42
@CodesInChaos Thanks for the suggestion about md5. I've reviewed my options here and we are probably gonna implement PHP-recommended blowfish hashing. –  Adam Chyský Oct 10 '13 at 23:37

### Deriving an encryption key and a password hash from a single password

Hash the password, along with a per-account salt.

hash1 = SHA256(password, salt)


hash1 is your encryption passphrase that you pass to GPG.

hash2 = SHA256(hash1)


You keep hash2 in your database to figure out when a user has logged in correctly.

(For additional security, change the first SHA256 out for scrypt, tuned for 50 milliseconds. More trouble than it's worth, IMO.)

### Encrypting a file

Use a library like GnuPG. If you don't have GnuPG bindings for your language (I'm guessing you're using PHP), you can hack something together by running gpg from your program with the -c and --passphrase arguments.

Note that if the user changes their password, you need to decrypt and re-encrypt using the new password.

-
I wonder why this did not come up to my mind. Thanks, that's it. –  Adam Chyský Oct 10 '13 at 23:26
I'd highly recommend using correctly scrypt, bcrypt, PBKDF2, or any other well-known password hash. Such practice is more foolproof than ad-hoc use of SHA256. Proper password hash will correctly combine password and other information like salt. Proper password hash will make breaking low entropy user passwords much slower, if the password DB is ever gotten by a third party. –  user4982 Oct 11 '13 at 22:00
@user4982 There's an opportunity cost involved in setting up scrypt. It takes up development time that could be used to lock down unneeded services, to set up AppArmor, to look for SQL injection/XSS/CSRF, or to install fail2ban. All of those things take higher priority in my mind. –  Nick ODell Oct 11 '13 at 23:49
@NickODell I'm aware of some practical concerns in software development. However, usually they should be excluded from the concerns of crypto.SE. They are the field of IT Security. In cryptography, it is just currently recommended practice to just consider normal hash functions (like MD5, SHA256, etc.) incompatible with passwords (or some other low-entropy inputs) and use password-based key derivation functions when such input is used. Generally there exists libraries for that (even for, say, PHP). It is much more likely to get things correctly that way than by rolling your own scheme. –  user4982 Oct 13 '13 at 9:56