How to generate a random, long salt for use in hashing?

前端 未结 4 1415
遇见更好的自我
遇见更好的自我 2020-12-15 07:42

What is a way in PHP to make a random, variable length salt for use in hashing? Let\'s say I want to make a 16-character long salt - how would I do it?

4条回答
  •  孤街浪徒
    2020-12-15 08:17

    depending on your OS, something like:

    $fh=fopen('/dev/urandom','rb');
    $salt=fgets($fh,16);
    fclose($fh);
    

    Do read up on the behaviour of random and urandom.

    While others have correctly pointed out that there some issues with md5 and repeated hashing, for passwords (i.e. relatively short strings) brute force attacks take the same amount of time regardless of how sophisticated the hashing algorithm is.

    C.

提交回复
热议问题