Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to store passwords in a mysql database? [duplicate]

Possible Duplicate:
How to best store user information and user login and password
How do you use bcrypt for hashing passwords in PHP?

I am used to using the md5() which I know is now outdated and I hear that sha1() is also insecure. So what is exactly the best way to store and retrieve passwords in a database these days with security in mind? I'd be very happy if you can provide a small example.

Thank you!

like image 490
user1296953 Avatar asked Sep 20 '12 19:09

user1296953


2 Answers

I would recommend looking at bcrypt, since it can help against brute-force attacks. http://codahale.com/how-to-safely-store-a-password/

You can find example Here

like image 130
nKandel Avatar answered Oct 05 '22 01:10

nKandel


You should really use bcrypt to hash your passwords, it was designed especially for hashing password.

Hash functions for passwords should be slow (need some computing time). Most hash algorithms like SHA-1 and MD5 or even SHA-256 are designed to be fast, but this makes it an easy target for brute force attacks. An off-the-shelf GPU is able to calculate about 8 Giga MD5 hashes per second!

Don't be afraid to use bcrypt! It is not for high security sites only, and using it can be as easy, as using an md5 hash. It's recommended to use a well established library like phpass, and if you want to understand how it can be implemented, you can read this article, where i tried to explain the most important points.

UPDATE:

Current PHP versions offers the functions password_hash() and password_verify() to handle passwords. Use them like this:

// Hash a new password for storing in the database.
// The function automatically generates a cryptographically safe salt.
$hashToStoreInDb = password_hash($password, PASSWORD_DEFAULT);

// Check if the hash of the entered login password, matches the stored hash.
// The salt and the cost factor will be extracted from $existingHashFromDb.
$isPasswordCorrect = password_verify($password, $existingHashFromDb);
like image 27
martinstoeckli Avatar answered Oct 05 '22 02:10

martinstoeckli