Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[PasswordHasher] Use bcrypt as default hash algorithm for "native" and "auto" #40176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2021
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
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/PasswordHasher/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
---

* Add the component
* Use `bcrypt` as default algorithm in `NativePasswordHasher`
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class NativePasswordHasher implements PasswordHasherInterface
private $options;

/**
* @param string|null $algo An algorithm supported by password_hash() or null to use the stronger available algorithm
* @param string|null $algo An algorithm supported by password_hash() or null to use the best available algorithm
wouterj marked this conversation as resolved.
Show resolved Hide resolved
*/
public function __construct(int $opsLimit = null, int $memLimit = null, int $cost = null, ?string $algo = null)
{
Expand All @@ -52,11 +52,11 @@ public function __construct(int $opsLimit = null, int $memLimit = null, int $cos
$algos = [1 => \PASSWORD_BCRYPT, '2y' => \PASSWORD_BCRYPT];

if (\defined('PASSWORD_ARGON2I')) {
$this->algo = $algos[2] = $algos['argon2i'] = (string) \PASSWORD_ARGON2I;
$algos[2] = $algos['argon2i'] = (string) \PASSWORD_ARGON2I;
}

if (\defined('PASSWORD_ARGON2ID')) {
$this->algo = $algos[3] = $algos['argon2id'] = (string) \PASSWORD_ARGON2ID;
$algos[3] = $algos['argon2id'] = (string) \PASSWORD_ARGON2ID;
}

if (null !== $algo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

namespace Symfony\Component\PasswordHasher\Hasher;

use Symfony\Component\Security\Core\Encoder\EncoderAwareInterface;
use Symfony\Component\PasswordHasher\Exception\LogicException;
use Symfony\Component\PasswordHasher\PasswordHasherInterface;
use Symfony\Component\Security\Core\Encoder\EncoderAwareInterface;

/**
* A generic hasher factory implementation.
Expand Down Expand Up @@ -103,9 +103,15 @@ private function createHasher(array $config, bool $isExtra = false): PasswordHas
private function getHasherConfigFromAlgorithm(array $config): array
{
if ('auto' === $config['algorithm']) {
$hasherChain = [];
// "plaintext" is not listed as any leaked hashes could then be used to authenticate directly
foreach ([SodiumPasswordHasher::isSupported() ? 'sodium' : 'native', 'pbkdf2', $config['hash_algorithm']] as $algo) {
if (SodiumPasswordHasher::isSupported()) {
$algos = ['native', 'sodium', 'pbkdf2', $config['hash_algorithm']];
} else {
$algos = ['native', 'pbkdf2', $config['hash_algorithm']];
}

$hasherChain = [];
foreach ($algos as $algo) {
$config['algorithm'] = $algo;
$hasherChain[] = $this->createHasher($config, true);
}
Expand Down Expand Up @@ -186,7 +192,7 @@ private function getHasherConfigFromAlgorithm(array $config): array
$config['algorithm'] = 'native';
$config['native_algorithm'] = \PASSWORD_ARGON2I;
} else {
throw new LogicException(sprintf('Algorithm "argon2i" is not available. Either use %s"auto" or upgrade to PHP 7.2+ instead.', \defined('SODIUM_CRYPTO_PWHASH_ALG_ARGON2ID13') ? '"argon2id", ' : ''));
throw new LogicException(sprintf('Algorithm "argon2i" is not available. Use "%s" instead.', \defined('SODIUM_CRYPTO_PWHASH_ALG_ARGON2ID13') ? 'argon2id" or "auto' : 'auto'));
}

return $this->getHasherConfigFromAlgorithm($config);
Expand All @@ -198,7 +204,7 @@ private function getHasherConfigFromAlgorithm(array $config): array
$config['algorithm'] = 'native';
$config['native_algorithm'] = \PASSWORD_ARGON2ID;
} else {
throw new LogicException(sprintf('Algorithm "argon2id" is not available. Either use %s"auto", upgrade to PHP 7.3+ or use libsodium 1.0.15+ instead.', \defined('PASSWORD_ARGON2I') || $hasSodium ? '"argon2i", ' : ''));
throw new LogicException(sprintf('Algorithm "argon2id" is not available. Either use "%s", upgrade to PHP 7.3+ or use libsodium 1.0.15+ instead.', \defined('PASSWORD_ARGON2I') || $hasSodium ? 'argon2i", "auto' : 'auto'));
}

return $this->getHasherConfigFromAlgorithm($config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ class NativePasswordHasherTest extends TestCase
{
public function testCostBelowRange()
{
$this->expectException('InvalidArgumentException');
$this->expectException(\InvalidArgumentException::class);
new NativePasswordHasher(null, null, 3);
}

public function testCostAboveRange()
{
$this->expectException('InvalidArgumentException');
$this->expectException(\InvalidArgumentException::class);
new NativePasswordHasher(null, null, 32);
}

Expand Down Expand Up @@ -73,6 +73,14 @@ public function testConfiguredAlgorithm()
$this->assertStringStartsWith('$2', $result);
}

public function testDefaultAlgorithm()
{
$hasher = new NativePasswordHasher();
$result = $hasher->hash('password');
$this->assertTrue($hasher->verify($result, 'password'));
$this->assertStringStartsWith('$2', $result);
}

public function testConfiguredAlgorithmWithLegacyConstValue()
{
$hasher = new NativePasswordHasher(null, null, null, '1');
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.