It is currently said that MD5 is partially unsafe. Taking this into consideration, I’d like to know which mechanism to use for password protection.
This question, Is “double hashing” a password less secure than just hashing it once?
suggests that hashing multiple times may be a good idea, whereas How to implement password protection for individual files? suggests using salt.
I’m using PHP. I want a safe and fast password encryption system. Hashing a password a million times may be safer, but also slower. How to achieve a good balance between speed and safety? Also, I’d prefer the result to have a constant number of characters.
- The hashing mechanism must be available in PHP
- It must be safe
- It can use salt (in this case, are all salts equally good? Is there any way to generate good salts?)
Also, should I store two fields in the database (one using MD5 and another one using SHA, for example)? Would it make it safer or unsafer?
In case I wasn’t clear enough, I want to know which hashing function(s) to use and how to pick a good salt in order to have a safe and fast password protection mechanism.
Related questions that don’t quite cover my question:
What’s the difference between SHA and MD5 in PHP
Simple Password Encryption
Secure methods of storing keys, passwords for asp.net
How would you implement salted passwords in Tomcat 5.5
8
14 Answers
DISCLAIMER: This answer was written in 2008.
Since then, PHP has given us
password_hash
andpassword_verify
and, since their introduction, they are the recommended password hashing & checking method.The theory of the answer is still a good read though.
TL;DR
Don’ts
- Don’t limit what characters users can enter for passwords. Only idiots do this.
- Don’t limit the length of a password. If your users want a sentence with supercalifragilisticexpialidocious in it, don’t prevent them from using it.
- Don’t strip or escape HTML and special characters in the password.
- Never store your user’s password in plain-text.
- Never email a password to your user except when they have lost theirs, and you sent a temporary one.
- Never, ever log passwords in any manner.
- Never hash passwords with SHA1 or MD5 or even SHA256! Modern crackers can exceed 60 and 180 billion hashes/second (respectively).
- Don’t mix bcrypt and with the raw output of hash(), either use hex output or base64_encode it. (This applies to any input that may have a rogue
openwall.com/phpass is also very good library
Md5 is now completely unsafe
@NSAwesomeGuy That depends on what you’re using it for. It’s trivial to rainbow-match or just brute force unsalted MD5 passwords, sure, but with decent salting it’s still exceedingly impractical to build a rainbow table for fast cracking of sets of passwords, and brute force is a no-hoper.
PHP 5.5+ has a secure password hash built in php.net/manual/en/function.password-hash.php
php.net/faq.password