1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace RichardStyles\EloquentEncryption\FileSystem; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Facades\Config; |
8
|
|
|
use Illuminate\Support\Facades\Storage; |
9
|
|
|
use RichardStyles\EloquentEncryption\Contracts\RsaKeyHandler; |
10
|
|
|
use RichardStyles\EloquentEncryption\Exceptions\RSAKeyFileMissing; |
11
|
|
|
|
12
|
|
|
class RsaKeyStorageHandler implements RsaKeyHandler |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Storage path for the Public Key File |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $public_key_path; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Storage path for the Private Key File |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $private_key_path; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* ApplicationKey constructor. |
31
|
|
|
*/ |
32
|
|
|
public function __construct() |
33
|
|
|
{ |
34
|
|
|
$this->public_key_path = |
35
|
|
|
Config::get('eloquent_encryption.key.public', 'eloquent_encryption.pub'); |
36
|
|
|
$this->private_key_path = |
37
|
|
|
Config::get('eloquent_encryption.key.private', 'eloquent_encryption'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Have any RSA keys been generated |
42
|
|
|
* |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
|
|
public function exists() |
46
|
|
|
{ |
47
|
|
|
return $this->hasPrivateKey() && $this->hasPublicKey(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* A Private key file exists |
52
|
|
|
* |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
|
|
public function hasPrivateKey() |
56
|
|
|
{ |
57
|
|
|
return Storage::exists($this->private_key_path); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* A Public key file exists |
62
|
|
|
* |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
|
|
public function hasPublicKey() |
66
|
|
|
{ |
67
|
|
|
return Storage::exists($this->public_key_path); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Save the generated RSA key to the storage location |
72
|
|
|
* |
73
|
|
|
* @param $public |
74
|
|
|
* @param $private |
75
|
|
|
*/ |
76
|
|
|
public function saveKey($public, $private) |
77
|
|
|
{ |
78
|
|
|
Storage::put($this->public_key_path, $public); |
79
|
|
|
Storage::put($this->private_key_path, $private); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the contents of the public key file |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
* @throws RSAKeyFileMissing |
87
|
|
|
*/ |
88
|
|
|
public function getPublicKey() |
89
|
|
|
{ |
90
|
|
|
if (!$this->hasPublicKey()) { |
91
|
|
|
throw new RSAKeyFileMissing(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return Storage::get($this->public_key_path); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get the contents of the private key file |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
* @throws RSAKeyFileMissing |
102
|
|
|
*/ |
103
|
|
|
public function getPrivateKey() |
104
|
|
|
{ |
105
|
|
|
if (!$this->hasPrivateKey()) { |
106
|
|
|
throw new RSAKeyFileMissing(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return Storage::get($this->private_key_path); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|