Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Security/Passwords.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public function __construct($algo = PASSWORD_DEFAULT, array $options = [])
*/
public function hash(string $password): string
{
if ($password === '') {
throw new Nette\InvalidArgumentException('Password can not be empty.');
}

$hash = isset($this)
? @password_hash($password, $this->algo, $this->options) // @ is escalated to exception
: @password_hash($password, PASSWORD_BCRYPT, func_get_args()[1] ?? []); // back compatibility with v2.x
Expand Down
6 changes: 5 additions & 1 deletion tests/Security/Passwords.hash().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require __DIR__ . '/../bootstrap.php';


Assert::truthy(
preg_match('#^\$.{50,}\z#', (new Passwords)->hash(''))
preg_match('#^\$.{50,}\z#', (new Passwords)->hash('my-password'))
);

Assert::truthy(
Expand All @@ -27,3 +27,7 @@ Assert::same($hash, crypt('dg', $hash));
Assert::exception(function () {
(new Passwords(PASSWORD_BCRYPT, ['cost' => 3]))->hash('dg');
}, Nette\InvalidStateException::class, 'Computed hash is invalid. password_hash(): Invalid bcrypt cost parameter specified: 3');

Assert::exception(function () {
(new Passwords)->hash('');
}, Nette\InvalidArgumentException::class, 'Password can not be empty.');
2 changes: 1 addition & 1 deletion tests/Security/Passwords.static.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require __DIR__ . '/../bootstrap.php';

// deprecated static usage
Assert::error(function () {
Passwords::hash('');
Passwords::hash('my-password');
}, E_DEPRECATED, 'Non-static method Nette\Security\Passwords::hash() should not be called statically');

Assert::truthy(
Expand Down