'ABCDEFGHIJKLMNOPQRSTUVWXYZ', self::CHAR_LOWER => 'abcdefghijklmnopqrstuvwxyz', self::CHAR_DIGITS => '0123456789', self::CHAR_UPPER_HEX => 'ABCDEF', self::CHAR_LOWER_HEX => 'abcdef', self::CHAR_BASE64 => '+/', self::CHAR_SYMBOLS => '!"#$%&\'()* +,-./:;<=>?@[\]^_`{|}~', self::CHAR_BRACKETS => '()[]{}<>', self::CHAR_PUNCT => ',.;:', ); /** * Generate a random string of specified length using supplied character list. * * @param integer $length The length of the generated string. * @param mixed $characters List of characters to use or character flags. * * @return SecurityGeneratorPromise */ public static function generateString($length, $characters) { // Combine character sets. if ( is_int($characters) ) { $characters = self::expandCharacterSets($characters); } return new SecurityGeneratorPromise( SecurityGeneratorPromise::TYPE_STRING, array($length, $characters) ); } /** * Expand a character set bitwise spec into a string character set. * This will also replace EASY_TO_READ characters if the flag is set. * * @param integer $spec The spec to expand (bitwise combination of flags). * * @return string The expanded string */ protected static function expandCharacterSets($spec) { $combined = ''; if ( $spec == self::EASY_TO_READ ) { $spec |= self::CHAR_ALNUM; } foreach ( self::$charArrays as $flag => $chars ) { // Handle this later. if ( $flag == self::EASY_TO_READ ) { continue; } if ( ($spec & $flag) === $flag ) { $combined .= $chars; } } // Remove ambiguous characters. if ( $spec & self::EASY_TO_READ ) { $combined = str_replace(str_split(self::AMBIGUOUS_CHARS), '', $combined); } return count_chars($combined, 3); } /** * Generates a random number. * * @param integer $min Smallest value. * @param integer $max Largest value. * * @return SecurityGeneratorPromise */ public static function generateNumber($min, $max) { return new SecurityGeneratorPromise( SecurityGeneratorPromise::TYPE_NUMBER, array($min, $max) ); } /** * Generates random bytes. * * @param integer $length Raw result length. * @param boolean $raw_output Return raw result. * * @return SecurityGeneratorPromise */ public static function generateBytes($length = 16, $raw_output = false) { return new SecurityGeneratorPromise( SecurityGeneratorPromise::TYPE_BYTES, array($length, $raw_output) ); } }