Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Encryption
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 5
210
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 get_key
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 get_salt
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 encrypt
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 decrypt
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace NewfoldLabs\WP\Module\Data\Helpers;
4
5/**
6 * Encrypt/Decrypt strings for database storage
7 */
8class Encryption {
9
10    /**
11     * Key to use for encrypting/decrypting
12     *
13     * @since 1.0
14     * @var string
15     */
16    private $key;
17
18    /**
19     * Salt to use prior to encryption
20     *
21     * @since 1.0
22     * @var string
23     */
24    private $salt;
25
26    /**
27     * Method to be used for encrypting
28     *
29     * @since 1.0
30     * @var string
31     */
32    private $method;
33
34    /**
35     * The initialization vector length based on the method
36     *
37     * @since 1.0
38     * @var string
39     */
40    private $ivlength;
41
42    /**
43     * Construct
44     *
45     * @since 1.0
46     */
47    public function __construct() {
48        $this->key      = $this->get_key();
49        $this->salt     = $this->get_salt();
50        $this->method   = 'aes-256-ctr';
51        $this->ivlength = openssl_cipher_iv_length( $this->method );
52    }
53
54    /**
55     * Returns the encryption key to use
56     *
57     * @since 1.0
58     *
59     * @return string The encryption key
60     */
61    private function get_key() {
62        if ( defined( 'LOGGED_IN_KEY' ) && '' !== LOGGED_IN_KEY ) {
63            return LOGGED_IN_KEY;
64        }
65
66        // If a site does not have a LOGGED_IN_KEY set, then they already have more
67        // pressing security issues than exposure of the data connection token.
68        // We'll set a basic fallback just in case.
69        return 'DB6F4B11FF31F37F3C53D6FDD13A12F06D2802DA';
70    }
71
72    /**
73     * Returns the salt to use before encryption
74     *
75     * @since 1.0
76     *
77     * @return string The salt
78     */
79    private function get_salt() {
80        if ( defined( 'LOGGED_IN_SALT' ) && '' !== LOGGED_IN_SALT ) {
81            return LOGGED_IN_SALT;
82        }
83
84        // If a site does not have a LOGGED_IN_SALT set, then they already have more
85        // pressing security issues than exposure of the data connection token.
86        // We'll set a basic fallback just in case.
87        return 'EE3440AA014D3E0627127D844EFAE7946B51BFBB';
88    }
89
90
91    /**
92     * Encrypts a value
93     *
94     * @since 1.0
95     *
96     * @param string $value The string to be encrypted
97     *
98     * @return string|False The encrypted value, or false on failure
99     */
100    public function encrypt( $value ) {
101
102        // If we don't have openssl for some reason, we'll just bail and return the value
103        if ( ! extension_loaded( 'openssl' ) ) {
104            return $value;
105        }
106
107        $salty_value = $value . $this->salt;
108        $iv          = openssl_random_pseudo_bytes( $this->ivlength );
109
110        $cipher = openssl_encrypt( $salty_value, $this->method, $this->key, 0, $iv );
111
112        // If encryption failed
113        if ( ! $cipher ) {
114            return false;
115        }
116
117        return base64_encode( $iv . $cipher );
118    }
119
120    /**
121     * Decrypts a value
122     *
123     * @since 1.0
124     *
125     * @param string $cipher The value to be decrypted
126     *
127     * @return string|False The decrypted value or false on failure
128     */
129    public function decrypt( $cipher ) {
130
131        // If we don't have openssl for some reason, we'll just bail and return the value
132        if ( ! extension_loaded( 'openssl' ) ) {
133            return $cipher;
134        }
135
136        $cipher = base64_decode( $cipher, true );
137
138        // Grab the IV from the front of the passed encrypted string
139        $iv = substr( $cipher, 0, $this->ivlength );
140
141        // Get the encrypted value from the second half
142        $value = substr( $cipher, $this->ivlength );
143
144        // Decrypt!
145        $decrypted_value = openssl_decrypt( $value, $this->method, $this->key, 0, $iv );
146
147        // Decription failed, or the salt doesn't match the end of the decrypted string
148        if ( ! $value || substr( $decrypted_value, - strlen( $this->salt ) ) !== $this->salt ) {
149            return false;
150        }
151
152        // Remove the salt from the end and return the decypted value
153        return substr( $decrypted_value, 0, - strlen( $this->salt ) );
154    }
155}