@@ -21,6 +21,11 @@ |
||
21 | 21 | return crypt($plaintext, $ciphertext) == $ciphertext; |
22 | 22 | } |
23 | 23 | |
24 | + /** |
|
25 | + * @param integer $cost |
|
26 | + * @param integer $length |
|
27 | + * @param string $bcrypt_version |
|
28 | + */ |
|
24 | 29 | public static function generate_salt($cost, $length, $bcrypt_version){ |
25 | 30 | //make sure adding the cost in two digits |
26 | 31 | $cost = sprintf('%02d', $cost); |
@@ -10,7 +10,7 @@ discard block |
||
10 | 10 | $salt = self::generate_salt($cost, 17, $bcrypt_version); |
11 | 11 | $ciphertext = crypt($plaintext, $salt); |
12 | 12 | |
13 | - return $ciphertext; |
|
13 | + return $ciphertext; |
|
14 | 14 | } |
15 | 15 | |
16 | 16 | public static function verify($plaintext, $ciphertext) |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | */ |
33 | 33 | $bytes = openssl_random_pseudo_bytes($length); |
34 | 34 | if ($bytes === false){ |
35 | - throw new RuntimeException('Unable to generate a random string'); |
|
35 | + throw new RuntimeException('Unable to generate a random string'); |
|
36 | 36 | } |
37 | 37 | $salt=substr(base64_encode($bytes),0,22); |
38 | 38 | |
@@ -41,12 +41,12 @@ discard block |
||
41 | 41 | * anything about the '=', as this only occurs when the b64 string is |
42 | 42 | * padded, which is always after the first 22 characters. |
43 | 43 | */ |
44 | - $salt=str_replace("+",".",$salt); |
|
44 | + $salt=str_replace("+",".",$salt); |
|
45 | 45 | |
46 | - /* Create a string that will be passed to crypt, containing all |
|
46 | + /* Create a string that will be passed to crypt, containing all |
|
47 | 47 | * of the settings, separated by dollar signs |
48 | 48 | */ |
49 | - $salt='$'.implode('$',[$bcrypt_version, $cost, $salt]); |
|
49 | + $salt='$'.implode('$',[$bcrypt_version, $cost, $salt]); |
|
50 | 50 | return $salt; |
51 | 51 | } |
52 | 52 | } |
@@ -5,7 +5,7 @@ discard block |
||
5 | 5 | { |
6 | 6 | const VERSION = '1.0.0'; |
7 | 7 | |
8 | - public static function encrypt($plaintext, $bcrypt_version="2y", $cost=10) |
|
8 | + public static function encrypt($plaintext, $bcrypt_version = "2y", $cost = 10) |
|
9 | 9 | { |
10 | 10 | $salt = self::generate_salt($cost, 17, $bcrypt_version); |
11 | 11 | $ciphertext = crypt($plaintext, $salt); |
@@ -15,13 +15,13 @@ discard block |
||
15 | 15 | |
16 | 16 | public static function verify($plaintext, $ciphertext) |
17 | 17 | { |
18 | - if(version_compare(PHP_VERSION, '5.6.0', '>=')){ |
|
18 | + if (version_compare(PHP_VERSION, '5.6.0', '>=')) { |
|
19 | 19 | return hash_equals($ciphertext, crypt($plaintext, $ciphertext)); |
20 | 20 | } |
21 | 21 | return crypt($plaintext, $ciphertext) == $ciphertext; |
22 | 22 | } |
23 | 23 | |
24 | - public static function generate_salt($cost, $length, $bcrypt_version){ |
|
24 | + public static function generate_salt($cost, $length, $bcrypt_version) { |
|
25 | 25 | //make sure adding the cost in two digits |
26 | 26 | $cost = sprintf('%02d', $cost); |
27 | 27 | |
@@ -31,22 +31,22 @@ discard block |
||
31 | 31 | * 22 base64 characters |
32 | 32 | */ |
33 | 33 | $bytes = openssl_random_pseudo_bytes($length); |
34 | - if ($bytes === false){ |
|
34 | + if ($bytes === false) { |
|
35 | 35 | throw new RuntimeException('Unable to generate a random string'); |
36 | 36 | } |
37 | - $salt=substr(base64_encode($bytes),0,22); |
|
37 | + $salt = substr(base64_encode($bytes), 0, 22); |
|
38 | 38 | |
39 | 39 | /* As blowfish takes a salt with the alphabet ./A-Za-z0-9 we have to |
40 | 40 | * replace any '+' in the base64 string with '.'. We don't have to do |
41 | 41 | * anything about the '=', as this only occurs when the b64 string is |
42 | 42 | * padded, which is always after the first 22 characters. |
43 | 43 | */ |
44 | - $salt=str_replace("+",".",$salt); |
|
44 | + $salt = str_replace("+", ".", $salt); |
|
45 | 45 | |
46 | 46 | /* Create a string that will be passed to crypt, containing all |
47 | 47 | * of the settings, separated by dollar signs |
48 | 48 | */ |
49 | - $salt='$'.implode('$',[$bcrypt_version, $cost, $salt]); |
|
49 | + $salt = '$'.implode('$', [$bcrypt_version, $cost, $salt]); |
|
50 | 50 | return $salt; |
51 | 51 | } |
52 | 52 | } |
@@ -15,13 +15,14 @@ discard block |
||
15 | 15 | |
16 | 16 | public static function verify($plaintext, $ciphertext) |
17 | 17 | { |
18 | - if(version_compare(PHP_VERSION, '5.6.0', '>=')){ |
|
18 | + if(version_compare(PHP_VERSION, '5.6.0', '>=')) { |
|
19 | 19 | return hash_equals($ciphertext, crypt($plaintext, $ciphertext)); |
20 | 20 | } |
21 | 21 | return crypt($plaintext, $ciphertext) == $ciphertext; |
22 | 22 | } |
23 | 23 | |
24 | - public static function generate_salt($cost, $length, $bcrypt_version){ |
|
24 | + public static function generate_salt($cost, $length, $bcrypt_version) |
|
25 | + { |
|
25 | 26 | //make sure adding the cost in two digits |
26 | 27 | $cost = sprintf('%02d', $cost); |
27 | 28 | |
@@ -31,7 +32,7 @@ discard block |
||
31 | 32 | * 22 base64 characters |
32 | 33 | */ |
33 | 34 | $bytes = openssl_random_pseudo_bytes($length); |
34 | - if ($bytes === false){ |
|
35 | + if ($bytes === false) { |
|
35 | 36 | throw new RuntimeException('Unable to generate a random string'); |
36 | 37 | } |
37 | 38 | $salt=substr(base64_encode($bytes),0,22); |