3 * A core wrapper class to provide easy access to all of the cryptographic functions
4 * contained within the library
8 * @category PHPCryptLib
10 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
11 * @copyright 2011 The Authors
12 * @license http://www.opensource.org/licenses/mit-license.html MIT License
13 * @version Build @@version@@
19 * The autoloader class will be autoloaded at this point even if another autoloader
20 * is in use. So if it does not exist at this point, we know we must bootstrap
23 if (!class_exists('\\CryptLib\Core\AutoLoader', true)) {
24 require_once 'bootstrap.php';
27 use CryptLib\Password\Factory
as PasswordFactory
;
28 use CryptLib\Random\Factory
as RandomFactory
;
31 * A core wrapper class to provide easy access to some of the cryptographic
32 * functions contained within the library
34 * @category PHPCryptLib
36 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
41 * Create a password hash from the supplied password and generator prefix
43 * @param string $password The password to hash
44 * @param string $prefix The prefix of the hashing function
46 * @return string The generated password hash
48 public function createPasswordHash($password, $prefix = '$2a$') {
49 $factory = new PasswordFactory();
50 return $factory->createHash($password, $prefix);
54 * Verify a password against a supplied password hash
56 * @param string $password The supplied password to attempt to verify
57 * @param string $hash The valid hash to verify against
59 * @return boolean Is the password valid
61 public function verifyPasswordHash($password, $hash) {
62 $factory = new PasswordFactory();
63 return $factory->verifyHash($password, $hash);
67 * Get a random element from the array
69 * @param array $sourceArray The source array to fetch from
71 * @return mixed A random element from the source array
73 public function getRandomArrayElement(array $sourceArray) {
74 $keys = array_keys($sourceArray);
75 $upperBound = count($keys);
76 $factory = new RandomFactory
;
77 $generator = $factory->getMediumStrengthGenerator();
78 $key = $generator->generateInt(0, $upperBound - 1);
79 return $sourceArray[$keys[$key]];
83 * Generate a random full-byte string (characters 0 - 255)
85 * @param int $size The length of the generated string
87 * @return string The generated string
89 public function getRandomBytes($size) {
90 $factory = new RandomFactory
;
91 $generator = $factory->getMediumStrengthGenerator();
92 return $generator->generate($size);
96 * Get a random number between the supplied boundaries
98 * @param int $min The smallest bound the generated number can be
99 * @param int $max The upper bound on the generated number
101 * @return int The generated random number
103 public function getRandomNumber($min = 0, $max = PHP_INT_MAX
) {
104 $factory = new RandomFactory
;
105 $generator = $factory->getMediumStrengthGenerator();
106 return $generator->generateInt($min, $max);
110 * Generate a random token using base64 characters (a-zA-Z0-9./)
112 * @param int $size The number of characters in the generated output
114 * @return string The generated token string
116 public function getRandomToken($size) {
117 $factory = new RandomFactory
;
118 $generator = $factory->getMediumStrengthGenerator();
119 return $generator->generateString($size);
123 * Shuffle an array. This will preserve key => value relationships, and return
124 * a new array that has been randomized in order.
126 * To get keys randomized, simply pass the result through array_values()...
128 * @param array $array The input array to randomize
130 * @return array The suffled array
132 public function shuffleArray(array $array) {
133 $factory = new RandomFactory
;
134 $generator = $factory->getMediumStrengthGenerator();
136 $values = array_values($array);
137 $keys = array_keys($array);
138 $max = count($array);
139 for ($i = $max - 1; $i >= 0; $i--) {
140 $int = $generator->generateInt(0, $i);
141 $result[$keys[$int]] = $values[$int];
142 unset($keys[$int], $values[$int]);
143 $keys = array_values($keys);
144 $values = array_values($values);
150 * Shuffle a string and return the randomized string
152 * @param string $string The string to randomize
154 * @return string The shuffled string
156 public function shuffleString($string) {
157 $factory = new RandomFactory
;
158 $generator = $factory->getMediumStrengthGenerator();
159 $array = str_split($string);
160 $result = $this->shuffleArray($array);
161 return implode('', $result);