Commit | Line | Data |
---|---|---|
14d4f286 S |
1 | <?php |
2 | /** | |
3 | * A core wrapper class to provide easy access to all of the cryptographic functions | |
4 | * contained within the library | |
5 | * | |
6 | * PHP version 5.3 | |
7 | * | |
8 | * @category PHPCryptLib | |
9 | * @package Core | |
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@@ | |
14 | */ | |
15 | ||
16 | namespace CryptLib; | |
17 | ||
18 | /** | |
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 | |
21 | * the libraries. | |
22 | */ | |
23 | if (!class_exists('\\CryptLib\Core\AutoLoader', true)) { | |
24 | require_once 'bootstrap.php'; | |
25 | } | |
26 | ||
27 | use CryptLib\Password\Factory as PasswordFactory; | |
28 | use CryptLib\Random\Factory as RandomFactory; | |
29 | ||
30 | /** | |
31 | * A core wrapper class to provide easy access to some of the cryptographic | |
32 | * functions contained within the library | |
33 | * | |
34 | * @category PHPCryptLib | |
35 | * @package Core | |
36 | * @author Anthony Ferrara <ircmaxell@ircmaxell.com> | |
37 | */ | |
38 | class CryptLib { | |
39 | ||
40 | /** | |
41 | * Create a password hash from the supplied password and generator prefix | |
42 | * | |
43 | * @param string $password The password to hash | |
44 | * @param string $prefix The prefix of the hashing function | |
45 | * | |
46 | * @return string The generated password hash | |
47 | */ | |
48 | public function createPasswordHash($password, $prefix = '$2a$') { | |
49 | $factory = new PasswordFactory(); | |
50 | return $factory->createHash($password, $prefix); | |
51 | } | |
52 | ||
53 | /** | |
54 | * Verify a password against a supplied password hash | |
55 | * | |
56 | * @param string $password The supplied password to attempt to verify | |
57 | * @param string $hash The valid hash to verify against | |
58 | * | |
59 | * @return boolean Is the password valid | |
60 | */ | |
61 | public function verifyPasswordHash($password, $hash) { | |
62 | $factory = new PasswordFactory(); | |
63 | return $factory->verifyHash($password, $hash); | |
64 | } | |
65 | ||
66 | /** | |
67 | * Get a random element from the array | |
68 | * | |
69 | * @param array $sourceArray The source array to fetch from | |
70 | * | |
71 | * @return mixed A random element from the source array | |
72 | */ | |
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]]; | |
80 | } | |
81 | ||
82 | /** | |
83 | * Generate a random full-byte string (characters 0 - 255) | |
84 | * | |
85 | * @param int $size The length of the generated string | |
86 | * | |
87 | * @return string The generated string | |
88 | */ | |
89 | public function getRandomBytes($size) { | |
90 | $factory = new RandomFactory; | |
91 | $generator = $factory->getMediumStrengthGenerator(); | |
92 | return $generator->generate($size); | |
93 | } | |
94 | ||
95 | /** | |
96 | * Get a random number between the supplied boundaries | |
97 | * | |
98 | * @param int $min The smallest bound the generated number can be | |
99 | * @param int $max The upper bound on the generated number | |
100 | * | |
101 | * @return int The generated random number | |
102 | */ | |
103 | public function getRandomNumber($min = 0, $max = PHP_INT_MAX) { | |
104 | $factory = new RandomFactory; | |
105 | $generator = $factory->getMediumStrengthGenerator(); | |
106 | return $generator->generateInt($min, $max); | |
107 | } | |
108 | ||
109 | /** | |
110 | * Generate a random token using base64 characters (a-zA-Z0-9./) | |
111 | * | |
112 | * @param int $size The number of characters in the generated output | |
113 | * | |
114 | * @return string The generated token string | |
115 | */ | |
116 | public function getRandomToken($size) { | |
117 | $factory = new RandomFactory; | |
118 | $generator = $factory->getMediumStrengthGenerator(); | |
119 | return $generator->generateString($size); | |
120 | } | |
121 | ||
122 | /** | |
123 | * Shuffle an array. This will preserve key => value relationships, and return | |
124 | * a new array that has been randomized in order. | |
125 | * | |
126 | * To get keys randomized, simply pass the result through array_values()... | |
127 | * | |
128 | * @param array $array The input array to randomize | |
129 | * | |
130 | * @return array The suffled array | |
131 | */ | |
132 | public function shuffleArray(array $array) { | |
133 | $factory = new RandomFactory; | |
134 | $generator = $factory->getMediumStrengthGenerator(); | |
135 | $result = array(); | |
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); | |
145 | } | |
146 | return $result; | |
147 | } | |
148 | ||
149 | /** | |
150 | * Shuffle a string and return the randomized string | |
151 | * | |
152 | * @param string $string The string to randomize | |
153 | * | |
154 | * @return string The shuffled string | |
155 | */ | |
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); | |
162 | } | |
163 | ||
164 | } |