Subversion Repositories php_clientchallenge

Compare Revisions

Regard whitespace Rev 4 → Rev 3

/trunk/ClientChallenge.class.php
21,52 → 21,26
 
class ClientChallenge {
 
private static function sha3_512($message, $raw_output=false) {
private static function sha3_512($password, $raw_output=false) {
if (version_compare(PHP_VERSION, '7.1.0') >= 0) {
return hash('sha3-512', $message, $raw_output);
return hash('sha3-512', $password, $raw_output);
} else {
return \bb\Sha3\Sha3::hash($message, 512, $raw_output);
return \bb\Sha3\Sha3::hash($password, 512, $raw_output);
}
}
 
private static function sha3_512_hmac($message, $key, $raw_output=false) {
// RFC 2104 HMAC
if (version_compare(PHP_VERSION, '7.1.0') >= 0) {
return hash_hmac('sha3-512', $message, $key, $raw_output);
} else {
$blocksize = 576; // block size of sha-512!
public static function checkValidation($max_time=10) {
 
if (strlen($key) > ($blocksize/8)) {
$k_ = sha3_512($key,true);
} else {
$k_ = $key;
}
if (!isset($_REQUEST['vts_validation_result'])) throw new Exception('No challenge response found');
 
$k_opad = str_repeat(chr(0x5C),($blocksize/8));
$k_ipad = str_repeat(chr(0x36),($blocksize/8));
for ($i=0; $i<strlen($k_); $i++) {
$k_opad[$i] = $k_opad[$i] ^ $k_[$i];
$k_ipad[$i] = $k_ipad[$i] ^ $k_[$i];
}
list($starttime, $ip_target, $challenge, $answer) = @json_decode($_REQUEST['vts_validation_result'], true);
 
return sha3_512($k_opad . sha3_512($k_ipad . $message, true));
}
}
 
public static function checkValidation($max_time=10, $server_secret) {
 
if (!isset($_REQUEST['vts_validation_result'])) throw new \Exception('No challenge response found');
 
list($starttime, $ip_target, $challenge, $answer, $challenge_integrity) = @json_decode($_REQUEST['vts_validation_result'], true);
 
if ($ip_target != $_SERVER['REMOTE_ADDR']) {
throw new \Exception('Wrong IP');
throw new Exception('Wrong IP');
} else if (time()-$starttime > $max_time) {
throw new \Exception('Challenge expired');
} else if ($challenge_integrity != self::sha3_512_hmac($challenge,$server_secret)) {
throw new \Exception('Challenge integrity failed');
throw new Exception('Challenge expired');
} else if ($challenge !== self::sha3_512($starttime.'/'.$ip_target.'/'.$answer)) {
throw new \Exception('Wrong answer');
throw new Exception('Wrong answer');
} else {
return true;
}
73,7 → 47,7
}
 
// This is only called by ajax_get_challenge.php
public static function createChallenge($complexity=500000, $server_secret) {
public static function createChallenge($complexity=500000) {
 
$min = 0;
$max = $complexity;
86,10 → 60,8
 
$challenge = self::sha3_512($starttime.'/'.$ip_target.'/'.$random);
 
$challenge_integrity = self::sha3_512_hmac($challenge,$server_secret);
$send_to_client = array($starttime, $ip_target, $challenge, $min, $max);
 
$send_to_client = array($starttime, $ip_target, $challenge, $min, $max, $challenge_integrity);
 
header('Content-Type:application/json');
die(json_encode($send_to_client));
 
/trunk/ClientChallenge.js
27,11 → 27,10
var challenge = data[2];
var min = data[3];
var max = data[4];
var challenge_integrity = data[5];
for (i=min; i<=max; i++) {
if (challenge == sha3_512(starttime+"/"+ip_target+"/"+i)) {
var answer = i;
var vts_validation_result = JSON.stringify([starttime, ip_target, challenge, answer, challenge_integrity]);
var vts_validation_result = JSON.stringify([starttime, ip_target, challenge, answer]);
callback(params, vts_validation_result);
break;
}
/trunk/example/config.inc.php
File deleted
/trunk/example/ajax_example.php
23,18 → 23,12
 
require_once __DIR__ . '/../ClientChallenge.class.php';
 
require_once __DIR__ . '/config.inc.php';
define('MAX_TIME', 10); // seconds
 
if (isset($_REQUEST['action']) && ($_REQUEST['action'] === 'add_numbers')) {
 
// Check request field "vts_validation_result" for valid response of the Challenge
try {
\ViaThinkSoft\RateLimitingChallenge\ClientChallenge::checkValidation(MAX_TIME, VTS_CS_SERVER_SECRET);
} catch (\Exception $e) {
$res = array("error" => $e->getMessage());
header('Content-Type:application/json');
die(json_encode($res));
}
\ViaThinkSoft\RateLimitingChallenge\ClientChallenge::checkValidation(MAX_TIME);
 
// Do your stuff here. Example:
$a = $_REQUEST['a'];
/trunk/example/ajax_get_challenge.php
23,6 → 23,6
 
require_once __DIR__ . '/../ClientChallenge.class.php';
 
require_once __DIR__ . '/config.inc.php';
define('COMPLEXITY', 500000);
 
\ViaThinkSoft\RateLimitingChallenge\ClientChallenge::createChallenge(COMPLEXITY, VTS_CS_SERVER_SECRET);
\ViaThinkSoft\RateLimitingChallenge\ClientChallenge::createChallenge(COMPLEXITY);
/trunk/example/index.html
28,12 → 28,7
"b": params['b']
},
success: function(data) {
if ("error" in data) {
$("#out").val('ERROR');
alert(data["error"]);
} else {
$("#out").val(data["result"]);
}
},
error: error_cb
});