美文网首页
php生成安全的随机字符串

php生成安全的随机字符串

作者: helloHdb | 来源:发表于2017-02-10 15:29 被阅读0次
    • php 通过读取linux 下的/dev/urandom 设备获取安全的随机数

    • 微信小程序推荐使用16B的随机数 也就是128位

       //获取安全的随机字符串
    
        public function getSafetyRandomString()
        {
            $random_string = '';
            try {
                $fp = @fopen('/dev/urandom', 'r');
                if ($fp !== false) {
                    $random_string .= @fread($fp, 16);
                    $pr_bits_arr = unpack('H*', $random_string);
                    $random_string = $pr_bits_arr[1];
                    @fclose($fp);
                }
    
            } catch (\Exception $e) {
                error_log($e->getMessage());
            }
            return $random_string;
        }
        
        //得到的结果如  :  063060b430eea54f49b8795aeaa08038 
        
    

    相关文章

      网友评论

          本文标题:php生成安全的随机字符串

          本文链接:https://www.haomeiwen.com/subject/fbwgittx.html