Skip to content
This repository was archived by the owner on Mar 29, 2024. It is now read-only.

Commit f25e291

Browse files
committed
Documentation and fix function output def
1 parent d37be32 commit f25e291

File tree

6 files changed

+32
-39
lines changed

6 files changed

+32
-39
lines changed

src/com/carlgo11/tempfiles/EncryptedFile.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
<?php
22

3-
43
namespace com\carlgo11\tempfiles;
54

6-
75
use Exception;
86

97
class EncryptedFile {
108

119
protected string $_blob;
12-
protected string $_iv;
13-
protected string $_tag;
10+
protected array $_iv;
11+
protected array $_tag;
1412
protected string $_metadata;
1513
protected string $_id;
1614

@@ -57,11 +55,11 @@ public function getEncryptedFileContent(): string {
5755
return $this->_blob;
5856
}
5957

60-
public function getIV(): string {
58+
public function getIV(): array {
6159
return $this->_iv;
6260
}
6361

64-
public function getTag(): string {
62+
public function getTag(): array {
6563
return $this->_tag;
6664
}
6765
}

src/com/carlgo11/tempfiles/Encryption.php

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,23 @@ class Encryption {
1616
*
1717
* @param string $content Data to encrypt.
1818
* @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.
2121
* @global array $conf Configuration variables.
22-
* @since 2.3 Added support for AEAD cipher modes.
2322
* @since 2.0
23+
* @since 2.3 Added support for AEAD cipher modes.
2424
*/
25-
public static function encryptFileContent(string $content, string $password) {
25+
public static function encryptFileContent(string $content, string $password): ?array {
2626
global $conf;
2727
$cipher = $conf['Encryption-Method'];
28-
$iv = self::getIV($cipher);
29-
28+
$iv = self::createIV($cipher);
3029
$data = base64_encode(openssl_encrypt($content, $cipher, $password, OPENSSL_RAW_DATA, $iv, $tag));
3130

3231
// Test if encrypted data is able to be decrypted
3332
if (Encryption::decrypt(base64_decode($data), $password, bin2hex($iv), bin2hex($tag), OPENSSL_RAW_DATA) != FALSE)
3433
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;
3736
}
3837

3938
/**
@@ -42,11 +41,12 @@ public static function encryptFileContent(string $content, string $password) {
4241
*
4342
* @param string $cipher Encryption method to use.
4443
* @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
4745
* @since 2.0
46+
* @since 2.3 Added support for variable IV length.
47+
* @since 3.0 Rename getIV with createIV
4848
*/
49-
public static function getIV(string $cipher) {
49+
public static function createIV(string $cipher): string {
5050
$ivLength = openssl_cipher_iv_length($cipher);
5151
return random_bytes($ivLength);
5252
}
@@ -59,19 +59,18 @@ public static function getIV(string $cipher) {
5959
* @param string $iv IV for decryption.
6060
* @param string|null $tag AEAD tag from the data encryption.
6161
* @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.
6464
* @global array $conf Configuration variables.
6565
* @since 2.0
6666
* @since 2.3 Added support for AEAD cipher modes.
6767
* @since 2.4 Added ability to specify OPENSSL options.
6868
* @global array $conf Configuration variables.
6969
*/
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 {
7171
global $conf;
7272
$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());
7574
return $data;
7675
}
7776

@@ -81,34 +80,30 @@ public static function decrypt(string $input, string $password, string $iv, stri
8180
* @param array $metadata the $_FILES[] array to use.
8281
* @param string $deletionPassword Deletion password to encrypt along with the metadata.
8382
* @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.
8685
* @since 2.0
8786
* @since 2.2 Added $deletionPassword to the array of things to encrypt.
8887
* @since 2.3 Added support for AEAD cipher modes.
8988
* @global array $conf Configuration variables.
9089
*/
91-
public static function encryptFileDetails(array $metadata, string $deletionPassword, string $password) {
90+
public static function encryptFileDetails(array $metadata, string $deletionPassword, string $password): array {
9291
global $conf;
9392
$cipher = $conf['Encryption-Method'];
94-
$iv = self::getIV($cipher);
93+
$iv = self::createIV($cipher);
9594

96-
$data_array = [
95+
$data = [
9796
base64_encode($metadata['name']),
9897
base64_encode($metadata['size']),
9998
base64_encode($metadata['type']),
10099
base64_encode($deletionPassword),
101100
];
102101

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));
106103

107104
// 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());
113108
}
114109
}

src/com/carlgo11/tempfiles/api/Cleanup.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace com\carlgo11\tempfiles\api;
44

55
use com\carlgo11\tempfiles\datastorage\DataStorage;
6-
use com\carlgo11\tempfiles\exception\BadMethod;
76
use Exception;
87

98
class Cleanup extends API {

src/com/carlgo11/tempfiles/datastorage/DataStorage.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public static function getFile(string $id, string $password): File {
3939

4040
$content = Encryption::decrypt(base64_decode($storedContent), $password, $storedEncryptionData['iv'][0], $storedEncryptionData['tag'][0]);
4141
$metadata = explode(' ', Encryption::decrypt($storedMetaData, $password, $storedEncryptionData['iv'][1], $storedEncryptionData['tag'][1]));
42-
$metadata = ['name' => $metadata[0],
42+
$metadata = [
43+
'name' => $metadata[0],
4344
'size' => $metadata[1],
4445
'type' => $metadata[2],
4546
'delpass' => $metadata[3],

tests/com/carlgo11/tempfiles/EncryptionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class EncryptionTest extends TestCase
1111
public function testGetIV() {
1212
global $conf;
1313
try {
14-
$this->assertIsString(Encryption::getIV($conf['Encryption-Method']));
14+
$this->assertIsString(Encryption::createIV($conf['Encryption-Method']));
1515
} catch (Exception $e) {
1616
error_log($e);
1717
return FALSE;

tests/com/carlgo11/tempfiles/FileTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct($name = NULL, array $data = [], $dataName = '') {
2323
public function testSetIV() {
2424
global $conf;
2525
try {
26-
$iv = [Encryption::getIV($conf['Encryption-Method']), Encryption::getIV($conf['Encryption-Method'])];
26+
$iv = [Encryption::createIV($conf['Encryption-Method']), Encryption::createIV($conf['Encryption-Method'])];
2727
} catch (Exception $e) {
2828
error_log($e);
2929
return FALSE;

0 commit comments

Comments
 (0)