/** * Test... * * @covers Joomla\Crypt\Crypt::genRandomBytes * * @return void */ public function testGenRandomBytes() { // We're just testing wether the value has the expected length. // We obviously can't test the result since it's random. $randomBytes16 = Crypt::genRandomBytes(); $this->assertEquals(strlen($randomBytes16), 16); $randomBytes8 = Crypt::genRandomBytes(8); $this->assertEquals(strlen($randomBytes8), 8); $randomBytes17 = Crypt::genRandomBytes(17); $this->assertEquals(strlen($randomBytes17), 17); }
/** * Generate a random password * * @param integer $length Length of the password to generate * * @return string Random Password * * @since 11.1 */ public static function genRandomPassword($length = 8) { $salt = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $base = strlen($salt); $makepass = ''; /* * Start with a cryptographic strength random string, then convert it to * a string with the numeric base of the salt. * Shift the base conversion on each character so the character * distribution is even, and randomize the start shift so it's not * predictable. */ $random = Crypt::genRandomBytes($length + 1); $shift = ord($random[0]); for ($i = 1; $i <= $length; ++$i) { $makepass .= $salt[($shift + ord($random[$i])) % $base]; $shift += ord($random[$i]); } return $makepass; }
/** * Generates a salt of specified length. The salt consists of characters in the set [./0-9A-Za-z]. * * @param integer $length The number of characters to return. * * @return string The string of random characters. * * @since 1.0 */ protected function getSalt($length) { $bytes = ceil($length * 6 / 8); $randomData = str_replace('+', '.', base64_encode(Crypt::genRandomBytes($bytes))); return substr($randomData, 0, $length); }