random_bytes() - php 伪随机数产生器
random_bytes()
版本:php7
(PHP 7)
Generates cryptographically secure pseudo-random bytes
说明
random_bytes(int $length): stringGenerates an arbitrary length string of cryptographic random bytes that are suitable for cryptographic use, such as when generating salts, keys or initialization vectors.
The sources of randomness used for this function are as follows:
- On Windows,»CryptGenRandom()will always be used. As of PHP 7.2.0,the» CNG-APIwill always be used instead.
- On Linux,the» getrandom(2)syscall will be used if available.
- On other platforms,/dev/urandomwill be used.
- If none of the aforementioned sources are available, then anExceptionwill be thrown.
Note:Although this function was added to PHP in PHP 7.0,a» userland implementationis available for PHP 5.2 to 5.6, inclusive.
参数
$lengthThe length of the random string that should be returned in bytes.
返回值
Returns a string containing the requested number of cryptographically secure random bytes.
错误/异常
- If an appropriate source of randomness cannot be found,anExceptionwill be thrown.
- If invalid parameters are given,aTypeErrorwill be thrown.
- If an invalid$lengthof bytes is given,anErrorwill be thrown.
范例
Example #1random_bytes()example
以上例程的输出类似于:
string(10) "385e33f741"
参见
random_int()
Generates cryptographically secure pseudo-random integersopenssl_random_pseudo_bytes()
生成一个伪随机字节串bin2hex()
函数把包含数据的二进制字符串转换为十六进制值
I used below function to create random token, and also a salt from the token. I used it in my application to prevent CSRF attack.
Simple php implementation of Blum Blum Shub.
function str_rand($largura = 32){ $chars = str_shuffle('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'); // separar a string acima com uma virgula após cada letra ou número; $chars = preg_replace("/([a-z0-9])/i", "$1,", $chars); $chars = explode(',', $chars); $string_generate = array(); for($i = 0; $i = 4){ $random_string_start = str_shuffle(substr($random_string, 0, $largura / 2)); $random_string_end = str_shuffle(substr($random_string, $largura / 2, $largura)); $new_random_string = str_shuffle($random_string_start . $random_string_end); return str_shuffle($new_random_string); } else { return str_shuffle($random_string); } }
if unavailable use this with core functions... maybe not as secure and optimized (any help?), but practical
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)