Repository navigation
Expand file tree
/
Copy pathEncryption.php
More file actions
136 lines (115 loc) · 3.19 KB
/
Copy pathEncryption.php
File metadata and controls
136 lines (115 loc) · 3.19 KB
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
<?php
/**
* Encryption class.
*
* @see https://felix-arntz.me/blog/storing-confidential-data-in-wordpress/
* @package PoweredCache
*/
namespace PoweredCache;
/**
* Class Encryption.
* Encrypting and decrypting data.
*/
final class Encryption {
/**
* Key to use for encryption.
*
* @since 3.4
* @var string
*/
private $key;
/**
* Salt to use for encryption.
*
* @since 3.4
* @var string
*/
private $salt;
/**
* Constructor.
*
* @since 3.4
*/
public function __construct() {
$this->key = $this->get_default_key();
$this->salt = $this->get_default_salt();
}
/**
* Encrypts a value.
* If a user-based key is set, that key is used. Otherwise the default key is used.
*
* @param string $value Value to encrypt.
*
* @return string|bool Encrypted value, or false on failure.
* @since 3.4
*/
public function encrypt( $value ) {
if ( ! extension_loaded( 'openssl' ) ) {
return $value;
}
$method = 'aes-256-ctr';
$ivlen = openssl_cipher_iv_length( $method );
$iv = openssl_random_pseudo_bytes( $ivlen );
$raw_value = openssl_encrypt( $value . $this->salt, $method, $this->key, 0, $iv );
if ( ! $raw_value ) {
return false;
}
return base64_encode( $iv . $raw_value ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
}
/**
* Decrypts a value.
* If a user-based key is set, that key is used. Otherwise the default key is used.
*
* @param string $raw_value Value to decrypt.
*
* @return string|bool Decrypted value, or false on failure.
* @since 3.4
*/
public function decrypt( $raw_value ) {
if ( ! extension_loaded( 'openssl' ) ) {
return $raw_value;
}
$raw_value = base64_decode( $raw_value, true ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
$method = 'aes-256-ctr';
$ivlen = openssl_cipher_iv_length( $method );
$iv = substr( $raw_value, 0, $ivlen );
$raw_value = substr( $raw_value, $ivlen );
$value = openssl_decrypt( $raw_value, $method, $this->key, 0, $iv );
if ( ! $value || substr( $value, - strlen( $this->salt ) ) !== $this->salt ) {
return false;
}
return substr( $value, 0, - strlen( $this->salt ) );
}
/**
* Gets the default encryption key
*
* @return string Default (not user-based) encryption key.
* @since 3.4
*/
private function get_default_key() {
if ( defined( 'POWERED_CACHE_ENCRYPTION_KEY' ) && '' !== POWERED_CACHE_ENCRYPTION_KEY ) {
return POWERED_CACHE_ENCRYPTION_KEY;
}
if ( defined( 'LOGGED_IN_KEY' ) && '' !== LOGGED_IN_KEY ) {
return LOGGED_IN_KEY;
}
// If this is reached, you're either not on a live site or have a serious security issue.
return 'this-is-not-a-secret-key';
}
/**
* Gets the encryption salt
*
* @return string Encryption salt.
* @since 3.4
*/
private function get_default_salt() {
if ( defined( 'POWERED_CACHE_ENCRYPTION_SALT' ) && '' !== POWERED_CACHE_ENCRYPTION_SALT ) {
return POWERED_CACHE_ENCRYPTION_SALT;
}
if ( defined( 'LOGGED_IN_SALT' ) && '' !== LOGGED_IN_SALT ) {
return LOGGED_IN_SALT;
}
// If this is reached, you're either not on a live site or have a serious security issue.
return 'this-is-not-a-secret-salt';
}
}