Skip to content

Commit 26ea131

Browse files
SQUASHED COMMIT
PSR-4 Refactor PSR-4 autoloader in composer.json, version 2.0.0 Exception handler fixes Fix example/benchmark scripts Fix exception definition Set exception handler, use \ prefixes on functions in Crypto.php Be more explicit
1 parent 78dfc81 commit 26ea131

12 files changed

+435
-326
lines changed

autoload.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* PSR-4 compatible autoloader
4+
*/
5+
\spl_autoload_register(function ($class) {
6+
// Project-specific namespace prefix
7+
$prefix = 'Defuse\\Crypto';
8+
9+
// Base directory for the namespace prefix
10+
$base_dir = __DIR__.'/src/';
11+
12+
// Does the class use the namespace prefix?
13+
$len = \strlen($prefix);
14+
if (\strncmp($prefix, $class, $len) !== 0) {
15+
// no, move to the next registered autoloader
16+
return;
17+
}
18+
19+
// Get the relative class name
20+
$relative_class = \substr($class, $len);
21+
22+
// Replace the namespace prefix with the base directory, replace namespace
23+
// separators with directory separators in the relative class name, append
24+
// with .php
25+
$file = $base_dir.
26+
\str_replace(
27+
['\\', '_'],
28+
'/',
29+
$relative_class
30+
).'.php';
31+
32+
// If the file exists, require it
33+
if (\file_exists($file)) {
34+
require $file;
35+
}
36+
});
37+
38+
$crypto_exception_handler_object_dont_touch_me = new \Defuse\Crypto\ExceptionHandler;

benchmark.php

+23-24
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,41 @@
11
<?php
2+
use \Defuse\Crypto\Crypto;
3+
require_once'autoload.php';
4+
5+
function showResults($type, $start, $end, $count)
6+
{
7+
$time = $end - $start;
8+
$rate = $count / $time;
9+
echo $type, ': ', $rate, ' calls/s', "\n";
10+
}
211

3-
require_once('Crypto.php');
412

513
// Note: By default, the runtime tests are "cached" and not re-executed for
614
// every call. To disable this, look at the RuntimeTest() function.
715

8-
$start = microtime(true);
16+
$start = \microtime(true);
917
for ($i = 0; $i < 1000; $i++) {
10-
$key = Crypto::CreateNewRandomKey();
18+
$key = Crypto::createNewRandomKey();
1119
}
12-
$end = microtime(true);
13-
showResults("CreateNewRandomKey()", $start, $end, 1000);
20+
$end = \microtime(true);
21+
showResults("createNewRandomKey()", $start, $end, 1000);
1422

15-
$start = microtime(true);
23+
$start = \microtime(true);
1624
for ($i = 0; $i < 100; $i++) {
17-
$ciphertext = Crypto::Encrypt(
18-
str_repeat("A", 1024*1024),
19-
str_repeat("B", 16)
25+
$ciphertext = Crypto::encrypt(
26+
\str_repeat("A", 1024*1024),
27+
\str_repeat("B", 16)
2028
);
2129
}
2230
$end = microtime(true);
23-
showResults("Encrypt(1MB)", $start, $end, 100);
31+
showResults("encrypt(1MB)", $start, $end, 100);
2432

2533
$start = microtime(true);
2634
for ($i = 0; $i < 1000; $i++) {
27-
$ciphertext = Crypto::Encrypt(
28-
str_repeat("A", 1024),
29-
str_repeat("B", 16)
35+
$ciphertext = Crypto::encrypt(
36+
\str_repeat("A", 1024),
37+
\str_repeat("B", 16)
3038
);
3139
}
32-
$end = microtime(true);
33-
showResults("Encrypt(1KB)", $start, $end, 1000);
34-
35-
function showResults($type, $start, $end, $count)
36-
{
37-
$time = $end - $start;
38-
$rate = $count / $time;
39-
echo "$type: $rate calls/s\n";
40-
}
41-
42-
?>
40+
$end = \microtime(true);
41+
showResults("encrypt(1KB)", $start, $end, 1000);

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "defuse/php-encryption",
33
"description": "Secure PHP Encryption Library",
44
"license": "MIT",
5+
"version": "2.0.0",
56
"keywords": ["security", "encryption", "AES", "openssl", "cipher", "cryptography", "symmetric key cryptography", "crypto", "encrypt", "authenticated encryption"],
67
"authors": [
78
{
@@ -10,7 +11,7 @@
1011
}
1112
],
1213
"autoload": {
13-
"files": ["Crypto.php"]
14+
"files": ["src/autoload.php"]
1415
},
1516
"require": {
1617
"php": ">=5.4.0",

example.php

+37-33
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,40 @@
11
<?php
2-
require_once('Crypto.php');
3-
try {
4-
$key = Crypto::CreateNewRandomKey();
5-
// WARNING: Do NOT encode $key with bin2hex() or base64_encode(),
6-
// they may leak the key to the attacker through side channels.
7-
} catch (CryptoTestFailedException $ex) {
8-
die('Cannot safely create a key');
9-
} catch (CannotPerformOperationException $ex) {
10-
die('Cannot safely create a key');
11-
}
122

13-
$message = "ATTACK AT DAWN";
14-
try {
15-
$ciphertext = Crypto::Encrypt($message, $key);
16-
} catch (CryptoTestFailedException $ex) {
17-
die('Cannot safely perform encryption');
18-
} catch (CannotPerformOperationException $ex) {
19-
die('Cannot safely perform decryption');
20-
}
3+
use \Defuse\Crypto\Crypto;
4+
use \Defuse\Crypto\Exception as Ex;
215

22-
try {
23-
$decrypted = Crypto::Decrypt($ciphertext, $key);
24-
} catch (InvalidCiphertextException $ex) { // VERY IMPORTANT
25-
// Either:
26-
// 1. The ciphertext was modified by the attacker,
27-
// 2. The key is wrong, or
28-
// 3. $ciphertext is not a valid ciphertext or was corrupted.
29-
// Assume the worst.
30-
die('DANGER! DANGER! The ciphertext has been tampered with!');
31-
} catch (CryptoTestFailedException $ex) {
32-
die('Cannot safely perform encryption');
33-
} catch (CannotPerformOperationException $ex) {
34-
die('Cannot safely perform decryption');
35-
}
36-
?>
6+
require_once 'autoload.php';
7+
8+
try {
9+
$key = Crypto::createNewRandomKey();
10+
// WARNING: Do NOT encode $key with bin2hex() or base64_encode(),
11+
// they may leak the key to the attacker through side channels.
12+
} catch (Ex\CryptoTestFailed $ex) {
13+
die('Cannot safely create a key');
14+
} catch (Ex\CannotPerformOperation $ex) {
15+
die('Cannot safely create a key');
16+
}
17+
18+
$message = "ATTACK AT DAWN";
19+
try {
20+
$ciphertext = Crypto::encrypt($message, $key);
21+
} catch (Ex\CryptoTestFailed $ex) {
22+
die('Cannot safely perform encryption');
23+
} catch (Ex\CannotPerformOperation $ex) {
24+
die('Cannot safely perform encryption');
25+
}
26+
27+
try {
28+
$decrypted = Crypto::decrypt($ciphertext, $key);
29+
} catch (Ex\InvalidCiphertext $ex) { // VERY IMPORTANT
30+
// Either:
31+
// 1. The ciphertext was modified by the attacker,
32+
// 2. The key is wrong, or
33+
// 3. $ciphertext is not a valid ciphertext or was corrupted.
34+
// Assume the worst.
35+
die('DANGER! DANGER! The ciphertext has been tampered with!');
36+
} catch (Ex\CryptoTestFailed $ex) {
37+
die('Cannot safely perform dencryption');
38+
} catch (Ex\CannotPerformOperation $ex) {
39+
die('Cannot safely perform decryption');
40+
}

0 commit comments

Comments
 (0)