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

前端 未结 4 1412
遇见更好的自我
遇见更好的自我 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:18

    Here is I found a function to generate random string:

    /* random string */
    function rand_string( $length ) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";  
        $size = strlen( $chars );
        for( $i = 0; $i < $length; $i++ ) {
            $str .= $chars[ rand( 0, $size - 1 ) ];
        }
        return $str;
    }
    

提交回复
热议问题