Website-Stand vom 14.10.2026 veroeffentlichen
This commit is contained in:
commit
4ae8cea7b1
158 files changed
+46887
No files matched your search
+113
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
/**
|
||||
* ALTCHA v1-Protokoll: Challenge-Erstellung und Lösungsprüfung.
|
||||
*
|
||||
* Nachbau des offiziellen altcha-lib-php (MIT, github.com/altcha-org/altcha-lib-php,
|
||||
* src/V1), auf das Nötigste reduziert und ohne Namensräume, damit das
|
||||
* Kontaktformular ohne Composer/Abhängigkeiten auf dem Webspace läuft.
|
||||
*
|
||||
* Protokoll:
|
||||
* - Server erzeugt salt (zufälliges Hex) und number (0..maxnumber) und
|
||||
* daraus challenge = SHA-256(salt . number) sowie
|
||||
* signature = HMAC-SHA256(challenge, hmac_key).
|
||||
* - Der Client sucht per Brute-Force die Zahl n mit SHA-256(salt . n)
|
||||
* == challenge und sendet sie base64-kodiert zurück.
|
||||
* - Der Server rechnet beide Werte nach; ein Bot ohne Lösung der
|
||||
* Proof-of-Work-Aufgabe kommt nicht durch.
|
||||
*/
|
||||
|
||||
function altcha_hash_hex(string $algorithm, string $data): string
|
||||
{
|
||||
return match ($algorithm) {
|
||||
'SHA-1' => sha1($data),
|
||||
'SHA-512' => hash('sha512', $data),
|
||||
default => hash('sha256', $data),
|
||||
};
|
||||
}
|
||||
|
||||
function altcha_hmac_hex(string $algorithm, string $data, string $key): string
|
||||
{
|
||||
return match ($algorithm) {
|
||||
'SHA-1' => hash_hmac('sha1', $data, $key),
|
||||
'SHA-512' => hash_hmac('sha512', $data, $key),
|
||||
default => hash_hmac('sha256', $data, $key),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Erzeugt eine neue Challenge. Rückgabe: Array für die JSON-Antwort
|
||||
* an das ALTCHA-Widget (algorithm, challenge, maxnumber, salt, signature).
|
||||
*
|
||||
* @param string $hmac_key Geheimer HMAC-Schlüssel.
|
||||
* @param int $max_number Obergrenze für die zu suchende Zahl (Schwierigkeit).
|
||||
* @param int|null $expires Unix-Zeitstempel, ab dem die Challenge ungültig wird.
|
||||
*/
|
||||
function altcha_create_challenge(string $hmac_key, int $max_number = 1000000, ?int $expires = null): array
|
||||
{
|
||||
$salt = bin2hex(random_bytes(12));
|
||||
if ($expires !== null) {
|
||||
$salt .= '?expires=' . $expires . '&';
|
||||
} else {
|
||||
// Abschluss-Zeichen gegen Parameter-Splicing, wie in der Referenzimplementierung
|
||||
$salt .= '&';
|
||||
}
|
||||
|
||||
$number = random_int(0, $max_number);
|
||||
$challenge = altcha_hash_hex('SHA-256', $salt . $number);
|
||||
$signature = altcha_hmac_hex('SHA-256', $challenge, $hmac_key);
|
||||
|
||||
return [
|
||||
'algorithm' => 'SHA-256',
|
||||
'challenge' => $challenge,
|
||||
'maxnumber' => $max_number,
|
||||
'salt' => $salt,
|
||||
'signature' => $signature,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Prüft eine vom Widget gesendete Lösung (Base64-JSON mit algorithm,
|
||||
* challenge, number, salt, signature).
|
||||
*/
|
||||
function altcha_verify_solution(string $hmac_key, string $payload_base64): bool
|
||||
{
|
||||
$decoded = base64_decode($payload_base64, true);
|
||||
if ($decoded === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$data = json_decode($decoded, true);
|
||||
if (!is_array($data)) {
|
||||
return false;
|
||||
}
|
||||
if (
|
||||
!isset($data['algorithm'], $data['challenge'], $data['number'], $data['salt'], $data['signature'])
|
||||
|| !is_string($data['algorithm'])
|
||||
|| !is_string($data['challenge'])
|
||||
|| !is_int($data['number'])
|
||||
|| !is_string($data['salt'])
|
||||
|| !is_string($data['signature'])
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Ablauf der Challenge prüfen (im salt als Query-Parameter hinterlegt)
|
||||
$salt = $data['salt'];
|
||||
$questionPos = strpos($salt, '?');
|
||||
if ($questionPos !== false) {
|
||||
parse_str(substr($salt, $questionPos + 1), $params);
|
||||
if (isset($params['expires']) && is_numeric($params['expires'])) {
|
||||
if (time() > (int) $params['expires']) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$expected_challenge = altcha_hash_hex($data['algorithm'], $salt . $data['number']);
|
||||
if (!hash_equals($expected_challenge, $data['challenge'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$expected_signature = altcha_hmac_hex($data['algorithm'], $data['challenge'], $hmac_key);
|
||||
return hash_equals($expected_signature, $data['signature']);
|
||||
}
|
||||
Reference in new issue
Block a user