forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExportEncrypted.php
More file actions
51 lines (38 loc) · 1.23 KB
/
ExportEncrypted.php
File metadata and controls
51 lines (38 loc) · 1.23 KB
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
<?php
namespace ProcessMaker\ImportExport;
class ExportEncrypted
{
private $key;
private $method;
public function __construct(string $password)
{
$this->key = hash('sha256', $password, true);
$this->method = config('app.cipher', 'AES-256-CBC');
}
/**
* Encrypt the export key.
*/
public function call(array $package): array
{
$plaintext = json_encode($package['export']);
$iv = openssl_random_pseudo_bytes(16);
$ciphertext = openssl_encrypt($plaintext, $this->method, $this->key, OPENSSL_RAW_DATA, $iv);
$base64 = base64_encode($iv . $ciphertext);
$package['export'] = $base64;
$package['encrypted'] = true;
return $package;
}
/**
* Decrypt the export key using the given password.
*/
public function decrypt(array $package): array
{
$base64 = $package['export'];
$ciphertext_iv = base64_decode($base64);
$iv = substr($ciphertext_iv, 0, 16);
$ciphertext = substr($ciphertext_iv, 16);
$plaintext = openssl_decrypt($ciphertext, $this->method, $this->key, OPENSSL_RAW_DATA, $iv);
$package['export'] = json_decode($plaintext, true);
return $package;
}
}