22
33namespace Tempest \Cryptography \Password ;
44
5+ use Error ;
56use Tempest \Cryptography \Password \Exceptions \HashingFailed ;
6- use Tempest \ Cryptography \ Password \ HashingAlgorithm ;
7+ use ValueError ;
78
89final class GenericPasswordHasher implements PasswordHasher
910{
@@ -17,22 +18,24 @@ public function __construct(
1718
1819 public function hash (#[\SensitiveParameter] string $ password ): string
1920 {
20- $ hash = password_hash ($ password , $ this ->algorithm ->value , $ this ->config ->options );
21-
22- if ($ hash === false ) {
23- throw HashingFailed ::forUnknownReason ();
21+ if ($ password === '' ) {
22+ throw HashingFailed ::forEmptyPassword ();
2423 }
2524
26- if (mb_strlen ($ password ) === 0 ) {
27- throw HashingFailed ::forEmptyPassword ();
25+ try {
26+ $ hash = password_hash ($ password , $ this ->algorithm ->value , $ this ->config ->options );
27+ } catch (ValueError ) {
28+ throw HashingFailed ::forInvalidHashingAlgorithm ();
29+ } catch (Error ) {
30+ throw HashingFailed ::forUnknownReason ();
2831 }
2932
3033 return $ hash ;
3134 }
3235
3336 public function verify (#[\SensitiveParameter] string $ password , #[\SensitiveParameter] string $ hash ): bool
3437 {
35- if (mb_strlen ( $ hash) === 0 ) {
38+ if ($ password === '' || $ hash === '' ) {
3639 return false ;
3740 }
3841
@@ -41,11 +44,19 @@ public function verify(#[\SensitiveParameter] string $password, #[\SensitivePara
4144
4245 public function needsRehash (#[\SensitiveParameter] string $ hash ): bool
4346 {
47+ if ($ hash === '' ) {
48+ return false ;
49+ }
50+
4451 return password_needs_rehash ($ hash , $ this ->algorithm ->value , $ this ->config ->options );
4552 }
4653
4754 public function analyze (#[\SensitiveParameter] string $ hash ): ?Hash
4855 {
56+ if ($ hash === '' ) {
57+ return null ;
58+ }
59+
4960 $ info = password_get_info ($ hash );
5061
5162 if ($ info ['algo ' ] === null ) {
0 commit comments