-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto.php
175 lines (172 loc) · 5.57 KB
/
crypto.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
class crypto
{
private $keypath;
private $content;
private $direction;
function __construct($keypath, $content='', $direction='') {
//
$this->keypath = $keypath;
$this->content = $content;
$this->direction = $direction;
//
}
/**
* @usage: $EncryptedContent = (new crypto($keyPath, $Content, 'e'))->crypto();
* @usage: $Content = (new crypto($keyPath, $EncryptedContent, 'd'))->crypto();
* @return bool|string
* @throws Exception
*/
public function crypto()
{
//
//
///////////////////////////////////////////////////////////////////////
// Sodium Cryptography Extensions Is Required
///////////////////////////////////////////////////////////////////////
//
if (!extension_loaded('sodium')) {
//
throw new Exception('crypto requires the Sodium Cryptography Extensions be installed on your server. Please visit https://www.php.net/manual/en/sodium.installation.php');
//
}
//
//
///////////////////////////////////////////////////////////////////////
// Validate Key Exists
///////////////////////////////////////////////////////////////////////
//
if (strlen($this->keypath) <= PHP_MAXPATHLEN && file_exists($this->keypath)) {
//
$getKeyData = file_get_contents($this->keypath);
//
if ( isset($getKeyData) && !empty($getKeyData) ) {
//
$keyData = $getKeyData;
//
}
else {
//
throw new Exception('crypto::crypto: Key File Did Not Load. Check that the file exists and can be read.');
//
}
//
}
else {
//
throw new Exception('crypto::crypto: Key Does Not Exist');
//
}
//
//
///////////////////////////////////////////////////////////////////////
// Encrypt
///////////////////////////////////////////////////////////////////////
//
if ( $this->direction === 'e' ) {
//
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
$cipher = base64_encode($nonce.sodium_crypto_secretbox($this->content, $nonce, $keyData));
//
sodium_memzero($this->content);
sodium_memzero($keyData);
//
return $cipher;
//
}
//
//
///////////////////////////////////////////////////////////////////////
// Decrypt
///////////////////////////////////////////////////////////////////////
//
elseif ( $this->direction === 'd' ) {
//
$decoded = base64_decode($this->content);
//
if ($decoded === false) {
//
throw new Exception('crypto::crypto: Encoding Failure');
//
}
if (mb_strlen($decoded, '8bit') < (SODIUM_CRYPTO_SECRETBOX_NONCEBYTES + SODIUM_CRYPTO_SECRETBOX_MACBYTES)) {
//
throw new Exception('crypto::crypto: Invalid Content Provided');
//
}
//
$nonce = mb_substr($decoded, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, '8bit');
$cipher = mb_substr($decoded, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, null, '8bit');
$plain = sodium_crypto_secretbox_open($cipher,$nonce,$keyData);
//
if ($plain === false) {
//
throw new Exception('crypto::crypto: Message Tampering');
//
}
//
sodium_memzero($cipher);
sodium_memzero($keyData);
//
return $plain;
//
}
else {
//
return false;
//
}
//
}
/**
* @usage: (new crypto($keyPath))->generateKey();
* @return bool
* @throws Exception
*/
public function generateKey()
{
//
//
///////////////////////////////////////////////////////////////////////
// Does Key Already Exist
///////////////////////////////////////////////////////////////////////
//
if (strlen($this->keypath) <= PHP_MAXPATHLEN && file_exists($this->keypath)) {
//
return true;
//
}
//
//
///////////////////////////////////////////////////////////////////////
// Generate Key Bytes
///////////////////////////////////////////////////////////////////////
//
$keyData = random_bytes(SODIUM_CRYPTO_SECRETBOX_KEYBYTES);
//
//
///////////////////////////////////////////////////////////////////////
// Save Key Data
///////////////////////////////////////////////////////////////////////
//
file_put_contents($this->keypath, $keyData);
//
//
///////////////////////////////////////////////////////////////////////
// Verify file was created
///////////////////////////////////////////////////////////////////////
//
if (strlen($this->keypath) <= PHP_MAXPATHLEN && file_exists($this->keypath)) {
//
return true;
//
}
else {
//
throw new Exception('crypto::generateKey: Key Failed To Be Generated');
//
}
//
}
//
}