Skip to content

Commit d4edc5d

Browse files
committed
feat: add password hashing
1 parent 8e8d839 commit d4edc5d

18 files changed

+450
-0
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"tempest/console": "self.version",
7676
"tempest/container": "self.version",
7777
"tempest/core": "self.version",
78+
"tempest/cryptography": "self.version",
7879
"tempest/database": "self.version",
7980
"tempest/datetime": "self.version",
8081
"tempest/debug": "self.version",
@@ -110,6 +111,7 @@
110111
"Tempest\\Console\\": "packages/console/src",
111112
"Tempest\\Container\\": "packages/container/src",
112113
"Tempest\\Core\\": "packages/core/src",
114+
"Tempest\\Cryptography\\": "packages/cryptography/src",
113115
"Tempest\\Database\\": "packages/database/src",
114116
"Tempest\\DateTime\\": "packages/datetime/src",
115117
"Tempest\\Debug\\": "packages/debug/src",
@@ -173,6 +175,7 @@
173175
"Tempest\\Console\\Tests\\": "packages/console/tests",
174176
"Tempest\\Container\\Tests\\": "packages/container/tests",
175177
"Tempest\\Core\\Tests\\": "packages/core/tests",
178+
"Tempest\\Cryptography\\Tests\\": "packages/cryptography/tests",
176179
"Tempest\\Database\\Tests\\": "packages/database/tests",
177180
"Tempest\\DateTime\\Tests\\": "packages/datetime/tests",
178181
"Tempest\\EventBus\\Tests\\": "packages/event-bus/tests",
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Exclude build/test files from the release
2+
.github/ export-ignore
3+
tests/ export-ignore
4+
.gitattributes export-ignore
5+
.gitignore export-ignore
6+
phpunit.xml export-ignore
7+
README.md export-ignore
8+
9+
# Configure diff output
10+
*.view.php diff=html
11+
*.php diff=php
12+
*.css diff=css
13+
*.html diff=html
14+
*.md diff=markdown

packages/cryptography/LICENCE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2024 Brent Roose [email protected]
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "tempest/cryptography",
3+
"description": "A component for working with passwords, hashing, and cryptography.",
4+
"license": "MIT",
5+
"minimum-stability": "dev",
6+
"require": {
7+
"php": "^8.4",
8+
"tempest/container": "dev-main",
9+
"tempest/support": "dev-main"
10+
},
11+
"autoload": {
12+
"psr-4": {
13+
"Tempest\\Cryptography\\": "src"
14+
}
15+
},
16+
"autoload-dev": {
17+
"psr-4": {
18+
"Tempest\\Cryptography\\Tests\\": "tests"
19+
}
20+
}
21+
}

packages/cryptography/phpunit.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.4/phpunit.xsd"
5+
bootstrap="vendor/autoload.php"
6+
executionOrder="depends,defects"
7+
beStrictAboutOutputDuringTests="true"
8+
displayDetailsOnPhpunitDeprecations="true"
9+
failOnPhpunitDeprecation="false"
10+
failOnRisky="true"
11+
failOnWarning="true"
12+
>
13+
<testsuites>
14+
<testsuite name="Tempest cryptography">
15+
<directory>tests</directory>
16+
</testsuite>
17+
</testsuites>
18+
<source restrictNotices="true" restrictWarnings="true" ignoreIndirectDeprecations="true">
19+
<include>
20+
<directory>src</directory>
21+
</include>
22+
</source>
23+
</phpunit>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Tempest\Cryptography\Password;
4+
5+
final class ArgonConfig implements PasswordHashingConfig
6+
{
7+
public HashingAlgorithm $algorithm = HashingAlgorithm::ARGON2ID;
8+
9+
public array $options {
10+
get => [
11+
'memory_cost' => $this->memoryCost,
12+
'time_cost' => $this->timeCost,
13+
'threads' => $this->threads,
14+
];
15+
}
16+
17+
/**
18+
* @param int $memoryCost The amount of memory in bytes that Argon will use while trying to compute a hash. The higher, the more resistant to GPU/ASIC attacks, but also more resource-intensive and slow.
19+
* @param int $timeCost Number of passes Argon will perform over the memory. Increasing this increases the computation time but makes brute-force attacks slower.
20+
* @param int $threads Number of threads used to compute the hash. More threads can improve performance on multi-core CPUs by parallelizing the computation, but may impact other processes.
21+
*/
22+
public function __construct(
23+
public int $memoryCost = PASSWORD_ARGON2_DEFAULT_MEMORY_COST,
24+
public int $timeCost = PASSWORD_ARGON2_DEFAULT_TIME_COST,
25+
public int $threads = PASSWORD_ARGON2_DEFAULT_THREADS,
26+
) {}
27+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Tempest\Cryptography\Password;
4+
5+
use Tempest\Cryptography\Password\HashingAlgorithm;
6+
7+
final class BcryptConfig implements PasswordHashingConfig
8+
{
9+
public HashingAlgorithm $algorithm = HashingAlgorithm::BCRYPT;
10+
11+
public array $options {
12+
get => [
13+
'cost' => $this->cost,
14+
];
15+
}
16+
17+
/**
18+
* @param int $cost Number of iterations bcrypt will perform. Increasing this increases the computation time but makes brute-force attacks slower.
19+
*/
20+
public function __construct(
21+
public int $cost = PASSWORD_BCRYPT_DEFAULT_COST,
22+
) {}
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Tempest\Cryptography\Password\Exceptions;
4+
5+
use Exception;
6+
7+
final class HashingFailed extends Exception implements PasswordHashingException
8+
{
9+
public static function forUnknownReason(): self
10+
{
11+
return new self('Hashing resulted in an error.');
12+
}
13+
14+
public static function forEmptyPassword(): self
15+
{
16+
return new self('Could not hash an empty password.');
17+
}
18+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Tempest\Cryptography\Password\Exceptions;
4+
5+
interface PasswordHashingException
6+
{
7+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace Tempest\Cryptography\Password;
4+
5+
use Tempest\Cryptography\Password\Exceptions\HashingFailed;
6+
use Tempest\Cryptography\Password\HashingAlgorithm;
7+
8+
final class GenericPasswordHasher implements PasswordHasher
9+
{
10+
public HashingAlgorithm $algorithm {
11+
get => $this->config->algorithm;
12+
}
13+
14+
public function __construct(
15+
private readonly PasswordHashingConfig $config,
16+
) {}
17+
18+
public function hash(#[\SensitiveParameter] string $password): string
19+
{
20+
$hash = password_hash($password, $this->algorithm->value, $this->config->options);
21+
22+
if ($hash === false) {
23+
throw HashingFailed::forUnknownReason();
24+
}
25+
26+
if (mb_strlen($password) === 0) {
27+
throw HashingFailed::forEmptyPassword();
28+
}
29+
30+
return $hash;
31+
}
32+
33+
public function verify(#[\SensitiveParameter] string $password, #[\SensitiveParameter] string $hash): bool
34+
{
35+
if (mb_strlen($hash) === 0) {
36+
return false;
37+
}
38+
39+
return password_verify($password, $hash);
40+
}
41+
42+
public function needsRehash(#[\SensitiveParameter] string $hash): bool
43+
{
44+
return password_needs_rehash($hash, $this->algorithm->value, $this->config->options);
45+
}
46+
47+
public function analyze(#[\SensitiveParameter] string $hash): Hash
48+
{
49+
$info = password_get_info($hash);
50+
$algorithm = HashingAlgorithm::from($info['algo']);
51+
52+
return new Hash(
53+
hash: $hash,
54+
algorithm: $algorithm,
55+
config: match ($algorithm) {
56+
HashingAlgorithm::BCRYPT => new BcryptConfig(
57+
cost: $info['options']['cost'] ?? PASSWORD_BCRYPT_DEFAULT_COST,
58+
),
59+
HashingAlgorithm::ARGON2ID => new ArgonConfig(
60+
memoryCost: $info['options']['memory_cost'] ?? PASSWORD_ARGON2_DEFAULT_MEMORY_COST,
61+
timeCost: $info['options']['time_cost'] ?? PASSWORD_ARGON2_DEFAULT_TIME_COST,
62+
threads: $info['options']['threads'] ?? PASSWORD_ARGON2_DEFAULT_THREADS,
63+
),
64+
},
65+
);
66+
}
67+
}

0 commit comments

Comments
 (0)