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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Analytics\Model;
use Magento\Framework\Exception\LocalizedException;
/**
* Class for encrypting data.
*/
class Cryptographer
{
/**
* Resource for handling MBI token value.
*
* @var AnalyticsToken
*/
private $analyticsToken;
/**
* Cipher method for encryption.
*
* @var string
*/
private $cipherMethod = 'AES-256-CBC';
/**
* @var EncodedContextFactory
*/
private $encodedContextFactory;
/**
* @param AnalyticsToken $analyticsToken
* @param EncodedContextFactory $encodedContextFactory
*/
public function __construct(
AnalyticsToken $analyticsToken,
EncodedContextFactory $encodedContextFactory
) {
$this->analyticsToken = $analyticsToken;
$this->encodedContextFactory = $encodedContextFactory;
}
/**
* Encrypt input data.
*
* @param string $source
* @return EncodedContext
* @throws LocalizedException
*/
public function encode($source)
{
if (!is_string($source)) {
try {
$source = (string)$source;
} catch (\Exception $e) {
throw new LocalizedException(
__(
'The data is invalid. '
. 'Enter the data as a string or data that can be converted into a string and try again.'
)
);
}
} elseif (!$source) {
throw new LocalizedException(__('The data is invalid. Enter the data as a string and try again.'));
}
if (!$this->validateCipherMethod($this->cipherMethod)) {
throw new LocalizedException(__('The data is invalid. Use a valid cipher method and try again.'));
}
$initializationVector = $this->getInitializationVector();
$encodedContext = $this->encodedContextFactory->create([
'content' => openssl_encrypt(
$source,
$this->cipherMethod,
$this->getKey(),
OPENSSL_RAW_DATA,
$initializationVector
),
'initializationVector' => $initializationVector,
]);
return $encodedContext;
}
/**
* Return key for encryption.
*
* @return string
* @throws LocalizedException
*/
private function getKey()
{
$token = $this->analyticsToken->getToken();
if (!$token) {
throw new LocalizedException(__('Enter the encryption key and try again.'));
}
return hash('sha256', $token);
}
/**
* Return established cipher method.
*
* @return string
*/
private function getCipherMethod()
{
return $this->cipherMethod;
}
/**
* Return each time generated random initialization vector which depends on the cipher method.
*
* @return string
*/
private function getInitializationVector()
{
$ivSize = openssl_cipher_iv_length($this->getCipherMethod());
return openssl_random_pseudo_bytes($ivSize);
}
/**
* Check that cipher method is allowed for encryption.
*
* @param string $cipherMethod
* @return bool
*/
private function validateCipherMethod($cipherMethod)
{
$methods = array_map(
'strtolower',
openssl_get_cipher_methods()
);
$cipherMethod = strtolower($cipherMethod);
return (false !== array_search($cipherMethod, $methods));
}
}