<?php
require_once dirname(dirname(__FILE__))."/find_dsa.php"; // ../find_dsa.php
$dsa_path = find_dsaphp(FALSE);
require_once $dsa_path;

$action = "generate";
if (isset($_GET["action"])) {
	$action = $_GET["action"];
}

function gpc_magicless($data) {
	if (get_magic_quotes_gpc()) {
		$data = stripslashes($data);
	}
	return $data;
}

function load_key_from_post() {
	$keydata = gpc_magicless($_POST["keydata"]);
	$k = new DSA;
	$k->from_json($keydata);
	return $k;
}

if ($action == "generate") {
	header("Content-Type: text/plain");
	header("Cache-Control: no-cache");
	$k = new DSA;
	$k->progress_class = "Json_Line_Progress";
	$k->generate();
	echo $k->to_json();
} else if ($action == "sign") {
	header("Content-Type: text/plain");
	// Get the Key from the POST data
	$k = load_key_from_post();
	$text = gpc_magicless($_POST["text"]);
	$signature = $k->sign($text);
	echo $signature->to_json();
} else if ($action == "verify") {
	header("Content-Type: text/plain");
	$k = load_key_from_post();
	$text = gpc_magicless($_POST["text"]);
	$sig = array_map("gmp_init", json_decode(gpc_magicless($_POST["sig"])));
	echo json_encode(array($k->verify_raw($text, $sig)));
}

?>
