random_bytes

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));
<?php     public function testInsertAndSelect(): void

    {

        $id1 = random_bytes(16);

        $id2 = random_bytes(16);



        $value1 = random_bytes(64);
<?php     public function testInsertAndSelect(): void

    {

        $id1 = random_bytes(16);

        $id2 = random_bytes(16);



        $value1 = random_bytes(64);

        $value2 = random_bytes(64);
<?php         $id1 = random_bytes(16);

        $id2 = random_bytes(16);



        $value1 = random_bytes(64);

        $value2 = random_bytes(64);
<?php         $id2 = random_bytes(16);



        $value1 = random_bytes(64);

        $value2 = random_bytes(64);




        if ($this->connection->getDriver() instanceof Driver) {
<?php     public static function setUpBeforeClass(): void

    {

        static::$adapterPrefix = 'ci/' . bin2hex(random_bytes(10));

    }



    protected function tearDown(): void
<?php     public static function setUpBeforeClass(): void

    {

        static::$adapterPrefix = getenv('FLYSYSTEM_AWS_S3_PREFIX') ?: 'ci/' . bin2hex(random_bytes(10));

    }



    protected function tearDown(): void
<?php     public static function setUpBeforeClass(): void

    {

        static::$adapterPrefix = 'ci/' . bin2hex(random_bytes(10));

    }



    protected static function createFilesystemAdapter(): FilesystemAdapter
<?php         if (!$gid = self::$gid) {

            $gid = self::$gid = md5(random_bytes(6)); // Unique string used to detect the special $GLOBALS variable

        }

        $arrayStub = new Stub();

        $arrayStub->type = Stub::TYPE_ARRAY;