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
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!