
<?php
	if (!extension_loaded('infosec'))
	{
		if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
  		{
			dl('php_infosec.dll');
		}
   		else
		{
			dl('infosec.so');
		}
 	}
	else
	{
		echo "loaded infosec module success <br/>";
	}

	if(isset($_POST["sign"]))
	{
		$plaint=$_POST["plaint"];
		if(strlen($plaint) <= 0)
		{
			echo "WARNING : no source data input<br/>";
			exit();
		}
		$keyfile=$_POST["key"];
		if(strlen($keyfile) <= 0)
		{
			echo "WARNING : no key data input<br/>";
			exit();
		}
		//read private key from file
		//$fd = "pri-CEA.key";
		$fp = fopen($keyfile,"rb");
		if($fp == NULL)
		{
			echo "open file error<br/>";
			exit();
		}

		fseek($fp,0,SEEK_END);
		$filelen=ftell($fp);
		fseek($fp,0,SEEK_SET);
		$contents = fread($fp,$filelen);
		fclose($fp);
		
		$key = substr($contents,2);

		$pass=$_POST["pass"];
		if(strlen($pass) <= 0)
		{
			echo "WARNING : no key password input<br/>";
			exit();
		}
		else
		{
			$signature = sign($plaint,$key,$pass);
			$code = current($signature);
			$len = next($signature);
			$signcode = base64enc($code);
			echo "signature : ",current($signcode),"<br/>";
			echo "signature len: ",$len,"<br/>";
		}
	}
	exit();
?>
