PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 2.2.2
WooCommerce Square v2.2.2
5.5.0 5.4.3 5.4.2 5.4.1 5.4.0 trunk 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.2.0 3.3.0 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.7.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 4.0.0 4.1.0 4.2.0 4.2.1 4.2.2 4.2.3 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.4.2 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.9.0 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.6 4.9.7 4.9.8 4.9.9 5.0.0 5.0.1 5.1.0 5.1.1 5.1.2 5.2.0 5.3.0 5.3.1 5.3.2 5.3.3
woocommerce-square / includes / Utilities / Encryption_Utility.php
woocommerce-square / includes / Utilities Last commit date
Encryption_Utility.php 6 years ago Money_Utility.php 6 years ago String_Utility.php 6 years ago
Encryption_Utility.php
265 lines
1 <?php
2 /**
3 * WooCommerce Square
4 *
5 * This source file is subject to the GNU General Public License v3.0
6 * that is bundled with this package in the file license.txt.
7 * It is also available through the world-wide-web at this URL:
8 * http://www.gnu.org/licenses/gpl-3.0.html
9 * If you did not receive a copy of the license and are unable to
10 * obtain it through the world-wide-web, please send an email
11 * to license@woocommerce.com so we can send you a copy immediately.
12 *
13 * DISCLAIMER
14 *
15 * Do not edit or add to this file if you wish to upgrade WooCommerce Square to newer
16 * versions in the future. If you wish to customize WooCommerce Square for your
17 * needs please refer to https://docs.woocommerce.com/document/woocommerce-square/
18 *
19 * @author WooCommerce
20 * @copyright Copyright: (c) 2019, Automattic, Inc.
21 * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
22 */
23
24 namespace WooCommerce\Square\Utilities;
25
26 defined( 'ABSPATH' ) || exit;
27
28 use SkyVerge\WooCommerce\PluginFramework\v5_4_0 as Framework;
29
30 /**
31 * The encryption utility class.
32 *
33 * Requires OpenSSL by default.
34 *
35 * @since 2.0.0
36 */
37 class Encryption_Utility {
38
39
40 /** @var string default cipher method */
41 protected $default_cipher_method = 'AES-128-CBC';
42
43 /** @var string cipher method */
44 protected $cipher_method;
45
46
47 /**
48 * Constructs the class.
49 *
50 * @param string $preferred_cipher_method cipher method
51 */
52 public function __construct( $preferred_cipher_method = '' ) {
53
54 // bail entirely if openssl isn't available
55 if ( ! self::is_encryption_supported() ) {
56 wc_doing_it_wrong( __CLASS__, __( 'Encryption is not supported on this site.', 'woocommerce-square' ), Framework\SV_WC_Plugin::VERSION );
57 return;
58 }
59
60 $this->cipher_method = $this->get_default_cipher_method();
61
62 // if a preferred cipher method is set, check and set it
63 if ( is_string( $preferred_cipher_method ) && ! empty( $preferred_cipher_method ) ) {
64
65 // only use what's preferred if it's supported
66 if ( $this->is_cipher_method_supported( $preferred_cipher_method ) ) {
67
68 $this->cipher_method = $preferred_cipher_method;
69
70 } else { // otherwise, throw a notice and continue with the default
71
72 $message = sprintf(
73 __( '%1$s encryption is not available on this site. %2$s will be used instead.', 'woocommerce-square' ),
74 $preferred_cipher_method,
75 $this->cipher_method
76 );
77
78 wc_doing_it_wrong( __CLASS__, $message, Framework\SV_WC_Plugin::VERSION );
79 }
80 }
81 }
82
83
84 /**
85 * Encrypts data.
86 *
87 * @since 2.0.0
88 *
89 * @param string|array $data data to encrypt
90 * @param string $key encryption key
91 * @return string
92 * @throws Framework\SV_WC_Plugin_Exception
93 */
94 public function encrypt_data( $data, $key = '' ) {
95
96 // sanity check to ensure encryption can happen
97 if ( ! $this->get_cipher_method() ) {
98 throw new Framework\SV_WC_Plugin_Exception( __( 'No encryption method available', 'woocommerce-square' ) );
99 }
100
101 if ( empty( $data ) || ( ! is_string( $data ) && ! is_array( $data ) ) ) {
102 throw new Framework\SV_WC_Plugin_Exception( __( 'Data must be a non-empty string or array', 'woocommerce-square' ) );
103 }
104
105 if ( ! is_string( $key ) ) {
106 throw new Framework\SV_WC_Plugin_Exception( __( 'Encryption key must be a string', 'woocommerce-square' ) );
107 }
108
109 // default to the WP salt
110 if ( empty( $key ) ) {
111 $key = $this->get_default_key();
112 }
113
114 $vector = openssl_random_pseudo_bytes( $this->get_vector_length(), $crypto_strong );
115
116 // bail if a strong vector wasn't generated
117 if ( false === $vector || false === $crypto_strong ) {
118 throw new Framework\SV_WC_Plugin_Exception( __( 'Could not generate encryption vector.', 'woocommerce-square' ) );
119 }
120
121 $encrypted_data = openssl_encrypt( json_encode( $data ), $this->get_cipher_method(), $key, 0, $vector );
122
123 return base64_encode( $vector . $encrypted_data );
124 }
125
126
127 /**
128 * Decrypts data.
129 *
130 * @since 2.0.0
131 *
132 * @param string $data data to decrypt
133 * @param string $key decryption key
134 * @return string|array
135 * @throws Framework\SV_WC_Plugin_Exception
136 */
137 public function decrypt_data( $data, $key = '' ) {
138
139 // sanity check to ensure decryption can happen
140 if ( ! $this->get_cipher_method() ) {
141 throw new Framework\SV_WC_Plugin_Exception( __( 'No decryption method available', 'woocommerce-square' ) );
142 }
143
144 if ( empty( $data ) || ! is_string( $data ) ) {
145 throw new Framework\SV_WC_Plugin_Exception( __( 'Data must be a non-empty string', 'woocommerce-square' ) );
146 }
147
148 if ( ! is_string( $key ) ) {
149 throw new Framework\SV_WC_Plugin_Exception( __( 'Encryption key must be a string', 'woocommerce-square' ) );
150 }
151
152 // default to the WP salt
153 if ( empty( $key ) ) {
154 $key = $this->get_default_key();
155 }
156
157 $data = base64_decode( $data );
158
159 $vector_length = $this->get_vector_length();
160 $vector = substr( $data, 0, $vector_length );
161 $data = substr( $data, $vector_length );
162 $data = openssl_decrypt( $data, $this->get_cipher_method(), $key, 0, $vector );
163
164 return json_decode( $data, true );
165 }
166
167
168 /**
169 * Gets the vector length.
170 *
171 * @since 2.0.0
172 *
173 * @return int
174 */
175 protected function get_vector_length() {
176
177 return openssl_cipher_iv_length( $this->get_cipher_method() );
178 }
179
180
181 /**
182 * Determines if a cipher method is supported by the server.
183 *
184 * @since 2.0.0
185 *
186 * @param string $method cipher method to check
187 * @return bool
188 */
189 protected function is_cipher_method_supported( $method ) {
190
191 return in_array( $method, $this->get_supported_cipher_methods(), true );
192 }
193
194
195 /**
196 * Determines if encryption is supported at all.
197 *
198 * @since 2.0.0
199 *
200 * @return bool
201 */
202 public static function is_encryption_supported() {
203
204 return extension_loaded( 'openssl' );
205 }
206
207
208 /**
209 * Gets the cipher method.
210 *
211 * @since 2.0.0
212 *
213 * @return string
214 */
215 protected function get_cipher_method() {
216
217 return $this->cipher_method;
218 }
219
220
221 /**
222 * Gets the default cipher method.
223 *
224 * Checks the list of supported methods first, and if the default isn't supported, uses the first available.
225 *
226 * @since 2.0.0
227 *
228 * @return string
229 */
230 protected function get_default_cipher_method() {
231
232 $available_methods = $this->get_supported_cipher_methods();
233
234 return in_array( $this->default_cipher_method, $available_methods, true ) ? $this->default_cipher_method : $available_methods[0];
235 }
236
237
238 /**
239 * Gets the supported cipher methods.
240 *
241 * @since 2.0.0
242 *
243 * @return array
244 */
245 protected function get_supported_cipher_methods() {
246
247 return openssl_get_cipher_methods();
248 }
249
250
251 /**
252 * Gets the default encryption key.
253 *
254 * @since 2.0.0
255 *
256 * @return string
257 */
258 protected function get_default_key() {
259
260 return md5( wp_salt(), true );
261 }
262
263
264 }
265