/** * Generate a random sequence of bytes. * @param int $n Number of bytes. * @param string $method Output parameter for the method used to generate * bytes: 'php7', 'mcrypt', 'openssl', 'urandom', or 'mt_rand'. * @return string String of bytes. */ public static function bytes($n, &$method = null) { $bytes = self::php7Bytes($n); $method = 'php7'; if (!isset($bytes)) { $bytes = self::mcryptBytes($n); $method = 'mcrypt'; } if (!isset($bytes)) { $bytes = self::opensslBytes($n); $method = 'openssl'; } if (!isset($bytes)) { $bytes = self::urandomBytes($n); $method = 'urandom'; } if (!isset($bytes)) { $bytes = self::mtRandBytes($n); $method = 'mt_rand'; } $l = Binary::length($bytes); if ($l < $n) { $bytes .= self::mtRandBytes($n - $l); } return $bytes; }