<commit_before><?php

namespace FatcaIdesPhp;
use Monolog\Logger;

class Receiver {

var $data; // php data with fatca information
var $dataXml;
var $dataXmlSigned;
var $dataCompressed;
var $dataEncrypted;
var $diDigest;
var $aeskey;
var $tf1;
var $tf2;
var $tf3;
var $tf4;
var $aesEncrypted;
var $ts;
var $file_name;

var $from;
var $to;

// config: php array
// tf4: temp file?
function __construct($conMan,$tf4=false) {
  $this->tf4=$tf4;
  assert($conMan instanceOf ConfigManager);
  $this->conMan=$conMan;
}

function start() {
  $this->conMan->check();
  
	if(!$this->tf4) {
		$this->tf4=sys_get_temp_dir();
		$temp_file = tempnam(sys_get_temp_dir(), 'Tux');
		unlink($temp_file);
		mkdir($temp_file);
		$this->tf4=$temp_file;
	} else {
    if(!file_exists($this->tf4)) {
      throw new \Exception(sprintf("Passed folder inexistant: '%s'",$this->tf4));
    }
  }

	$this->tf1=tempnam("/tmp","");
	$this->tf2=tempnam("/tmp","");
	$this->tf3=tempnam("/tmp","");

	$this->ts=time();
	$this->ts2=strftime("%Y-%m-%dT%H:%M:%SZ",$this->ts);
}

function fromZip($filename) {
	$zip = new \ZipArchive();
	if ($zip->open($filename) === TRUE) {
	    $zip->extractTo($this->tf4);
	    $zip->close();
	} else {
	    throw new \Exception(sprintf("failed to open archive: '%s'",$filename));
	}

	$xx=scandir($this->tf4);
	$this->files["payload"]=array_values(preg_grep("/.*_Payload/",$xx));
	$this->files["payload"]=$this->files["payload"][0];
	$this->from=preg_replace("/(.*)_Payload/","$1",$this->files["payload"]);
	$this->files["key"]=array_values(preg_grep("/.*_Key/",$xx));
	$this->files["key"]=$this->files["key"][0];
	$this->to  =preg_replace("/(.*)_Key/","$1",$this->files["key"]);

	  $fp=fopen($this->tf4."/".$this->files["key"],"r");
	  $this->aesEncrypted=fread($fp,8192);
	  fclose($fp);
}

	function decryptAesKey() {
		$this->aeskey="";
		if(!openssl_private_decrypt( $this->aesEncrypted , $this->aeskey , $this->readFfaPrivateKey() )) throw new \Exception("Could not decrypt aes key");
		if($this->aeskey=="") throw new \Exception("Failed to decrypt AES key");
	}

	function readFfaPrivateKey($returnResource=true) {
	  $kk=($this->from==$this->conMan->config["ffaidReceiver"]?$this->conMan->config["FatcaKeyPrivate"]:($this->from==$this->conMan->config["ffaid"]?$this->conMan->config["FatcaIrsPublic"]:die("WTF")));
	  $fp=fopen($kk,"r");
	  $priv_key_string=fread($fp,8192);
	  fclose($fp);
	  if($returnResource) {
		$priv_key="";
		$priv_key=openssl_get_privatekey($priv_key_string); 
		return $priv_key;
	  } else {
		return $priv_key_string;
	  }
	}

	function fromEncrypted() {
		$key_size =  strlen($this->aeskey);
		if($key_size!=32) throw new \Exception("Invalid key size ".$key_size);

		$fp=fopen($this->tf4."/".$this->files["payload"],"r");
		$this->dataEncrypted=fread($fp,8192);
		fclose($fp);

		$this->dataCompressed = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->aeskey, $this->dataEncrypted, "ecb");
	}

	function fromCompressed() {
		$tf3=tempnam("/tmp","");
		file_put_contents($tf3,$this->dataCompressed);

		$zip = new \ZipArchive();
		if ($zip->open($tf3) === TRUE) {
			$this->dataXmlSigned=$zip->getFromIndex(0);
			$zip->close();
		} else {
		    throw new \Exception('failed to read compressed data');
		}
	}

  public static function shortcut($config,$zipFn=null,$credentials=null,$idesServer=null,$LOG_LEVEL=Logger::WARNING) {

    assert(is_null($zipFn) xor is_null($credentials));

    $dm = new Downloader(null,$LOG_LEVEL);
    $cm = new ConfigManager($config,$dm,$LOG_LEVEL);

    if(is_null($zipFn)) {
      assert(is_array($credentials) && array_key_exists("username",$credentials) && array_key_exists("password",$credentials));
      assert(!is_null($idesServer) && in_array($idesServer,array("live","test")));<commit_msg>switch from ecb to cbc<commit_after><?php

namespace FatcaIdesPhp;
use Monolog\Logger;

class Receiver {

var $data; // php data with fatca information
var $dataXml;
var $dataXmlSigned;
var $dataCompressed;
var $dataEncrypted;
var $diDigest;
var $aeskey;
var $tf1;
var $tf2;
var $tf3;
var $tf4;
var $aesEncrypted;
var $ts;
var $file_name;

var $from;
var $to;

// config: php array
// tf4: temp file?
function __construct($conMan,$tf4=false) {
  $this->tf4=$tf4;
  assert($conMan instanceOf ConfigManager);
  $this->conMan=$conMan;
}

function start() {
  $this->conMan->check();
  
	if(!$this->tf4) {
		$this->tf4=sys_get_temp_dir();
		$temp_file = tempnam(sys_get_temp_dir(), 'Tux');
		unlink($temp_file);
		mkdir($temp_file);
		$this->tf4=$temp_file;
	} else {
    if(!file_exists($this->tf4)) {
      throw new \Exception(sprintf("Passed folder inexistant: '%s'",$this->tf4));
    }
  }

	$this->tf1=tempnam("/tmp","");
	$this->tf2=tempnam("/tmp","");
	$this->tf3=tempnam("/tmp","");

	$this->ts=time();
	$this->ts2=strftime("%Y-%m-%dT%H:%M:%SZ",$this->ts);
}

function fromZip($filename) {
	$zip = new \ZipArchive();
	if ($zip->open($filename) === TRUE) {
	    $zip->extractTo($this->tf4);
	    $zip->close();
	} else {
	    throw new \Exception(sprintf("failed to open archive: '%s'",$filename));
	}

	$xx=scandir($this->tf4);
	$this->files["payload"]=array_values(preg_grep("/.*_Payload/",$xx));
	$this->files["payload"]=$this->files["payload"][0];
	$this->from=preg_replace("/(.*)_Payload/","$1",$this->files["payload"]);
	$this->files["key"]=array_values(preg_grep("/.*_Key/",$xx));
	$this->files["key"]=$this->files["key"][0];
	$this->to  =preg_replace("/(.*)_Key/","$1",$this->files["key"]);

	  $fp=fopen($this->tf4."/".$this->files["key"],"r");
	  $this->aesEncrypted=fread($fp,8192);
	  fclose($fp);
}

	function decryptAesKey() {
		$this->aeskey="";
		if(!openssl_private_decrypt( $this->aesEncrypted , $this->aeskey , $this->readFfaPrivateKey() )) throw new \Exception("Could not decrypt aes key");
		if($this->aeskey=="") throw new \Exception("Failed to decrypt AES key");
	}

	function readFfaPrivateKey($returnResource=true) {
	  $kk=($this->from==$this->conMan->config["ffaidReceiver"]?$this->conMan->config["FatcaKeyPrivate"]:($this->from==$this->conMan->config["ffaid"]?$this->conMan->config["FatcaIrsPublic"]:die("WTF")));
	  $fp=fopen($kk,"r");
	  $priv_key_string=fread($fp,8192);
	  fclose($fp);
	  if($returnResource) {
		$priv_key="";
		$priv_key=openssl_get_privatekey($priv_key_string); 
		return $priv_key;
	  } else {
		return $priv_key_string;
	  }
	}

	function fromEncrypted() {
    // split aeskey entry into aeskey + iv
    // Reference: https://www.irs.gov/businesses/corporations/fatca-ides-technical-faqs#EncryptionE21
    $iv = substr($this->aeskey,32,16);
    $aeskey = substr($this->aeskey,0,32);
    $key_size =  strlen($aeskey);
		if($key_size!=32) throw new \Exception("Invalid key size ".$key_size);

		$fp=fopen($this->tf4."/".$this->files["payload"],"r");
		$this->dataEncrypted=fread($fp,8192);
		fclose($fp);

		$this->dataCompressed = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $aeskey, $this->dataEncrypted, "cbc", $iv);
	}

	function fromCompressed() {
		$tf3=tempnam("/tmp","");
		file_put_contents($tf3,$this->dataCompressed);

		$zip = new \ZipArchive();
		if ($zip->open($tf3) === TRUE) {
			$this->dataXmlSigned=$zip->getFromIndex(0);
			$zip->close();
		} else {
		    throw new \Exception('failed to read compressed data');
		}
	}

  public static function shortcut($config,$zipFn=null,$credentials=null,$idesServer=null,$LOG_LEVEL=Logger::WARNING) {

    assert(is_null($zipFn) xor is_null($credentials));

    $dm = new Downloader(null,$LOG_LEVEL);
    $cm = new ConfigManager($config,$dm,$LOG_LEVEL);

    if(is_null($zipFn)) {
      assert(is_array($credentials) && array_key_exists("username",$credentials) && array_key_exists("password",$credentials));
      assert(!is_null($idesServer) && in_array($idesServer,array("live","test")));
{'fraction_non_alphanumeric': 0.2234536082474227, 'fraction_numerical': 0.013015463917525774, 'mean_word_length': 5.505448449287511, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 123, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6861796', 'n_tokens_mistral': 3139, 'n_tokens_neox': 2953, 'n_words': 424}