@@ -16,24 +16,23 @@ class Encryption {
16
16
*
17
17
* @param string $content Data to encrypt.
18
18
* @param string $password Password used to encrypt data.
19
- * @return array|false Returns encoded and encrypted file content.
20
- * @throws Exception
19
+ * @return array Returns encoded and encrypted file content.
20
+ * @throws Exception Throws {@see Exception} if encryption failed.
21
21
* @global array $conf Configuration variables.
22
- * @since 2.3 Added support for AEAD cipher modes.
23
22
* @since 2.0
23
+ * @since 2.3 Added support for AEAD cipher modes.
24
24
*/
25
- public static function encryptFileContent (string $ content , string $ password ) {
25
+ public static function encryptFileContent (string $ content , string $ password ): ? array {
26
26
global $ conf ;
27
27
$ cipher = $ conf ['Encryption-Method ' ];
28
- $ iv = self ::getIV ($ cipher );
29
-
28
+ $ iv = self ::createIV ($ cipher );
30
29
$ data = base64_encode (openssl_encrypt ($ content , $ cipher , $ password , OPENSSL_RAW_DATA , $ iv , $ tag ));
31
30
32
31
// Test if encrypted data is able to be decrypted
33
32
if (Encryption::decrypt (base64_decode ($ data ), $ password , bin2hex ($ iv ), bin2hex ($ tag ), OPENSSL_RAW_DATA ) != FALSE )
34
33
return ['data ' => $ data , 'iv ' => bin2hex ($ iv ), 'tag ' => bin2hex ($ tag )];
35
- else error_log ( " Content encryption failed. " );
36
- return FALSE ;
34
+ if ( is_bool ( $ data )) throw new Exception ( openssl_error_string () );
35
+ return NULL ;
37
36
}
38
37
39
38
/**
@@ -42,11 +41,12 @@ public static function encryptFileContent(string $content, string $password) {
42
41
*
43
42
* @param string $cipher Encryption method to use.
44
43
* @return string Returns an IV string encoded with base64.
45
- * @throws Exception
46
- * @since 2.3 Added support for variable IV length.
44
+ * @throws Exception Throws {@see Exception} if unable to create IV
47
45
* @since 2.0
46
+ * @since 2.3 Added support for variable IV length.
47
+ * @since 3.0 Rename getIV with createIV
48
48
*/
49
- public static function getIV (string $ cipher ) {
49
+ public static function createIV (string $ cipher ): string {
50
50
$ ivLength = openssl_cipher_iv_length ($ cipher );
51
51
return random_bytes ($ ivLength );
52
52
}
@@ -59,19 +59,18 @@ public static function getIV(string $cipher) {
59
59
* @param string $iv IV for decryption.
60
60
* @param string|null $tag AEAD tag from the data encryption.
61
61
* @param array|null $options OPENSSL options.
62
- * @return string|bool Returns decrypted data or FALSE on failure .
63
- * @throws Exception
62
+ * @return string Returns decrypted data.
63
+ * @throws Exception Throws {@see Exception} if decryption failed.
64
64
* @global array $conf Configuration variables.
65
65
* @since 2.0
66
66
* @since 2.3 Added support for AEAD cipher modes.
67
67
* @since 2.4 Added ability to specify OPENSSL options.
68
68
* @global array $conf Configuration variables.
69
69
*/
70
- public static function decrypt (string $ input , string $ password , string $ iv , string $ tag = NULL , $ options = NULL ) {
70
+ public static function decrypt (string $ input , string $ password , string $ iv , string $ tag = NULL , $ options = NULL ): string {
71
71
global $ conf ;
72
72
$ data = openssl_decrypt ($ input , $ conf ['Encryption-Method ' ], $ password , $ options , hex2bin ($ iv ), hex2bin ($ tag ));
73
- if (is_bool ($ data )) throw new \Exception (openssl_error_string ());
74
-
73
+ if (is_bool ($ data )) throw new Exception (openssl_error_string ());
75
74
return $ data ;
76
75
}
77
76
@@ -81,34 +80,30 @@ public static function decrypt(string $input, string $password, string $iv, stri
81
80
* @param array $metadata the $_FILES[] array to use.
82
81
* @param string $deletionPassword Deletion password to encrypt along with the metadata.
83
82
* @param string $password Password used to encrypt the data.
84
- * @return array|false
85
- * @throws Exception
83
+ * @return array Returns array of [0 => encrypted string, 1 => encryption IV, 2 => encryption tag]
84
+ * @throws Exception Throws {@see Exception} if encryption failed.
86
85
* @since 2.0
87
86
* @since 2.2 Added $deletionPassword to the array of things to encrypt.
88
87
* @since 2.3 Added support for AEAD cipher modes.
89
88
* @global array $conf Configuration variables.
90
89
*/
91
- public static function encryptFileDetails (array $ metadata , string $ deletionPassword , string $ password ) {
90
+ public static function encryptFileDetails (array $ metadata , string $ deletionPassword , string $ password ): array {
92
91
global $ conf ;
93
92
$ cipher = $ conf ['Encryption-Method ' ];
94
- $ iv = self ::getIV ($ cipher );
93
+ $ iv = self ::createIV ($ cipher );
95
94
96
- $ data_array = [
95
+ $ data = [
97
96
base64_encode ($ metadata ['name ' ]),
98
97
base64_encode ($ metadata ['size ' ]),
99
98
base64_encode ($ metadata ['type ' ]),
100
99
base64_encode ($ deletionPassword ),
101
100
];
102
101
103
- $ data_string = implode (" " , $ data_array );
104
-
105
- $ data_enc = base64_encode (openssl_encrypt ($ data_string , $ cipher , $ password , OPENSSL_RAW_DATA , $ iv , $ tag ));
102
+ $ encrypted_string = base64_encode (openssl_encrypt (implode (' ' , $ data ), $ cipher , $ password , OPENSSL_RAW_DATA , $ iv , $ tag ));
106
103
107
104
// Test if encrypted data is able to be decrypted
108
- if (Encryption::decrypt (base64_decode ($ data_enc ), $ password , bin2hex ($ iv ), bin2hex ($ tag ), OPENSSL_RAW_DATA ) != FALSE )
109
- return ['data ' => $ data_enc , 'iv ' => bin2hex ($ iv ), 'tag ' => bin2hex ($ tag )];
110
-
111
- error_log ("Metadata encryption failed. " );
112
- return FALSE ;
105
+ if (Encryption::decrypt (base64_decode ($ encrypted_string ), $ password , bin2hex ($ iv ), bin2hex ($ tag ), OPENSSL_RAW_DATA ) != FALSE )
106
+ return ['data ' => $ encrypted_string , 'iv ' => bin2hex ($ iv ), 'tag ' => bin2hex ($ tag )];
107
+ throw new Exception (openssl_error_string ());
113
108
}
114
109
}
0 commit comments