1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MediaMonks\Doctrine\Transformable\Transformer; |
4
|
|
|
|
5
|
|
|
use ParagonIE\Halite\Halite; |
6
|
|
|
use ParagonIE\Halite\HiddenString; |
7
|
|
|
use ParagonIE\Halite\KeyFactory; |
8
|
|
|
use ParagonIE\Halite\Symmetric\Crypto; |
9
|
|
|
use ParagonIE\Halite\Symmetric\EncryptionKey; |
10
|
|
|
|
11
|
|
|
class HaliteSymmetricTransformer implements TransformerInterface |
12
|
|
|
{ |
13
|
|
|
const HALITE_LEGACY_VERSION = '1.0.0'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var bool |
17
|
|
|
*/ |
18
|
|
|
private $binary = true; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var EncryptionKey |
22
|
|
|
*/ |
23
|
|
|
private $encryptionKey = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param $encryptionKey |
27
|
|
|
* @param array $options |
28
|
|
|
*/ |
29
|
4 |
|
public function __construct($encryptionKey, array $options = []) |
30
|
|
|
{ |
31
|
4 |
|
$this->encryptionKey = KeyFactory::loadEncryptionKey($encryptionKey); |
32
|
|
|
|
33
|
4 |
|
$this->setOptions($options); |
34
|
4 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param array $options |
38
|
|
|
*/ |
39
|
4 |
|
protected function setOptions(array $options) |
40
|
|
|
{ |
41
|
4 |
|
if (array_key_exists('binary', $options)) { |
42
|
2 |
|
$this->binary = $options['binary']; |
43
|
2 |
|
} |
44
|
4 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
1 |
|
public function getBinary() |
50
|
|
|
{ |
51
|
1 |
|
return $this->binary; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $value |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
3 |
View Code Duplication |
public function transform($value) |
|
|
|
|
59
|
|
|
{ |
60
|
3 |
|
if (empty($value)) { |
61
|
1 |
|
return null; |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
if ($this->binary) { |
65
|
1 |
|
$value = \Sodium\bin2hex($value); |
66
|
1 |
|
} |
67
|
|
|
|
68
|
2 |
|
if (Halite::VERSION > self::HALITE_LEGACY_VERSION) { |
69
|
|
|
$value = new HiddenString($value); |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
return Crypto::encrypt($value, $this->encryptionKey); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $value |
77
|
|
|
* @return string|null |
78
|
|
|
*/ |
79
|
3 |
View Code Duplication |
public function reverseTransform($value) |
|
|
|
|
80
|
|
|
{ |
81
|
3 |
|
if (empty($value)) { |
82
|
1 |
|
return null; |
83
|
|
|
} |
84
|
|
|
|
85
|
2 |
|
$decryptedValue = Crypto::decrypt($value, $this->encryptionKey); |
86
|
|
|
|
87
|
2 |
|
if (Halite::VERSION > self::HALITE_LEGACY_VERSION) { |
88
|
|
|
$decryptedValue = $decryptedValue->getString(); |
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
|
if (!$this->binary) { |
92
|
1 |
|
return $decryptedValue; |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
return \Sodium\hex2bin($decryptedValue); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.