| @@ -1,90 +1,90 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace Getwid; | |
| 4 | - | |
| 5 | -final class StringEncryption { | |
| 6 | - | |
| 7 | - private $cipher = 'aes-256-ctr'; | |
| 8 | - private $salt; | |
| 9 | - private $passphrase; | |
| 10 | - | |
| 11 | - public function __construct() { | |
| 12 | - $this->salt = $this->get_salt(); | |
| 13 | - $this->passphrase = $this->get_passphrase(); | |
| 14 | - } | |
| 15 | - | |
| 16 | - public function encrypt( $string_to_encrypt ) { | |
| 17 | - | |
| 18 | - if ( ! $this->can_encrypt() ) { | |
| 19 | - return $string_to_encrypt; | |
| 20 | - } | |
| 21 | - | |
| 22 | - $ivlen = openssl_cipher_iv_length( $this->cipher ); | |
| 23 | - $iv = openssl_random_pseudo_bytes( $ivlen ); | |
| 24 | - | |
| 25 | - $encrypted_string = openssl_encrypt( | |
| 26 | - $string_to_encrypt . $this->salt, | |
| 27 | - $this->cipher, | |
| 28 | - $this->passphrase, | |
| 29 | - 0, | |
| 30 | - $iv | |
| 31 | - ); | |
| 32 | - | |
| 33 | - return base64_encode( $iv . $encrypted_string ); | |
| 34 | - } | |
| 35 | - | |
| 36 | - public function decrypt( $string_to_decrypt ) { | |
| 37 | - | |
| 38 | - if ( ! $this->can_encrypt() ) { | |
| 39 | - return $string_to_decrypt; | |
| 40 | - } | |
| 41 | - | |
| 42 | - $encrypted_string = base64_decode( $string_to_decrypt, true ); | |
| 43 | - | |
| 44 | - $ivlen = openssl_cipher_iv_length( $this->cipher ); | |
| 45 | - $iv = substr( $encrypted_string, 0, $ivlen ); | |
| 46 | - | |
| 47 | - $encrypted_string = substr( $encrypted_string, $ivlen ); | |
| 48 | - | |
| 49 | - $decrypted_string = openssl_decrypt( | |
| 50 | - $encrypted_string, | |
| 51 | - $this->cipher, | |
| 52 | - $this->passphrase, | |
| 53 | - 0, | |
| 54 | - $iv | |
| 55 | - ); | |
| 56 | - | |
| 57 | - if ( ! $decrypted_string || substr( $decrypted_string, - strlen( $this->salt ) ) !== $this->salt ) { | |
| 58 | - return $string_to_decrypt; | |
| 59 | - } | |
| 60 | - | |
| 61 | - return substr( $decrypted_string, 0, - strlen( $this->salt ) ); | |
| 62 | - } | |
| 63 | - | |
| 64 | - private function can_encrypt() { | |
| 65 | - | |
| 66 | - if ( ! function_exists( 'openssl_encrypt' ) ) { | |
| 67 | - return false; | |
| 68 | - } | |
| 69 | - | |
| 70 | - if ( ! in_array( $this->cipher, openssl_get_cipher_methods() ) ) { | |
| 71 | - return false; | |
| 72 | - } | |
| 73 | - | |
| 74 | - return true; | |
| 75 | - } | |
| 76 | - | |
| 77 | - private function get_passphrase() { | |
| 78 | - | |
| 79 | - if ( function_exists('wp_salt') && wp_salt() ) { | |
| 80 | - return wp_salt(); | |
| 81 | - } | |
| 82 | - | |
| 83 | - return 'getwid_non_secret_passphrase'; | |
| 84 | - } | |
| 85 | - | |
| 86 | - private function get_salt() { | |
| 87 | - return 'getwid_non_secret_salt'; | |
| 88 | - } | |
| 89 | - | |
| 90 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace Getwid; | |
| 4 | + | |
| 5 | +final class StringEncryption { | |
| 6 | + | |
| 7 | + private $cipher = 'aes-256-ctr'; | |
| 8 | + private $salt; | |
| 9 | + private $passphrase; | |
| 10 | + | |
| 11 | + public function __construct() { | |
| 12 | + $this->salt = $this->get_salt(); | |
| 13 | + $this->passphrase = $this->get_passphrase(); | |
| 14 | + } | |
| 15 | + | |
| 16 | + public function encrypt( $string_to_encrypt ) { | |
| 17 | + | |
| 18 | + if ( ! $this->can_encrypt() ) { | |
| 19 | + return $string_to_encrypt; | |
| 20 | + } | |
| 21 | + | |
| 22 | + $ivlen = openssl_cipher_iv_length( $this->cipher ); | |
| 23 | + $iv = openssl_random_pseudo_bytes( $ivlen ); | |
| 24 | + | |
| 25 | + $encrypted_string = openssl_encrypt( | |
| 26 | + $string_to_encrypt . $this->salt, | |
| 27 | + $this->cipher, | |
| 28 | + $this->passphrase, | |
| 29 | + 0, | |
| 30 | + $iv | |
| 31 | + ); | |
| 32 | + | |
| 33 | + return base64_encode( $iv . $encrypted_string ); | |
| 34 | + } | |
| 35 | + | |
| 36 | + public function decrypt( $string_to_decrypt ) { | |
| 37 | + | |
| 38 | + if ( ! $this->can_encrypt() ) { | |
| 39 | + return $string_to_decrypt; | |
| 40 | + } | |
| 41 | + | |
| 42 | + $encrypted_string = base64_decode( $string_to_decrypt, true ); | |
| 43 | + | |
| 44 | + $ivlen = openssl_cipher_iv_length( $this->cipher ); | |
| 45 | + $iv = substr( $encrypted_string, 0, $ivlen ); | |
| 46 | + | |
| 47 | + $encrypted_string = substr( $encrypted_string, $ivlen ); | |
| 48 | + | |
| 49 | + $decrypted_string = openssl_decrypt( | |
| 50 | + $encrypted_string, | |
| 51 | + $this->cipher, | |
| 52 | + $this->passphrase, | |
| 53 | + 0, | |
| 54 | + $iv | |
| 55 | + ); | |
| 56 | + | |
| 57 | + if ( ! $decrypted_string || substr( $decrypted_string, - strlen( $this->salt ) ) !== $this->salt ) { | |
| 58 | + return $string_to_decrypt; | |
| 59 | + } | |
| 60 | + | |
| 61 | + return substr( $decrypted_string, 0, - strlen( $this->salt ) ); | |
| 62 | + } | |
| 63 | + | |
| 64 | + private function can_encrypt() { | |
| 65 | + | |
| 66 | + if ( ! function_exists( 'openssl_encrypt' ) ) { | |
| 67 | + return false; | |
| 68 | + } | |
| 69 | + | |
| 70 | + if ( ! in_array( $this->cipher, openssl_get_cipher_methods() ) ) { | |
| 71 | + return false; | |
| 72 | + } | |
| 73 | + | |
| 74 | + return true; | |
| 75 | + } | |
| 76 | + | |
| 77 | + private function get_passphrase() { | |
| 78 | + | |
| 79 | + if ( function_exists('wp_salt') && wp_salt() ) { | |
| 80 | + return wp_salt(); | |
| 81 | + } | |
| 82 | + | |
| 83 | + return 'getwid_non_secret_passphrase'; | |
| 84 | + } | |
| 85 | + | |
| 86 | + private function get_salt() { | |
| 87 | + return 'getwid_non_secret_salt'; | |
| 88 | + } | |
| 89 | + | |
| 90 | +} | |