首页 > Web开发 > 详细

[PHP]利用openssl_random_pseudo_bytes和base64_encode函数来生成随机字符串

时间:2015-04-09 23:46:38      阅读:421      评论:0      收藏:0      [点我收藏+]

openssl_random_pseudo_bytes函数本身是用来生成指定个数的随机字节,因此在使用它来生成随机字符串时,还需要配合使用函数base64_encode。如下所示:

public static function getRandomString($length = 42)
    {
        /*
         * Use OpenSSL (if available)
         */
        if (function_exists(openssl_random_pseudo_bytes)) {
            $bytes = openssl_random_pseudo_bytes($length * 2);

            if ($bytes === false)
                throw new RuntimeException(Unable to generate a random string);

            return substr(str_replace([‘/‘, ‘+‘, ‘=‘], ‘‘, base64_encode($bytes)), 0, $length);
        }

        $pool = 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ;

        return substr(str_shuffle(str_repeat($pool, 5)), 0, $length);
    }

在调用base64_encode函数之后,还对结果进行了一次替换操作,目的是要去除随机生成的字符串中不需要的字符。

当然,在使用openssl_random_pseudo_bytes函数之前,最好使用function_exists来确保该函数在运行时是可用的。如果不可用,则使用Plan B:

substr(str_shuffle(str_repeat($pool, 5)), 0, $length);

这个函数的通用性很强,可以根据业务的需要进行适当修改然后当作静态方法进行调用。

[PHP]利用openssl_random_pseudo_bytes和base64_encode函数来生成随机字符串

原文:http://blog.csdn.net/dm_vincent/article/details/44967205

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!