feat(cloud): argon2id password + CSRF token helpers

This commit is contained in:
유창욱 2026-07-12 00:11:20 +09:00
parent 185629a4c4
commit 76ea1b4ecf
3 changed files with 45 additions and 0 deletions

View file

@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace App\Security;
final class Csrf
{
public static function issue(callable $randHex): string { return (string)$randHex(); }
public static function check(string $expected, string $given): bool {
return $expected !== '' && hash_equals($expected, $given);
}
}

View file

@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace App\Security;
final class Password
{
public static function hash(string $plain): string {
return password_hash($plain, PASSWORD_ARGON2ID);
}
public static function verify(string $plain, string $hash): bool {
return password_verify($plain, $hash);
}
}

View file

@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace App\Tests;
use App\Security\{Password, Csrf};
use PHPUnit\Framework\TestCase;
final class PasswordCsrfTest extends TestCase
{
public function testHashVerify(): void {
$h = Password::hash('s3cret!');
$this->assertNotSame('s3cret!', $h);
$this->assertTrue(Password::verify('s3cret!', $h));
$this->assertFalse(Password::verify('wrong', $h));
}
public function testCsrf(): void {
$t = Csrf::issue(fn() => 'abc123');
$this->assertSame('abc123', $t);
$this->assertTrue(Csrf::check($t, 'abc123'));
$this->assertFalse(Csrf::check($t, 'nope'));
}
}