https://github.com/DomBlack/php-scrypt/blob/master/scrypt.php#L69-L111
This code seems to do the following to try generate a salt:
- Try use mcrypt_create_iv
- Try use openssl_random_pseudo_bytes
- Try use /dev/urandom
- use mt_rand
This is a flawed approach (mainly because of windows/lack of sanity checking/usage of mt_rand). In general, you should consider the following path:
- Use libsodium if available.
- fread() /dev/urandom if available (never on Windows)
- mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM)
- COM('CAPICOM.Utilities.1')->GetRandom()
- openssl_random_pseudo_bytes() (absolute last resort)
- if none of the above, FAIL. Your execution can't continue past this point if the platform is unable to give you sufficient randomness for the salt.
You also must check when you read /dev/urandom that you've been provided with a char device, rather than something pretending to be /dev/urandom.
To be honest, though, it's best to just look at including/requiring something like https://github.com/paragonie/random_compat/releases and just using random_bytes() as this'll then ensure you get the right amount of randomess on any platform that this code could possibly run under.
https://github.com/DomBlack/php-scrypt/blob/master/scrypt.php#L69-L111
This code seems to do the following to try generate a salt:
This is a flawed approach (mainly because of windows/lack of sanity checking/usage of mt_rand). In general, you should consider the following path:
You also must check when you read /dev/urandom that you've been provided with a char device, rather than something pretending to be /dev/urandom.
To be honest, though, it's best to just look at including/requiring something like https://github.com/paragonie/random_compat/releases and just using
random_bytes()as this'll then ensure you get the right amount of randomess on any platform that this code could possibly run under.