PHP code example of altcha-org / altcha

1. Go to this page and download the library: Download altcha-org/altcha library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

altcha-org / altcha example snippets




ltchaOrg\Altcha\ChallengeOptions;
use AltchaOrg\Altcha\Altcha;

$hmacKey = 'secret hmac key';

// Create a new challenge
$options = new ChallengeOptions([
    'hmacKey'   => $hmacKey,
    'maxNumber' => 50000, // the maximum random number
]);

$challenge = Altcha::createChallenge($options);
echo "Challenge created: " . json_encode($challenge) . "\n";

// Example payload to verify
$payload = [
    'algorithm' => $challenge['algorithm'],
    'challenge' => $challenge['challenge'],
    'number'    => 12345, // Example number
    'salt'      => $challenge['salt'],
    'signature' => $challenge['signature'],
];

// Verify the solution
$ok = Altcha::verifySolution($payload, $hmacKey, true);

if ($ok) {
    echo "Solution verified!\n";
} else {
    echo "Invalid solution.\n";
}
sh
vendor/bin/phpunit --bootstrap src/Altcha.php tests/AltchaTest.php