php 随机密码和盐 来自wordpress

生成随机密码或盐。

Generate keys and salts using secure CSPRNG

$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|';
//字符打乱
$chars = str_shuffle($chars);
$max = strlen($chars) - 1;
for ( $i = 0; $i < 8; $i++ ) {
	$key = '';
	for ( $j = 0; $j < 64; $j++ ) {
		//如果是php mt_rand函数可换成random_int
		$key .= substr( $chars, mt_rand( 0, $max ), 1 );
	}
	$secret_keys[] = $key;
}

//输出
print_r($secret_keys);

//from http://www.cnblogs.com/osfipin/

  输出结果演示:

如果不是php7也想使用random_int(可以产生更高质量的随机数)函数,可以使用 Paragon Initiative 公司的 random_compat 库。

posted @ 2017-02-08 09:15  osfipin  阅读(640)  评论(0编辑  收藏  举报
我是底部