微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 从/ dev / urandom转换随机值

正如comments of mt_rand()中提到的那样,安全性很弱,我们应该使用/ dev / urandom.我的问题是,从urandom我得到一个二进制字符串.

如何将此二进制字符串转换为0-9a-zA-Z?

看起来像base_convert()在这里不起作用.

解决方法:

仅供记录全功能

function randomFromDev($len)
{
    $fp = @fopen('/dev/urandom','rb');
    $result = '';
    if ($fp !== FALSE) {
        $result .= @fread($fp, $len);
        @fclose($fp);
    }
    else
    {
        trigger_error('Can not open /dev/urandom.');
    }
    // convert from binary to string
    $result = base64_encode($result);
    // remove none url chars
    $result = strtr($result, '+/', '-_');
    // Remove = from the end
    $result = str_replace('=', ' ', $result);
    return $result;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐