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
I usually use SHA1 and salt with the user ID (or some other user-specific piece of information), and sometimes I additionally use a constant salt (so I have 2 parts to the salt).
SHA1 is now also considered somewhat compromised, but to a far lesser degree than MD5. By using a salt (any salt), you're preventing the use of a generic rainbow table to attack your hashes (some people have even had success using Google as a sort of rainbow table by searching for the hash). An attacker could conceivably generate a rainbow table using your salt, so that's why you should include a user-specific salt. That way, they will have to generate a rainbow table for each and every record in your system, not just one for your entire system! With that type of salting, even MD5 is decently secure.
Though the question has been answered, I just want to reiterate that salts used for hashing should be random and not like email address as suggested in first answer.
More explanation is available at- http://www.pivotalsecurity.com/blog/password-hashing-salt-should-it-be-random/
I'm using Phpass which is a simple one-file PHP class that could be implemented very easily in nearly every PHP project. See also The H.
By default it used strongest available encryption that is implemented in Phpass, which is
bcrypt
and falls back to other encryptions down to MD5 to provide backward compatibility to frameworks like Wordpress.The returned hash could be stored in database as it is. Sample use for generating hash is:
To verify password, one can use:
Google says SHA256 is available to PHP.
You should definitely use a salt. I'd recommend using random bytes (and not restrict yourself to characters and numbers). As usually, the longer you choose, the safer, slower it gets. 64 bytes ought to be fine, i guess.
THINGS TO REMEMBER
A lot has been said about Password encryption for PHP, most of which is very good advice, but before you even start the process of using PHP for password encryption make sure you have the following implemented or ready to be implemented.
SERVER
PORTS
No matter how good your encryption is if you don't properly secure the server that runs the PHP and DB all your efforts are worthless. Most servers function relatively the same way, they have ports assigned to allow you to access them remotely either through ftp or shell. Make sure that you change the default port of which ever remote connection you have active. By not doing this you in effect have made the attacker do one less step in accessing your system.
USERNAME
For all that is good in the world do not use the username admin, root or something similar. Also if you are on a unix based system DO NOT make the root account login accessible, it should always be sudo only.
PASSWORD
You tell your users to make good passwords to avoid getting hacked, do the same. What is the point in going through all the effort of locking your front door when you have the backdoor wide open.
DATABASE
SERVER
Ideally you want your DB and APPLICATION on separate servers. This is not always possible due to cost, but it does allow for some safety as the attacker will have to go through two steps to fully access the system.
USER
Always have your application have its own account to access the DB, and only give it the privileges it will need.
Then have a separate user account for you that is not stored anywhere on the server, not even in the application.
Like always DO NOT make this root or something similar.
PASSWORD
Follow the same guidelines as with all good passwords. Also don't reuse the same password on any SERVER or DB accounts on the same system.
PHP
PASSWORD
NEVER EVER store a password in your DB, instead store the hash and unique salt, I will explain why later.
HASHING
ONE WAY HASHING!!!!!!!, Never hash a password in a way that it can be reversed, Hashes should be one way, meaning you don't reverse them and compare them to the password, you instead hash the entered password the same way and compare the two hashes. This means that even if an attacker gets access to the DB he doesn't know what the actually password is, just its resulting hash. Which means more security for your users in the worst possible scenario.
There are a lot of good hashing functions out there (
password_hash
,hash
, etc...) but you need to select a good algorithm for the hash to be effective. (bcrypt and ones similar to it are decent algorithms.)When hashing speed is the key, the slower the more resistant to Brute Force attacks.
One of the most common mistakes in hashing is that hashes are not unique to the users. This is mainly because salts are not uniquely generated.
SALTING
Passwords should always be salted before hashed. Salting adds a random string to the password so similar passwords don't appear the same in the DB. However if the salt is not unique to each user (ie: you use a hard coded salt) than you pretty much have made your salt worthless. Because once an attacker figures out one password salt he has the salt for all of them.
When you create a salt make sure it is unique to the password it is salting, then store both the completed hash and salt in your DB. What this will do is make it so that an attacker will have to individually crack each salt and hash before they can gain access. This means a lot more work and time for the attacker.
USERS CREATING PASSWORDS
If the user is creating a password through the frontend that means it has to be sent to the server. This opens up a security issue because that means the unencrypted password is being sent to the server and if a attacker is able to listen and access that all your security in PHP is worthless. ALWAYS transmit the data SECURELY, this is done through SSL, but be weary even SSL is not flawless (OpenSSL's Heartbleed flaw is an example of this).
Also make the user create a secure password, it is simple and should always be done, the user will be grateful for it in the end.
Finally, no matter the security measures you take nothing is 100% secure, the more advanced the technology to protect becomes the more advanced the attacks become. But following these steps will make your site more secure and far less desirable for attackers to go after.
Here is a PHP class that creates a hash and salt for a password easily
http://git.io/mSJqpw
As of PHP 5.5, PHP has simple, secure functions for hashing and verifying passwords, password_hash() and password_verify()
When
password_hash()
is used, it generates a random salt and includes it in the outputted hash (along with the the cost and algorithm used.)password_verify()
then reads that hash and determines the salt and encryption method used, and verifies it against the provided plaintext password.Providing the
PASSWORD_DEFAULT
instructs PHP to use the default hashing algorithm of the installed version of PHP. Exactly which algorithm that means is intended to change over time in future versions, so that it will always be one of the strongest available algorithms.Increasing cost (which defaults to 10) makes the hash harder to brute-force but also means generating hashes and verifying passwords against them will be more work for your server's CPU.
Note that even though the default hashing algorithm may change, old hashes will continue to verify just fine because the algorithm used is stored in the hash and
password_verify()
picks up on it.