Repository navigation
Expand file tree
/
Copy pathEncryption.php
More file actions
146 lines (124 loc) · 3.97 KB
/
Copy pathEncryption.php
File metadata and controls
146 lines (124 loc) · 3.97 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
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
<?php
declare(strict_types=1);
namespace Rancoud\Session;
/**
* Trait Encryption.
*/
trait Encryption
{
/** @var string */
protected $key;
/** @var string */
protected $method = 'aes-256-cbc';
/**
* @param string $key
*/
public function setKey(string $key): void
{
$this->key = $key;
}
/**
* @param string $method
*
* @throws SessionException
*/
public function setMethod(string $method): void
{
if (!\in_array($method, $this->getAvailableMethods(), true)) {
throw new SessionException(\sprintf('Method unknowed: %s', $method));
}
$this->method = $method;
}
/**
* @return array
*/
public function getAvailableMethods(): array
{
$ciphers = \openssl_get_cipher_methods();
$ciphersAndAliases = \openssl_get_cipher_methods(true);
$cipherAliases = \array_diff($ciphersAndAliases, $ciphers);
$ciphers = \array_filter($ciphers, function ($n) {
return \mb_stripos($n, 'ecb') === false;
});
$ciphers = \array_filter($ciphers, function ($c) {
return \mb_stripos($c, 'des') === false;
});
$ciphers = \array_filter($ciphers, function ($c) {
return \mb_stripos($c, 'rc2') === false;
});
$ciphers = \array_filter($ciphers, function ($c) {
return \mb_stripos($c, 'rc4') === false;
});
$ciphers = \array_filter($ciphers, function ($c) {
return \mb_stripos($c, 'md5') === false;
});
$ciphers = \array_filter($ciphers, function ($c) {
return \mb_stripos($c, '-ocb') === false;
});
$ciphers = \array_filter($ciphers, function ($c) {
return \mb_stripos($c, '-ccm') === false;
});
$ciphers = \array_filter($ciphers, function ($c) {
return \mb_stripos($c, '-gcm') === false;
});
$ciphers = \array_filter($ciphers, function ($c) {
return \mb_stripos($c, '-wrap') === false;
});
$cipherAliases = \array_filter($cipherAliases, function ($c) {
return \mb_stripos($c, 'des') === false;
});
$cipherAliases = \array_filter($cipherAliases, function ($c) {
return \mb_stripos($c, 'rc2') === false;
});
$cipherAliases = \array_filter($cipherAliases, function ($c) {
return \mb_stripos($c, '-wrap') === false;
});
$methods = \array_merge($ciphers, $cipherAliases);
$methods = \array_filter($methods, function ($c) {
$forbiddenMethods = ['AES-128-CBC-HMAC-SHA1', 'AES-256-CBC-HMAC-SHA1',
'aes-128-cbc-hmac-sha1', 'aes-256-cbc-hmac-sha1'];
return !\in_array($c, $forbiddenMethods, true);
});
return $methods;
}
/**
* @param string $data
*
* @throws SessionException
*
* @return string|bool
*/
public function decrypt(string $data)
{
$this->throwExceptionIfKeyEmpty();
if ($data === '') {
return '';
}
list($encrypted_data, $iv) = \explode('::', \base64_decode($data, true), 2);
return \openssl_decrypt($encrypted_data, $this->method, $this->key, 0, $iv);
}
/**
* @param string $data
*
* @throws SessionException
*
* @return string
*/
public function encrypt(string $data): string
{
$this->throwExceptionIfKeyEmpty();
$iv = \openssl_random_pseudo_bytes(\openssl_cipher_iv_length($this->method));
$encrypted = \openssl_encrypt($data, $this->method, $this->key, 0, $iv);
$data = \base64_encode($encrypted . '::' . $iv);
return $data;
}
/**
* @throws SessionException
*/
protected function throwExceptionIfKeyEmpty(): void
{
if ($this->key === null || $this->key === '') {
throw new SessionException('Key has to be a non-empty string');
}
}
}