Completed
Push — master ( d1ff53...78d2a6 )
by Lorenzo
02:16
created

CryptoHelper::getEncryptedString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 14
ccs 0
cts 11
cp 0
rs 9.4286
cc 2
eloc 8
nc 2
nop 1
crap 6
1
<?php
2
namespace Padosoft\TesseraSanitaria;
3
4
/**
5
 * Class CryptoHelper
6
 * @package Padosoft\TesseraSanitaria
7
 */
8
class CryptoHelper
9
{
10
	use traits\Errorable;
11
12
	protected $cert_file="";
13
	protected $tmp_path="";
14
	protected $openssl_exe_path="";
15
	public $output="";
16
	protected $returned_val="";
17
18
	/**
19
	 * CryptoHelper constructor.
20
	 *
21
	 * @param $cert_file
22
	 * @param $tmp_path
23
	 * @param $openssl_exe_path
24
	 */
25
	public function __construct($cert_file, $tmp_path, $openssl_exe_path)
26
	{
27
		$this->cert_file = $cert_file;
28
		$this->tmp_path = $tmp_path;
29
		$this->openssl_exe_path = $openssl_exe_path;
30
	}
31
32
	/**
33
	 * @param $str
34
	 *
35
	 * @return string
36
	 */
37
	public function rsaEncrypt($str)
38
	{
39
        if(!$this->checkPath()){
40
            return '';
41
        }
42
		// Path e nomi dei file temporanei
43
		$rand_name = $this->getRandName();
44
		$file_source = $this->tmp_path.$rand_name.".txt";
45
		$file_dest = $this->tmp_path.$rand_name.".enc";
46
47
		// Scrive file temporaneo sorgente
48
		file_put_contents($file_source, $str);
49
50
		// creo il comando openssl
51
		$exec = $this->getCommand($file_source, $file_dest);
52
53
		// Esegue istruzione openssl, creando file temporaneo con testo criptato
54
        $this->excecuteCommand($exec);
55
56
		// Ricava il testo criptato dal file appena creato
57
        $encrypted_txt = $this->getEncryptedString($file_dest);
58
59
        //clean
60
        $this->deleteSourceFile($file_source);
61
62
        return $encrypted_txt;
63
	}
64
65
	/**
66
	 * @return bool
67
	 */
68
	public function getError()
69
	{
70
		$result = FALSE;
71
		if($this->returned_val == 1){
72
			$result = TRUE;
73
		}
74
		return $result;
75
	}
76
77
    /**
78
     * @return bool
79
     */
80
    private function checkPath()
81
    {
82
        if (!file_exists($this->tmp_path)) {
83
            $this->addError('Il percorso della path temporanea non &egrave; valido: ' . $this->tmp_path);
84
            return false;
85
        }
86
        if (!file_exists($this->cert_file)) {
87
            $this->addError('Il percorso del file del certificato non &egrave; valido: ' . $this->cert_file);
88
            return false;
89
        }
90
        if ($this->openssl_exe_path != '' && !file_exists($this->openssl_exe_path)) {
91
            $this->addError('Il percorso di OpenSSL non &egrave; valido: ' . $this->openssl_exe_path);
92
            return false;
93
        }
94
95
        return true;
96
    }
97
98
    /**
99
     * @param $file_source
100
     * @param $file_dest
101
     *
102
     * @return string
103
     */
104
    private function getCommand($file_source, $file_dest)
105
    {
106
        return $this->openssl_exe_path . "openssl rsautl -encrypt -in " . $file_source . " -out " . $file_dest . " -inkey " . $this->cert_file . " -certin -pkcs";
107
    }
108
109
    /**
110
     * @param $exec
111
     */
112
    private function excecuteCommand($exec)
113
    {
114
        $this->output = "";
115
        exec($exec . " 2>&1", $this->output, $this->returned_val);
116
117
        if ($this->returned_val == 1) // errore
118
        {
119
            $a = $this->output;
120
            $this->addError($a[0]);
121
        }
122
    }
123
124
    /**
125
     * @param $file_dest
126
     *
127
     * @return string
128
     */
129
    private function getEncryptedString($file_dest)
130
    {
131
        $encrypted_txt = "";
132
        if (file_exists($file_dest)) {
133
            $encrypted_txt = file_get_contents($file_dest);
134
135
            // Cancella i file di appoggio
136
            unlink($file_dest);
137
        } else {
138
            $this->addError("Criptazione fallita (file destinazione non esistente)");
139
        }
140
141
        return $encrypted_txt;
142
    }
143
144
    /**
145
     * @param $file_source
146
     */
147
    private function deleteSourceFile($file_source)
148
    {
149
        if (file_exists($file_source)) {
150
            unlink($file_source);
151
        } else {
152
            $this->addError("Criptazione fallita (file sorgente non esistente)");
153
        }
154
    }
155
156
    /**
157
     * @return string
158
     */
159
    private function getRandName()
160
    {
161
        return md5(time() . mt_rand(1, 99999));
162
    }
163
}
164