certimine/src/Security.php
remaster a72656f9d4 CertiMine: aplicación PHP, API DC3 Peñasquito y documentación
- Portal bilingüe (Bootstrap), biblioteca FRM, consulta y perfiles con QR.
- Integración REST penasquito.wotbi.com (Token, búsqueda por passport y polling).
- Webhook de sincronización, esquema MariaDB y semillas FRM/NOM.
- Documentación en docs/ y extracto DatosPdf.txt; ejemplo de config sin secretos locales.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-13 17:56:36 -06:00

45 lines
1.2 KiB
PHP

<?php
declare(strict_types=1);
namespace CertiMine;
final class Security
{
public static function csrfToken(): string
{
if (empty($_SESSION['_csrf'])) {
$_SESSION['_csrf'] = bin2hex(random_bytes(32));
}
return $_SESSION['_csrf'];
}
public static function verifyCsrf(?string $token): bool
{
return is_string($token)
&& isset($_SESSION['_csrf'])
&& hash_equals($_SESSION['_csrf'], $token);
}
public static function qrSignature(string $type, string $code): string
{
$app = Config::get('app');
$secret = is_array($app) ? (string) ($app['qr_secret'] ?? '') : '';
$payload = $type . '|' . $code;
return hash_hmac('sha256', $payload, $secret);
}
public static function verifyQrSignature(string $type, string $code, ?string $sig): bool
{
$app = Config::get('app');
$enforce = is_array($app) && !empty($app['qr_enforce']);
if (!$enforce) {
return true;
}
if ($sig === null || $sig === '') {
return false;
}
$expected = self::qrSignature($type, $code);
return hash_equals($expected, $sig);
}
}