<?php
function P_SIGN_DECRYPT($P_SIGN, $ACTION, $RC, $RRN, $ORDER, $AMOUNT)
{
$InData = array (
			'ACTION' => $ACTION,
			'RC' => $RC,
			'RRN' => $RRN,
			'ORDER' => $ORDER ,
			'AMOUNT' => $AMOUNT
		);

foreach($InData as $Id => $Filed) if ($Filed!= '-'  ) : $MAC .= strlen ($Filed).$Filed; else: $MAC .=$Filed; endif;
$MD5_Hash_In = strtoupper (md5 ($MAC)); 


$P_SIGNBIN = hex2bin ($P_SIGN);


$RSA_KeyPath = 'secure/victoria_pub.pem';
$RSA_Key = file_get_contents ($RSA_KeyPath);
if (!$RSA_KeyResource = openssl_get_publickey($RSA_Key)) die ('Failed get public key');

if (!openssl_public_decrypt ($P_SIGNBIN,$DECRYPTED_BIN,$RSA_Key))
{
		while ($msg = openssl_error_string()) echo $msg . "<br />\n";
		die ('Failed decrypt');
	}

$DECRYPTED = strtoupper( bin2hex ($DECRYPTED_BIN));
$Prefix = '3020300C06082A864886F70D020505000410';
$DECRYPTED_HASH=str_replace($Prefix,'',$DECRYPTED);

if ($DECRYPTED_HASH==$MD5_Hash_In) {
$RESULT="OK";
} else {
$RESULT="NOK";
}

return $RESULT;

}

?>