Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the recommended way to encrypt user passwords in a database?

In a web application written in Perl and using PostgreSQL the users have username and password. What would be the recommended way to store the passwords?

Encrypting them using the crypt() function of Perl and a random salt? That would limit the useful length of passswords to 8 characters and will require fetching the stored password in order to compare to the one given by the user when authenticating (to fetch the salt that was attached to it).

Is there a built-in way in PostgreSQL to do this?

Should I use Digest::MD5?

like image 956
szabgab Avatar asked Jan 07 '10 09:01

szabgab


People also ask

What is the best encryption for passwords?

Google recommends using stronger hashing algorithms such as SHA-256 and SHA-3. Other options commonly used in practice are bcrypt , scrypt , among many others that you can find in this list of cryptographic algorithms.

What is the best practice in storing passwords in a database?

In most cases, storing the representation of a password in the database is the proper thing to do. To do this, you have to hash the password using a different salt for every user using a one-way algorithm and store the result, removing the original password.

Should you store passwords in a database?

It sounds simple but needs to be done carefully to make sure your users are protected against data breaches or potential security failures. In a nutshell, you should never really store user passwords in the database! Yes, you read it right. User passwords as is (otherwise called plain-text) should never be stored.


1 Answers

Don't use SHA1 or SHA256, as most other people are suggesting. Definitely don't use MD5.

SHA1/256 and MD5 are both designed to create checksums of files and strings (and other datatypes, if necessary). Because of this, they're designed to be as fast as possible, so that the checksum is quick to generate.

This fast speed makes it much easier to bruteforce passwords, as a well-written program easily can generate thousands of hashes every second.

Instead, use a slow algorithm that is specifically designed for passwords. They're designed to take a little bit longer to generate, with the upside being that bruteforce attacks become much harder. Because of this, the passwords will be much more secure.

You won't experience any significant performance disadvantages if you're only looking at encrypting individual passwords one at a time, which is the normal implementation of storing and checking passwords. It's only in bulk where the real difference is.

I personally like bcrypt. There should be a Perl version of it available, as a quick Google search yielded several possible matches.

like image 115
vonconrad Avatar answered Oct 10 '22 12:10

vonconrad