©
이 문서에서는 PHP 중국어 웹사이트 매뉴얼 풀어 주다
(PHP 7)
random_bytes — Generates cryptographically secure pseudo-random bytes
$length
)Generates 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:
length
The length of the random string that should be returned in bytes.
Returns a string containing the requested number of cryptographically secure random bytes.
length
of bytes is given, an
Error will be thrown.
Example #1 random_bytes() example
<?php
$bytes = random_bytes ( 5 );
var_dump ( bin2hex ( $bytes ));
?>
以上例程的输出类似于:
string(10) "385e33f741"
[#1] niklongstone at gmail dot com [2015-09-30 20:12:01]
In order to handling better the result, could be useful to combine random_bytes with the unpack function, because gives more flexibility.
Example:
$bytes = random_bytes(10);
$value = unpack('H*', $bytes);
var_dump($value);
Output:
array(1) { [1]=> string(20) "5f655db3ae43c256937b" }