PluginProbe ʕ •ᴥ•ʔ
Really Simple Security – Simple and Performant Security (formerly Really Simple SSL) / 9.5.11
Really Simple Security – Simple and Performant Security (formerly Really Simple SSL) v9.5.11
9.5.11 9.5.10.1 9.5.10 trunk 9.4.0 9.4.1 9.4.2 9.4.3 9.5.0 9.5.0.1 9.5.0.2 9.5.1 9.5.2 9.5.2.2 9.5.2.3 9.5.3 9.5.3.1 9.5.3.2 9.5.4 9.5.5 9.5.6 9.5.7 9.5.8 9.5.9
really-simple-ssl / lib / admin / class-encryption.php
really-simple-ssl / lib / admin Last commit date
class-encryption.php 4 weeks ago class-helper.php 4 weeks ago
class-encryption.php
144 lines
1 <?php
2 namespace RSSSL\lib\admin;
3
4 require_once __DIR__ . '/class-helper.php';
5
6 /**
7 * Trait admin helper
8 *
9 *
10 * @package RSSSL\lib\admin\encryption
11 * @since 8.2
12 *
13 * @author Really Simple Security
14 * @see https://really-simple-ssl.com
15 */
16 trait Encryption {
17 use Helper;
18
19 /**
20 * Encrypt a string with a prefix. If the prefix is already there, it's already encrypted
21 *
22 * @param string $data
23 * @param string $prefix
24 *
25 * @return string
26 */
27
28 public function encrypt_with_prefix( string $data, string $prefix = 'rsssl_'):string {
29 if ( strpos($data, $prefix) === 0 ) {
30 return $data;
31 }
32
33 $data = $this->encrypt($data);
34 return $prefix . $data;
35 }
36
37 /**
38 * Decrypt data if prefixed. If not prefixed, return the data, as it is already decrypted
39 *
40 * @param string $data
41 * @param string $prefix
42 *
43 * @return string
44 */
45 public function decrypt_if_prefixed( string $data, string $prefix = 'rsssl_', string $deprecated_key = '' ):string{
46 if ( strpos($data, $prefix) !== 0 ) {
47 return $data;
48 }
49 $data = substr($data, strlen($prefix));
50
51 return $this->decrypt($data, 'string', $deprecated_key);
52 }
53
54 /**
55 * Encrypt a string.
56 *
57 * @param array|string $data
58 * @param string $type //ARRAY or STRING
59 * @param string $publicFacingKey Use this param to pass an encryption key
60 * @return string
61 */
62 public function encrypt( $data, string $type = 'string', string $publicFacingKey = '' ): string {
63
64 $key = ! empty( $publicFacingKey ) ? $publicFacingKey : $this->get_encryption_key();
65
66 if ( 'array' === strtolower( $type ) ) {
67 $data = serialize($data);
68 }
69
70 if ( strlen( trim( $data ) ) === 0 ) {
71 return '';
72 }
73
74 $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
75 $encrypted = openssl_encrypt($data, 'aes-256-cbc', $key, 0, $iv);
76 return base64_encode($encrypted . '::' . $iv);
77 }
78
79 /**
80 * Decrypt data
81 *
82 * @param mixed $data
83 * @param string $type
84 * @param string $deprecated_key
85 *
86 * @return array|string
87 */
88 public function decrypt( $data, string $type = 'string', $deprecated_key = '' ) {
89 // Check if user is logged in
90 $key = ! empty( $deprecated_key ) ? $deprecated_key : $this->get_encryption_key();
91
92 // If $data is empty, return appropriate empty value based on type
93 if ( empty( $data ) ) {
94 return strtolower( $type ) === 'string' ? '' : [];
95 }
96
97 // If $data is not a string (i.e., it's already an array), return it as is
98 if ( ! is_string( $data ) ) {
99 return $data;
100 }
101
102 $decoded = base64_decode( $data );
103 if ( false === $decoded ) {
104 return strtolower( $type ) === 'string' ? '' : [];
105 }
106
107 if ( strpos( $decoded, '::' ) !== false ) {
108 [ $encrypted_data, $iv ] = explode( '::', $decoded, 2 );
109 } else {
110 // Deprecated method, for backwards compatibility (license decryption)
111 $ivlength = openssl_cipher_iv_length( 'aes-256-cbc' );
112 $iv = substr( $decoded, 0, $ivlength );
113 $encrypted_data = substr( $decoded, $ivlength );
114 }
115
116 if ( function_exists( 'openssl_decrypt' ) ) {
117 $decrypted_data = openssl_decrypt( $encrypted_data, 'aes-256-cbc', $key, 0, $iv );
118 } else {
119 $this->log( 'The function openssl_decrypt does not exist. Check with your host if the OpenSSL library for PHP can be enabled.' );
120
121 return strtolower( $type ) === 'string' ? '' : [];
122 }
123
124 if ( 'array' === strtolower( $type ) ) {
125 $unserialized_data = @unserialize( $decrypted_data );
126
127 return ( is_array( $unserialized_data ) ) ? $unserialized_data : [];
128 }
129
130 return $decrypted_data;
131 }
132
133 private function get_encryption_key(): string {
134 // First, check if we have a key defined as a constant
135 if ( defined( 'RSSSL_KEY' ) ) {
136 return RSSSL_KEY;
137 }
138
139 // If not, check if we have a key stored in the database
140 return get_site_option( 'rsssl_main_key' );
141
142 }
143 }
144