The right way to generate a salt for password hashing is: do not do it yourself. Use a library which already does thing properly. See @Terry's answer for pointers.
For your exact question, it so happens that openssl_random_pseudo_bytes()
relies on OpenSSL's internal PRNG, which itself feeds on what the underlying platform provides, i.e. dev/urandom
, so it is safe. Strictly speaking, you should use the second parameter of that function to check whether OpenSSL did find a strong source of randomness on the local platform (see the documentation), but in practice, as long as OpenSSL runs on a Unix-like or Windows-like platform, things will be fine. So it does not really matter, for security, whether you call openssl_random_pseudo_bytes()
or read /dev/urandom
yourself. For maintenance reasons, I would prefer the former, which is simpler (only one call) and more portable (it will also work on Windows, whereas reading /dev/urandom
will not).
Either way, applying SHA-1 on the output of openssl_random_pseudo_bytes()
or /dev/urandom
, is totally useless. Good PRNG already produce unpredictable uniformly random bytes (and if your PRNG is not good, then why are you using it ?).