forked from ircmaxell/PHP-CryptLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCryptLib.php
164 lines (151 loc) · 5.15 KB
/
CryptLib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/**
* A core wrapper class to provide easy access to all of the cryptographic functions
* contained within the library
*
* PHP version 5.3
*
* @category PHPCryptLib
* @package Core
* @author Anthony Ferrara <ircmaxell@ircmaxell.com>
* @copyright 2011 The Authors
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version Build @@version@@
*/
namespace CryptLib;
/**
* The autoloader class will be autoloaded at this point even if another autoloader
* is in use. So if it does not exist at this point, we know we must bootstrap
* the libraries.
*/
if (!class_exists('\\CryptLib\Core\AutoLoader', true)) {
require_once 'bootstrap.php';
}
use CryptLib\Password\Factory as PasswordFactory;
use CryptLib\Random\Factory as RandomFactory;
/**
* A core wrapper class to provide easy access to some of the cryptographic
* functions contained within the library
*
* @category PHPCryptLib
* @package Core
* @author Anthony Ferrara <ircmaxell@ircmaxell.com>
*/
class CryptLib {
/**
* Create a password hash from the supplied password and generator prefix
*
* @param string $password The password to hash
* @param string $prefix The prefix of the hashing function
*
* @return string The generated password hash
*/
public function createPasswordHash($password, $prefix = '$2a$') {
$factory = new PasswordFactory();
return $factory->createHash($password, $prefix);
}
/**
* Verify a password against a supplied password hash
*
* @param string $password The supplied password to attempt to verify
* @param string $hash The valid hash to verify against
*
* @return boolean Is the password valid
*/
public function verifyPasswordHash($password, $hash) {
$factory = new PasswordFactory();
return $factory->verifyHash($password, $hash);
}
/**
* Get a random element from the array
*
* @param array $sourceArray The source array to fetch from
*
* @return mixed A random element from the source array
*/
public function getRandomArrayElement(array $sourceArray) {
$keys = array_keys($sourceArray);
$upperBound = count($keys);
$factory = new RandomFactory;
$generator = $factory->getMediumStrengthGenerator();
$key = $generator->generateInt(0, $upperBound - 1);
return $sourceArray[$keys[$key]];
}
/**
* Generate a random full-byte string (characters 0 - 255)
*
* @param int $size The length of the generated string
*
* @return string The generated string
*/
public function getRandomBytes($size) {
$factory = new RandomFactory;
$generator = $factory->getMediumStrengthGenerator();
return $generator->generate($size);
}
/**
* Get a random number between the supplied boundaries
*
* @param int $min The smallest bound the generated number can be
* @param int $max The upper bound on the generated number
*
* @return int The generated random number
*/
public function getRandomNumber($min = 0, $max = PHP_INT_MAX) {
$factory = new RandomFactory;
$generator = $factory->getMediumStrengthGenerator();
return $generator->generateInt($min, $max);
}
/**
* Generate a random token using base64 characters (a-zA-Z0-9./)
*
* @param int $size The number of characters in the generated output
*
* @return string The generated token string
*/
public function getRandomToken($size) {
$factory = new RandomFactory;
$generator = $factory->getMediumStrengthGenerator();
return $generator->generateString($size);
}
/**
* Shuffle an array. This will preserve key => value relationships, and return
* a new array that has been randomized in order.
*
* To get keys randomized, simply pass the result through array_values()...
*
* @param array $array The input array to randomize
*
* @return array The suffled array
*/
public function shuffleArray(array $array) {
$factory = new RandomFactory;
$generator = $factory->getMediumStrengthGenerator();
$result = array();
$values = array_values($array);
$keys = array_keys($array);
$max = count($array);
for ($i = $max - 1; $i >= 0; $i--) {
$int = $generator->generateInt(0, $i);
$result[$keys[$int]] = $values[$int];
unset($keys[$int], $values[$int]);
$keys = array_values($keys);
$values = array_values($values);
}
return $result;
}
/**
* Shuffle a string and return the randomized string
*
* @param string $string The string to randomize
*
* @return string The shuffled string
*/
public function shuffleString($string) {
$factory = new RandomFactory;
$generator = $factory->getMediumStrengthGenerator();
$array = str_split($string);
$result = $this->shuffleArray($array);
return implode('', $result);
}
}