Something went wrong. Try again.
Website template for structured, accessible static websites, built with HTML, CSS, TypeScript and Vite. spa-template.liberacode.dev
Something went wrong. Try again.
spa-template altcha-challenge.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778<?phpdeclare(strict_types=1);
ini_set("display_errors", "0");
header("Content-Type: application/json; charset=utf-8");header("X-Content-Type-Options: nosniff");header("X-Frame-Options: SAMEORIGIN");header("Referrer-Policy: strict-origin-when-cross-origin");header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
require_once __DIR__ . "/includes/utils.php";
$config = load_contact_config(__DIR__ . "/contact-config.php");
if (!file_exists(__DIR__ . "/vendor/autoload.php")) { server_error($config, "Missing vendor/autoload.php");}
require_once __DIR__ . "/vendor/autoload.php";
use AltchaOrg\Altcha\Algorithm\Pbkdf2;use AltchaOrg\Altcha\Altcha;use AltchaOrg\Altcha\CreateChallengeOptions;
if ($_SERVER["REQUEST_METHOD"] !== "GET") { json_error(405, [ "success" => false, "error" => "This request method is not allowed.", ]);}
rate_limit_or_fail($config, get_client_ip(), 30, 900, "altcha-rate-limit");
$hmacSecret = trim((string) ($config["altcha_hmac_secret"] ?? ""));$hmacKeySecret = trim((string) ($config["altcha_hmac_key_secret"] ?? ""));
if ($hmacSecret === "") { server_error($config, "Missing ALTCHA secret");}
$cost = (int) ($config["altcha_challenge_cost"] ?? 10000);$expiresIn = (int) ($config["altcha_challenge_expires"] ?? 600);
if ($cost < 1000) { $cost = 1000;}
if ($expiresIn < 60) { $expiresIn = 60;}
try { $pbkdf2 = new Pbkdf2();
$altcha = new Altcha( hmacSignatureSecret: $hmacSecret, hmacKeySignatureSecret: $hmacKeySecret !== "" ? $hmacKeySecret : null, );
$challenge = $altcha->createChallenge( new CreateChallengeOptions( algorithm: $pbkdf2, cost: $cost, counter: random_int(5000, 10000), expiresAt: time() + $expiresIn, ), );
echo json_encode( $challenge->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES, ); exit();} catch (Throwable $e) { server_error($config, "ALTCHA challenge creation failed", ["error" => $e->getMessage()]);}