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);

    }
    public function encrypt($value$serialize true)

    {

        
$iv random_bytes(openssl_cipher_iv_length(strtolower($this->cipher)));



        
$value \openssl_encrypt(

            
$serialize serialize($value) : $value,
            while (($len strlen($string)) < $length) {

                
$size $length $len;



                
$bytes random_bytes($size);



                
$string .= substr(str_replace(['/''+''='], ''base64_encode($bytes)), 0$size);

            }
        $mock m::mock(Model::class, [

            
'getKey' => random_bytes(10),

            
'getQueueableId' => 'mocked',

        ]);
    public function testEncryptionUsingBase64EncodedKey()

    {

        
$e = new Encrypter(random_bytes(16));

        
$encrypted $e->encrypt('foo');

        
$this->assertNotSame('foo'$encrypted);

        
$this->assertSame('foo'$e->decrypt($encrypted));
        $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));