Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I have read a lot about PHP login security recently, but many questions on Stack Overflow regarding security are outdated.

I understand bcrypt is one of the best ways of hashing passwords today. However, for my site, I believe sha512 will do very well, at least to begin with. (I mean bcrypt is for bigger sites, sites that require high security, right?)

I´m also wonder about salting. Is it necessary for every password to have its own unique salt? Should I have one field for the salt and one for the password in my database table? What would be a decent salt today? Should I join the username together with the password and add a random word/letter/special character combination to it?

share|improve this question

migrated from stackoverflow.com Jun 2 '12 at 12:02

This question came from our site for professional and enthusiast programmers.

This will be closed due to the open-ended-ness of the question (and due to the many similar questions out there - seriously, password security haven't changed much the last decade or so). My 2 cents about the salt is that it doesn't need to be that involved. Just generate a random salt for each user. You don't need the username or anything like that. The random salt is just to make it harder to build rainbow tables for bruteforcing the entire database. –  Emil Vikström Jun 2 '12 at 11:49
I've written my own class for password encryption many years ago and it still works fine. I suggest you do something like that. And another advise there are nothing like security for the big site and security for the small site, security is security and always must be as good as possible (maybe sometimes even more). –  PLB Jun 2 '12 at 11:50
@EmilVikström When you say random salt, should it be unique for every user? Or just a random salt that I use for every password? –  piers Jun 2 '12 at 11:55
A random salt for each user. The point of this is that an attacker should only be able to bruteforce one single password at a time, not all of them at once. You don't need to check that it's unique, just generate something random and it will be unique enough. –  Emil Vikström Jun 2 '12 at 11:57
read : blog.ircmaxell.com/2012/04/… –  tereško Jun 2 '12 at 12:02

2 Answers 2

I'm not sure why this question was migrated.. there are quite some questions with valid answers on stackoverflow about this.

First: use BCrypt-hash, it is the recommended hashing algorithm today.

Sha256 is a general purpose hashing algorithm, designed to be fast; you do not want your hashing algorithm to be fast for password hashing.

Second: use a random, evenly distributed, high entropy salt. I also invite you to read my long answer on salting

Depending on your programming language/platform, the BCrypt algorithm already creates a salt for you. However, not all implementations do this; PHP for example does not auto create a salt.

If at all possible, don't roll your own script. But use a standard library.
Security tends to be a lot more complicated and with more invisible screw up possibilities than most programmers could tackle alone, so, using a standard library is almost always easiest and most secure (if not the only) available option.

share|improve this answer
Nice explanation about salting. –  martinstoeckli Jun 5 '12 at 9:43

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 won't make your application more complicated to have a random salt per password, it can be stored in the same database field as the hash value.

Since version 3.5 PHP you can use the crypt() function to generate a bcrypt hash. In the article Generating password hashes with bcrypt i tried to explain the important points, one should understand about bcrypt.

share|improve this answer
With PHP, safe yourself the risc of making mistakes and use PHPass instead. –  Jacco Jun 5 '12 at 11:31
The linked example uses mcrypt_create_iv(). This function has been shown to fail in producing a sufficient random return value is some cases. In other words, the linked implementation is, as a result, not guaranteed to be secure. –  Jacco Jun 5 '12 at 11:37
@Jacco - PHPass is a good library, but it doesn't explain how it works. I know it's a dogma, that one should use an established library, but i'm convinced, that one should first understand the problem, and then afterwards one can use a well known library. The example code is commented well, to show what is going on. –  martinstoeckli Jun 5 '12 at 11:38
@Jacco - Since PHP version 3.5 mcrypt_create_iv() works as well on Windows servers. It reads from /dev/urandom, or from the Windows crypto API, so i'm very interested, where the failing is. I'm aware that before 3.5 it was not safe to use it on Windows systems. –  martinstoeckli Jun 5 '12 at 11:40
The insecurity is with PHP < 3.5 on systems other than linux/unix based. Since the prevalent PHP version is still 5.2, and slowly moving into the 5.3 realm, a set of functions that rely on features PHP >= 5.3 should be labeled as insecure, or it should fail to work on anything below the required version. –  Jacco Jun 6 '12 at 6:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.