KennisbankExamencentrum
NL · NLNederlands (Nederland)
Taal kiezen13

CKTIS / PHP

random_bytes()

Genereert het gevraagde aantal cryptografisch veilige willekeurige bytes.

Versie: PHP 8.4; repository runtime, production runtime not attested

Syntax

random_bytes(int $length): string

Genereert het gevraagde aantal cryptografisch veilige willekeurige bytes. Nulbytes zijn mogelijk en hexadecimale codering verdubbelt de lengte. Een lengte onder één geeft ValueError. Een defecte toevalsbron geeft in PHP 8.4 Random\RandomException.

Signatuur

random_bytes(int $length): string

Uitvoer van het voorbeeld

16 32
ValueError

Voorbeelden

<?php
$bytes=random_bytes(16);
echo strlen($bytes), " ", strlen(bin2hex($bytes)), "\n";
try { random_bytes(0); } catch (ValueError $e) { echo get_class($e); }

Resultaat

16 32
ValueError

Bronnen