Keeping your Users’ Passwords Safe
So the question is what’s the best way to encrypt passwords in your database?
I’ll be looking at a couple of different ways to encrypt passwords (sorted weakest to strongest):
- Hashing algorithms
- Salt + hash
- Individual salt + hash
- Library like Bcrypt
Storing plain-text passwords in your database is the dumbest thing you could do. The next dumbest is only using a one-way hash (dumb is a strong word here, but let’s roll with it).
One-way hashing algorithms are nearly impossible to reverse (depends on the hashing algorithm you’re using). However, it’s computationally easy for a hacker who has access to your database to run through a list of the most common passwords to find matches.
One step to make this more difficult and computationally more expensive is to include a password “salt”.
Salt + hash
A salt is a piece of data that is added to your plain text password before it is hashed to make reversing it more difficult. A salt + hash is your next best attempt at securing your users’ passwords.
However, if a hacker made their way to your database, they probably have access to your app’s salt as well. The salt will throw another obstacle in the hacker’s way, but it’s still relatively easy for them to loop through common passwords to break your database.
Maybe it would be better if we had a unique salt for each user row in our database?
Individual salt + hash
If you’re sensing a theme about making it more computationally intensive for a hacker to crack passwords, then you’re right.
The purpose of a unique salt for each user row in your database is to make it even more difficult for a hacker to decrypt your passwords. Individual salts will make it take even longer for a hacker to loop through common passwords to decrypt everything.
That said, it is still possible. Especially as computational power increases, hackers can break your system in a reasonable amount of time.
Introducing libraries like Bcrypt
Bcrypt is a unique hashing algorithm that was created for password hashing. Bcrypt runs an encryption hash multiple times in a loop. That means your password is hashed multiple times based on the number of loops you define. This will result in a slower experience for your user, but the additional milliseconds will be almost unnoticeable.
For hackers on the other hand, you’ve made decrypting your passwords extremely expensive (both in time and server costs). Bcrypt even enables you to increase the amount of encryption loops, so you can keep up as computers become more powerful.
As an added bonus, Bcrypt allows you to add a salt to each user’s record…that makes it the real winner :thumbs_up:
Another tip to keep passwords safe
This type is simple… require stricter guidelines for users when they create their password. Don’t let them use any word in the dictionary, make them add numbers, punctuation, capitalization, etc. This will make it nearly impossible for any hacker to use a set of common passwords to break your hashing system.
This isn’t the best user experience, but your users will thank you later.
Did I miss anything? How do you keep your passwords safe?