Skip to content
Draft
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
9 changes: 7 additions & 2 deletions packages/cryptography/src/Password/HashingAlgorithm.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@

enum HashingAlgorithm: string
{
// The values are the literal strings behind PASSWORD_ARGON2ID and PASSWORD_BCRYPT.
// PASSWORD_ARGON2ID is only defined on PHP builds compiled with Argon2, so using the
// constant here would make the whole enum unloadable on builds without it. The literals
// match what password_hash() expects and what password_get_info() reports.

/**
* @see https://en.wikipedia.org/wiki/Argon2
*/
case ARGON2ID = PASSWORD_ARGON2ID;
case ARGON2ID = 'argon2id';

/**
* @see https://en.wikipedia.org/wiki/bcrypt
*/
case BCRYPT = PASSWORD_BCRYPT;
case BCRYPT = '2y';
}
6 changes: 5 additions & 1 deletion packages/cryptography/src/Password/hashing.config.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

use Tempest\Cryptography\Password\ArgonConfig;
use Tempest\Cryptography\Password\BcryptConfig;
use Tempest\Cryptography\Password\HashingAlgorithm;

return new ArgonConfig();
return in_array(HashingAlgorithm::ARGON2ID->value, password_algos(), true)
? new ArgonConfig()
: new BcryptConfig();
9 changes: 9 additions & 0 deletions packages/cryptography/tests/Password/PasswordHasherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ public function test_algorithm(): void
$this->assertSame(HashingAlgorithm::BCRYPT, $hasher->algorithm);
}

public function test_algorithm_values_match_password_constants(): void
{
$this->assertSame(PASSWORD_BCRYPT, HashingAlgorithm::BCRYPT->value);

if (defined('PASSWORD_ARGON2ID')) {
$this->assertSame(PASSWORD_ARGON2ID, HashingAlgorithm::ARGON2ID->value);
}
}

public function test_config_options(): void
{
$this->assertSame(
Expand Down
5 changes: 4 additions & 1 deletion tests/Integration/Cryptography/PasswordHasherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ final class PasswordHasherTest extends FrameworkIntegrationTestCase
public function test_default_algorithm(): void
{
$hasher = $this->container->get(PasswordHasher::class);
$this->assertSame(HashingAlgorithm::ARGON2ID, $hasher->algorithm);
$expected = in_array(HashingAlgorithm::ARGON2ID->value, password_algos(), true)
? HashingAlgorithm::ARGON2ID
: HashingAlgorithm::BCRYPT;
$this->assertSame($expected, $hasher->algorithm);
}

public function test_hash_verify(): void
Expand Down