-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.php
117 lines (96 loc) · 3.49 KB
/
crypto.php
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
<?php
/**
* Encryption functions to encrypt and decrypt values.
*
* @package WPX
*
* @since 1.0.0
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! function_exists( 'wpx_encrypt' ) ) {
/**
* Encrypt an object using the AUTH_SALT from wp-config.php as the encryption key.
*
* @since 1.0.0
*
* @param mixed $value The value to encrypt.
*
* @return string The encrypted base64-encoded string.
*/
function wpx_encrypt( $value ) {
// Check to make sure encryption (php-mcrypt) is enabled on the server
if ( ! function_exists( 'mcrypt_get_iv_size' ) ) {
// Set the error message
$error_message = _x( 'Encryption requires the php-mcrypt module to encrypt the password. Please install php-mcrypt, restart your server, and reload the page.', 'Crypto', 'wpx' );
// Write to error log
error_log( $error_message );
// Return the original value
return $value;
}
// Set the encryption values
$iv_size = mcrypt_get_iv_size( MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB );
$iv = mcrypt_create_iv( $iv_size, MCRYPT_RAND );
$h_key = hash( 'sha256', AUTH_SALT, TRUE );
// Get the encrypted result
$encrypted = mcrypt_encrypt( MCRYPT_RIJNDAEL_256, $h_key, $value, MCRYPT_MODE_ECB, $iv );
// Get the base 64 encoded string
$base_64 = base64_encode( $encrypted );
// Return the base 64 encoded string
return $base_64;
}
} // wpx_encrypt
/**
* Filters a value through encryption and returns an encrypted string.
*
* @since 1.0.0
*
* @param mixed $value The value to encrypt.
*
* @return string The encrypted base64-encoded string.
*/
add_filter( 'wpx_encrypt', 'wpx_encrypt' );
if ( ! function_exists( 'wpx_decrypt' ) ) {
/**
* Decrypt a string using the AUTH_SALT from wp-config.php as the decryption key.
*
* @since 1.0.0
*
* @param string $string The encrypted string to decrypt.
*
* @return string The decrypted plain-text string.
*/
function wpx_decrypt( $string ) {
// Check to make sure encryption (php-mcrypt) is enabled on the server
if ( ! function_exists( 'mcrypt_get_iv_size' ) ) {
// Set the error message
$error_message = _x( 'Decryption requires the php-mcrypt module to encrypt the password. Please install php-mcrypt, restart your server, and reload the page.', 'Crypto', 'wpx' );
// Write to error log
error_log( $error_message );
// Return the original string
return $string;
}
// Set the decryption values
$iv_size = mcrypt_get_iv_size( MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB );
$iv = mcrypt_create_iv( $iv_size, MCRYPT_RAND );
$h_key = hash( 'sha256', AUTH_SALT, TRUE );
// Decode the string to descrypt
$decoded = base64_decode( $string );
// Decrypt the decoded string
$decrypted = mcrypt_decrypt( MCRYPT_RIJNDAEL_256, $h_key, $decoded, MCRYPT_MODE_ECB, $iv );
// Trim and return the decrypted string
return trim( $decrypted );
}
} // wpx_decrypt
/**
* Filters a value through decryption and returns the decrypted string.
*
* @since 1.0.0
*
* @param string $string The base64-encoded encrypted string to decrypt.
*
* @return string The decrypted plain-text string.
*/
add_filter( 'wpx_decrypt', 'wpx_decrypt' );