1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RichardStyles\EloquentEncryption; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Config; |
6
|
|
|
use phpseclib\Crypt\RSA; |
7
|
|
|
use RichardStyles\EloquentEncryption\Contracts\RsaKeyHandler; |
8
|
|
|
use RichardStyles\EloquentEncryption\Exceptions\InvalidRsaKeyHandler; |
9
|
|
|
use RichardStyles\EloquentEncryption\Exceptions\RSAKeyFileMissing; |
10
|
|
|
use RichardStyles\EloquentEncryption\FileSystem\RsaKeyStorageHandler; |
11
|
|
|
|
12
|
|
|
class EloquentEncryption |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var RsaKeyHandler |
17
|
|
|
*/ |
18
|
|
|
private $handler; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* ApplicationKey constructor. |
22
|
|
|
*/ |
23
|
|
|
public function __construct() |
24
|
|
|
{ |
25
|
|
|
$this->handler = app()->make( |
26
|
|
|
Config::get('eloquent_encryption.handler', RsaKeyStorageHandler::class) |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
if(!$this->handler instanceof RsaKeyHandler){ |
30
|
|
|
throw new InvalidRsaKeyHandler; |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Have any RSA keys been generated |
36
|
|
|
* |
37
|
|
|
* @return bool |
38
|
|
|
*/ |
39
|
|
|
public function exists() |
40
|
|
|
{ |
41
|
|
|
return $this->handler->exists(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Generate a set of RSA Keys which will be used to encrypt the database fields |
46
|
|
|
*/ |
47
|
|
|
public function makeEncryptionKeys() |
48
|
|
|
{ |
49
|
|
|
$key = $this->createKey(Config::get('eloquent_encryption.key.email')); |
50
|
|
|
$this->handler->saveKey($key['publickey'], $key['privatekey']); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Create a digital set of RSA keys, defaulting to 4096-bit |
55
|
|
|
* |
56
|
|
|
* @param string $email |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
public function createKey($email = '') |
60
|
|
|
{ |
61
|
|
|
$rsa = new RSA(); |
62
|
|
|
$rsa->setPublicKeyFormat(RSA::PUBLIC_FORMAT_OPENSSH); |
63
|
|
|
$rsa->setComment($email); |
64
|
|
|
|
65
|
|
|
return $rsa->createKey(Config::get('eloquent_encryption.key.length', 4096)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Helper function to ensure RSA options match for encrypting/decrypting |
70
|
|
|
* |
71
|
|
|
* @param $key |
72
|
|
|
* @return RSA |
73
|
|
|
*/ |
74
|
|
|
private function getRsa($key) |
75
|
|
|
{ |
76
|
|
|
$rsa = new RSA(); |
77
|
|
|
$rsa->loadKey($key); |
78
|
|
|
$rsa->setEncryptionMode(RSA::ENCRYPTION_OAEP); |
79
|
|
|
|
80
|
|
|
return $rsa; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Encrypt a value using the RSA key |
85
|
|
|
* |
86
|
|
|
* @param $value |
87
|
|
|
* @return false|string |
88
|
|
|
* @throws RSAKeyFileMissing |
89
|
|
|
*/ |
90
|
|
|
public function encrypt($value) |
91
|
|
|
{ |
92
|
|
|
return $this->getRsa($this->handler->getPublicKey()) |
93
|
|
|
->encrypt($value); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Decrypt a value using the RSA key |
98
|
|
|
* |
99
|
|
|
* @param $value |
100
|
|
|
* @return false|string|null |
101
|
|
|
* @throws RSAKeyFileMissing |
102
|
|
|
*/ |
103
|
|
|
public function decrypt($value) |
104
|
|
|
{ |
105
|
|
|
if (empty($value)) { |
106
|
|
|
return null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $this->getRsa($this->handler->getPrivateKey()) |
110
|
|
|
->decrypt($value); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function __call($name, $arguments) |
114
|
|
|
{ |
115
|
|
|
if(method_exists($this->handler, $name)){ |
116
|
|
|
return $this->handler->{$name}($arguments); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|