@@ -45,7 +45,7 @@ public function __construct(int $opsLimit = null, int $memLimit = null, int $cos
45
45
throw new \InvalidArgumentException ('$cost must be in the range of 4-31. ' );
46
46
}
47
47
48
- $ this ->algo = \defined ('PASSWORD_ARGON2I ' ) ? max ( PASSWORD_DEFAULT , \defined ('PASSWORD_ARGON2ID ' ) ? PASSWORD_ARGON2ID : PASSWORD_ARGON2I ) : PASSWORD_DEFAULT ;
48
+ $ this ->algo = \defined ('PASSWORD_ARGON2ID ' ) ? PASSWORD_ARGON2ID : ( \defined ('PASSWORD_ARGON2I ' ) ? PASSWORD_ARGON2I : PASSWORD_BCRYPT ) ;
49
49
$ this ->options = [
50
50
'cost ' => $ cost ,
51
51
'time_cost ' => $ opsLimit ,
@@ -59,32 +59,37 @@ public function __construct(int $opsLimit = null, int $memLimit = null, int $cos
59
59
*/
60
60
public function encodePassword ($ raw , $ salt )
61
61
{
62
- if (\strlen ($ raw ) > self ::MAX_PASSWORD_LENGTH ) {
62
+ if (\strlen ($ raw ) > self ::MAX_PASSWORD_LENGTH || ( PASSWORD_BCRYPT === $ this -> algo && 72 < \strlen ( $ raw )) ) {
63
63
throw new BadCredentialsException ('Invalid password. ' );
64
64
}
65
65
66
66
// Ignore $salt, the auto-generated one is always the best
67
67
68
- $ encoded = password_hash ($ raw , $ this ->algo , $ this ->options );
69
-
70
- if (72 < \strlen ($ raw) && 0 === strpos ($ encoded , '$2 ' )) {
71
- // BCrypt encodes only the first 72 chars
72
- throw new BadCredentialsException ('Invalid password. ' );
73
- }
74
-
75
- return $ encoded ;
68
+ return password_hash ($ raw , $ this ->algo , $ this ->options );
76
69
}
77
70
78
71
/**
79
72
* {@inheritdoc}
80
73
*/
81
74
public function isPasswordValid ($ encoded , $ raw , $ salt )
82
75
{
83
- if (72 < \strlen ($ raw ) && 0 === strpos ($ encoded , '$2 ' )) {
84
- // BCrypt encodes only the first 72 chars
76
+ if (\strlen ($ raw ) > self ::MAX_PASSWORD_LENGTH ) {
85
77
return false ;
86
78
}
87
79
88
- return \strlen ($ raw ) <= self ::MAX_PASSWORD_LENGTH && password_verify ($ raw , $ encoded );
80
+ if (0 === strpos ($ encoded , '$2 ' )) {
81
+ // BCrypt encodes only the first 72 chars
82
+ return 72 >= \strlen ($ raw ) && password_verify ($ raw , $ encoded );
83
+ }
84
+
85
+ if (\extension_loaded ('sodium ' ) && version_compare (\SODIUM_LIBRARY_VERSION , '1.0.14 ' , '>= ' )) {
86
+ return sodium_crypto_pwhash_str_verify ($ encoded , $ raw );
87
+ }
88
+
89
+ if (\extension_loaded ('libsodium ' ) && version_compare (phpversion ('libsodium ' ), '1.0.14 ' , '>= ' )) {
90
+ return \Sodium \crypto_pwhash_str_verify ($ encoded , $ raw );
91
+ }
92
+
93
+ return password_verify ($ raw , $ encoded );
89
94
}
90
95
}
0 commit comments