Supported Versions: PHP 7, PHP 8
Get cryptographically secure random bytes
<?php random_bytes(int $length): string
<?php     public static function generateKey($cipher)

    {

        return random_bytes(self::$supportedCiphers[strtolower($cipher)]['size'] ?? 32);

    }
<?php     public function encrypt($value, $serialize = true)

    {

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



        $value = \openssl_encrypt(

            $serialize ? serialize($value) : $value,
<?php                 $bytesSize = (int) ceil($size / 3) * 3;



                $bytes = random_bytes($bytesSize);



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

            }
<?php         $mock = m::mock(Model::class, [

            'getKey' => random_bytes(10),

            'getQueueableId' => 'mocked',

        ]);
<?php     public function testEncryptionUsingBase64EncodedKey()

    {

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

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

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

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