CacheBase.php
3 years ago
Chain.php
3 years ago
Crypto.php
3 years ago
File.php
3 years ago
PluginSilentUpgrader.php
3 years ago
PluginSilentUpgraderSkin.php
3 years ago
Templates.php
3 years ago
Transient.php
3 years ago
Crypto.php
131 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPForms\Helpers; |
| 4 | |
| 5 | /** |
| 6 | * Class for encryption functionality. |
| 7 | * |
| 8 | * @since 1.6.1.2 |
| 9 | * |
| 10 | * @link https://www.php.net/manual/en/intro.sodium.php |
| 11 | */ |
| 12 | class Crypto { |
| 13 | |
| 14 | /** |
| 15 | * Get a secret key for encrypt/decrypt. |
| 16 | * |
| 17 | * @since 1.6.1.2 |
| 18 | * |
| 19 | * @return string |
| 20 | */ |
| 21 | public static function get_secret_key() { |
| 22 | |
| 23 | $secret_key = get_option( 'wpforms_crypto_secret_key' ); |
| 24 | |
| 25 | // If we already have the secret, send it back. |
| 26 | if ( false !== $secret_key ) { |
| 27 | return base64_decode( $secret_key ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
| 28 | } |
| 29 | |
| 30 | // We don't have a secret, so let's generate one. |
| 31 | $secret_key = sodium_crypto_secretbox_keygen(); |
| 32 | add_option( 'wpforms_crypto_secret_key', base64_encode( $secret_key ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
| 33 | |
| 34 | return $secret_key; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Encrypt a message. |
| 39 | * |
| 40 | * @since 1.6.1.2 |
| 41 | * |
| 42 | * @param string $message Message to encrypt. |
| 43 | * @param string $key Encryption key. |
| 44 | * |
| 45 | * @return string |
| 46 | */ |
| 47 | public static function encrypt( $message, $key = '' ) { |
| 48 | |
| 49 | // Create a nonce for this operation. It will be stored and recovered in the message itself. |
| 50 | $nonce = random_bytes( |
| 51 | SODIUM_CRYPTO_SECRETBOX_NONCEBYTES |
| 52 | ); |
| 53 | |
| 54 | if ( empty( $key ) ) { |
| 55 | $key = self::get_secret_key(); |
| 56 | } |
| 57 | |
| 58 | // Encrypt message and combine with nonce. |
| 59 | $cipher = base64_encode( // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
| 60 | $nonce . |
| 61 | sodium_crypto_secretbox( |
| 62 | $message, |
| 63 | $nonce, |
| 64 | $key |
| 65 | ) |
| 66 | ); |
| 67 | |
| 68 | try { |
| 69 | sodium_memzero( $message ); |
| 70 | sodium_memzero( $key ); |
| 71 | } catch ( \Exception $e ) { |
| 72 | return $cipher; |
| 73 | } |
| 74 | |
| 75 | return $cipher; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Decrypt a message. |
| 80 | * |
| 81 | * @since 1.6.1.2 |
| 82 | * |
| 83 | * @param string $encrypted Encrypted message. |
| 84 | * @param string $key Encryption key. |
| 85 | * |
| 86 | * @return string |
| 87 | */ |
| 88 | public static function decrypt( $encrypted, $key = '' ) { |
| 89 | |
| 90 | // Unpack base64 message. |
| 91 | $decoded = base64_decode( $encrypted ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
| 92 | |
| 93 | if ( false === $decoded ) { |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | if ( mb_strlen( $decoded, '8bit' ) < ( SODIUM_CRYPTO_SECRETBOX_NONCEBYTES + SODIUM_CRYPTO_SECRETBOX_MACBYTES ) ) { |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | // Pull nonce and ciphertext out of unpacked message. |
| 102 | $nonce = mb_substr( $decoded, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, '8bit' ); |
| 103 | $ciphertext = mb_substr( $decoded, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, null, '8bit' ); |
| 104 | |
| 105 | if ( empty( $key ) ) { |
| 106 | $key = self::get_secret_key(); |
| 107 | } |
| 108 | |
| 109 | // Decrypt it. |
| 110 | $message = sodium_crypto_secretbox_open( |
| 111 | $ciphertext, |
| 112 | $nonce, |
| 113 | $key |
| 114 | ); |
| 115 | |
| 116 | // Check for decrpytion failures. |
| 117 | if ( false === $message ) { |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | try { |
| 122 | sodium_memzero( $ciphertext ); |
| 123 | sodium_memzero( $key ); |
| 124 | } catch ( \Exception $e ) { |
| 125 | return $message; |
| 126 | } |
| 127 | |
| 128 | return $message; |
| 129 | } |
| 130 | } |
| 131 |