random_bytes
laravel/framework
Supported Versions: PHP 7, PHP 8
Generates cryptographically secure pseudo-random bytes
random_bytes
(
int $length
):
string
public static function
generateKey
(
$cipher
)
{
return
random_bytes
(
self
::
$supportedCiphers
[
strtolower
(
$cipher
)][
'size'
] ??
32
);
}
src/Illuminate/Encryption/Encrypter.php
public function
encrypt
(
$value
,
$serialize
=
true
)
{
$iv
=
random_bytes
(
openssl_cipher_iv_length
(
strtolower
(
$this
->
cipher
)));
$value
=
\openssl_encrypt
(
$serialize
?
serialize
(
$value
) :
$value
,
src/Illuminate/Encryption/Encrypter.php
while ((
$len
=
strlen
(
$string
)) <
$length
) {
$size
=
$length
-
$len
;
$bytes
=
random_bytes
(
$size
);
$string
.=
substr
(
str_replace
([
'/'
,
'+'
,
'='
],
''
,
base64_encode
(
$bytes
)),
0
,
$size
);
}
src/Illuminate/Support/Str.php
$mock
=
m
::
mock
(
Model
::class, [
'getKey'
=>
random_bytes
(
10
),
'getQueueableId'
=>
'mocked'
,
]);
tests/Database/DatabaseEloquentCollectionQueueableTest.php
public function
testEncryptionUsingBase64EncodedKey
()
{
$e
= new
Encrypter
(
random_bytes
(
16
));
$encrypted
=
$e
->
encrypt
(
'foo'
);
$this
->
assertNotSame
(
'foo'
,
$encrypted
);
$this
->
assertSame
(
'foo'
,
$e
->
decrypt
(
$encrypted
));
tests/Encryption/EncrypterTest.php
$this
->
assertNotSame
(
'bar'
,
$encrypted
);
$this
->
assertSame
(
'bar'
,
$e
->
decrypt
(
$encrypted
));
$e
= new
Encrypter
(
random_bytes
(
32
),
'AES-256-GCM'
);
$encrypted
=
$e
->
encrypt
(
'foo'
);
$this
->
assertNotSame
(
'foo'
,
$encrypted
);
$this
->
assertSame
(
'foo'
,
$e
->
decrypt
(
$encrypted
));
tests/Encryption/EncrypterTest.php