PluginProbe ʕ •ᴥ•ʔ
OttoKit: All-in-One Automation Platform / 1.1.24
OttoKit: All-in-One Automation Platform v1.1.24
1.1.31 1.1.30 1.1.29 1.1.28 1.1.27 1.1.9 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 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 1.0.39 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 1.0.47 1.0.48 1.0.49 1.0.50 1.0.51 1.0.52 1.0.53 1.0.54 1.0.55 1.0.56 1.0.57 1.0.58 1.0.59 1.0.60 1.0.61 1.0.62 1.0.63 1.0.64 1.0.65 1.0.66 1.0.67 1.0.68 1.0.69 1.0.7 1.0.70 1.0.71 1.0.72 1.0.73 1.0.74 1.0.75 1.0.76 1.0.77 1.0.78 1.0.79 1.0.8 1.0.80 1.0.81 1.0.82 1.0.83 1.0.84 1.0.85 1.0.86 1.0.87 1.0.88 1.0.89 1.0.9 1.0.90 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.21 1.1.22 1.1.23 1.1.24 1.1.25 1.1.26 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8
suretriggers / src / Support / Encryption.php
suretriggers / src / Support Last commit date
Encryption.php 1 year ago
Encryption.php
163 lines
1 <?php
2 /**
3 * Based on the code from the following packages:
4 * Class Google\Site_Kit\Core\Storage\Data_Encryption
5 *
6 * @package Google\Site_Kit
7 * @copyright 2019 Google LLC
8 * @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
9 * @link https://sitekit.withgoogle.com
10 */
11
12 namespace SureTriggers\Support;
13
14 /**
15 * Class responsible for encrypting and decrypting data.
16 *
17 * @since 1.0.0
18 * @access private
19 * @ignore
20 */
21 class Encryption {
22 /**
23 * Key to use for encryption.
24 *
25 * @since 1.0.0
26 * @var string
27 */
28 private $key;
29
30 /**
31 * Salt to use for encryption.
32 *
33 * @since 1.0.0
34 * @var string
35 */
36 private $salt;
37
38 /**
39 * Constructor.
40 *
41 * @since 1.0.0
42 */
43 final public function __construct() {
44 $this->key = $this->get_default_key();
45 $this->salt = $this->get_default_salt();
46 }
47
48 /**
49 * Encrypts a value.
50 *
51 * If a user-based key is set, that key is used. Otherwise the default key is used.
52 *
53 * @since 1.0.0
54 *
55 * @param string $value Value to encrypt.
56 * @return string|bool Encrypted value, or false on failure.
57 */
58 protected function encrypt( $value ) {
59 if ( ! extension_loaded( 'openssl' ) ) {
60 return $value;
61 }
62 $method = 'aes-256-ctr';
63 $ivlen = openssl_cipher_iv_length( $method );
64 $iv = openssl_random_pseudo_bytes( $ivlen );
65
66 $raw_value = openssl_encrypt( $value . $this->salt, $method, $this->key, 0, $iv );
67 if ( ! $raw_value ) {
68 return false;
69 }
70
71 return base64_encode( $iv . $raw_value ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
72 }
73
74 /**
75 * Decrypts a value.
76 *
77 * If a user-based key is set, that key is used. Otherwise the default key is used.
78 *
79 * @since 1.0.0
80 *
81 * @param string $raw_value Value to decrypt.
82 * @return string|bool Decrypted value, or false on failure.
83 */
84 protected function decrypt( $raw_value ) {
85 if ( ! extension_loaded( 'openssl' ) ) {
86 return $raw_value;
87 }
88
89 $raw_value = base64_decode( $raw_value, true ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
90
91 $method = 'aes-256-ctr';
92 $ivlen = openssl_cipher_iv_length( $method );
93 $iv = substr( $raw_value, 0, $ivlen );
94
95 $raw_value = substr( $raw_value, $ivlen );
96
97 $value = openssl_decrypt( $raw_value, $method, $this->key, 0, $iv );
98 if ( ! $value || substr( $value, - strlen( $this->salt ) ) !== $this->salt ) {
99 return false;
100 }
101 return substr( $value, 0, - strlen( $this->salt ) );
102 }
103
104 /**
105 * Gets the default encryption key to use.
106 *
107 * @since 1.0.0
108 *
109 * @return string Default (not user-based) encryption key.
110 */
111 protected function get_default_key() {
112
113 if ( defined( 'SURETRIGGERS_ENCRYPTION_KEY' ) && '' !== SURETRIGGERS_ENCRYPTION_KEY ) {
114 return SURETRIGGERS_ENCRYPTION_KEY;
115 }
116
117 if ( defined( 'LOGGED_IN_KEY' ) && '' !== LOGGED_IN_KEY ) {
118 return LOGGED_IN_KEY;
119 }
120
121 // If this is reached, you're either not on a live site or have a serious security issue.
122 return 'this-is-fallback-key-for-encryption';
123 }
124
125 /**
126 * Gets the default encryption salt to use.
127 *
128 * @since 1.0.0
129 *
130 * @return string Encryption salt.
131 */
132 private function get_default_salt() {
133 if ( defined( 'SURETRIGGERS_ENCRYPTION_SALT' ) && '' !== SURETRIGGERS_ENCRYPTION_SALT ) {
134 return SURETRIGGERS_ENCRYPTION_SALT;
135 }
136
137 if ( defined( 'LOGGED_IN_SALT' ) && '' !== LOGGED_IN_SALT ) {
138 return LOGGED_IN_SALT;
139 }
140
141 // If this is reached, you're either not on a live site or have a serious security issue.
142 return 'this-is-fallback-salt-for-encryption';
143 }
144
145 /**
146 * Static Facade Accessor
147 *
148 * @param string $method Method to call.
149 * @param mixed $params Method params.
150 *
151 * @return mixed
152 */
153 public static function __callStatic( $method, $params ) {
154 /**
155 *
156 * Ignore line
157 *
158 * @phpstan-ignore-next-line
159 */
160 return call_user_func_array( [ new static(), $method ], $params );
161 }
162 }
163