public function testLength() { $this->assertEquals(0, Binary::length('')); $this->assertEquals(1, Binary::length('a')); $this->assertEquals(2, Binary::length('å')); $this->assertEquals(3, Binary::length('♥')); $this->assertEquals(6, Binary::length('♥♥')); $this->assertEquals(7, Binary::length('♥a♥')); }
/** * 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; }