Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
openssl_encrypt() cyphers testing comparsion
<?php
/**
* User: Arris
* Date: 21.09.2017, time: 0:35
*/
const TEST_COUNT = 100000;
const SOURCE = 'Тестируем обратимое шифрование на php';
const KEY = "password";
function TESTER( $testing_function, $argument )
{
$t = microtime(true);
for ($test_iterator = 0; $test_iterator < TEST_COUNT; $test_iterator++) {
$testing_function( $argument );
}
return round(microtime(true) - $t, 4);
}
$crypt = function($cipher) {
$ivlen = openssl_cipher_iv_length($cipher);
$iv = openssl_random_pseudo_bytes($ivlen);
openssl_encrypt(SOURCE, $cipher, KEY, $options=0, $iv);
};
$methods = openssl_get_cipher_methods(false);
array_splice( $methods, 0, count($methods) / 2);
$timings = array();
foreach ($methods as $cypher) {
$time = TESTER( $crypt, $cypher );
$timings[ $cypher ] = $time;
echo str_pad($cypher, 40, ' ', STR_PAD_LEFT), " have time ", str_pad($time, 8, STR_PAD_LEFT), ' seconds. ', PHP_EOL;
}
uasort($timings, function($a, $b){
return $a <=> $b;
});
$min_time = round(reset($timings) / TEST_COUNT, 7);
$min_cypher = key($timings);
$max_time = round(end($timings) / TEST_COUNT, 7);
$max_cypher = key($timings);
echo '-------------', PHP_EOL;
echo "Total tests: ", count($timings), PHP_EOL;
echo "Max timing : {$max_time} seconds for `{$max_cypher}` algorithm.", PHP_EOL;
echo "Min timing : {$min_time} seconds for `{$min_cypher}` algorithm.", PHP_EOL;
echo 'Details: ', PHP_EOL;
foreach ($timings as $m => $t) {
echo '- ', str_pad($t, 8, STR_PAD_LEFT), " seconds for `{$m}`", PHP_EOL;
}
echo PHP_EOL;
@KarelWintersky

This comment has been minimized.

Copy link
Owner Author

@KarelWintersky KarelWintersky commented Sep 20, 2017

Hardware: GA-N3150N-D3V, Celeron™ N3150 (1.6 GHz) quad-core processor, 4Gb RAM
Software: Gentoo 4.2.15, PHP version 7.0.2

Results:
Total tests: 99
Max timing : 0.000218 seconds for camellia-192-cfb1 algorithm.
Min timing : 4.2E-6 seconds for aes-128-ecb algorithm.

Details (time for 100000 operations)

  • 0.423500 seconds for aes-128-ecb
  • 0.428100 seconds for aes-192-ecb
  • 0.433100 seconds for aes-256-ecb
  • 0.538300 seconds for camellia-128-ecb
  • 0.545500 seconds for rc4-40
  • 0.552800 seconds for rc5-ecb
  • 0.561100 seconds for rc4
  • 0.566800 seconds for camellia-256-ecb
  • 0.567200 seconds for camellia-192-ecb
  • 0.582400 seconds for idea-ecb
  • 0.593200 seconds for cast5-ecb
  • 0.636500 seconds for des-ecb
  • 0.641500 seconds for seed-ecb
  • 0.650400 seconds for rc4-hmac-md5
  • 0.820200 seconds for rc2-ecb
  • 1.097400 seconds for des-ede
  • 1.105600 seconds for id-aes128-wrap
  • 1.106400 seconds for id-aes256-wrap
  • 1.124500 seconds for des-ede3
  • 1.128900 seconds for id-aes192-wrap
  • 1.132700 seconds for aes-128-ofb
  • 1.148100 seconds for aes-192-cbc
  • 1.154000 seconds for aes-192-ofb
  • 1.154100 seconds for aes-256-cbc
  • 1.161500 seconds for aes-256-cfb
  • 1.166600 seconds for aes-256-ofb
  • 1.169700 seconds for aes-128-xts
  • 1.171800 seconds for aes-256-ctr
  • 1.174700 seconds for aes-192-ctr
  • 1.192900 seconds for aes-192-cfb
  • 1.201900 seconds for aes-256-xts
  • 1.225600 seconds for id-aes128-CCM
  • 1.237100 seconds for rc5-cbc
  • 1.244200 seconds for aes-256-ccm
  • 1.244600 seconds for id-aes192-CCM
  • 1.265900 seconds for rc5-ofb
  • 1.271800 seconds for rc5-cfb
  • 1.274200 seconds for aes-192-ccm
  • 1.276700 seconds for id-aes256-CCM
  • 1.294300 seconds for aes-256-cbc-hmac-sha1
  • 1.312900 seconds for idea-cbc
  • 1.315800 seconds for idea-cfb
  • 1.317100 seconds for camellia-128-cfb
  • 1.317100 seconds for idea-ofb
  • 1.319700 seconds for camellia-128-ofb
  • 1.322900 seconds for cast5-cbc
  • 1.324000 seconds for cast5-cfb
  • 1.324100 seconds for cast5-ofb
  • 1.335200 seconds for camellia-256-cfb
  • 1.344400 seconds for camellia-192-ofb
  • 1.345600 seconds for camellia-256-ofb
  • 1.348100 seconds for camellia-192-cfb
  • 1.363600 seconds for camellia-128-cbc
  • 1.380400 seconds for aes-128-gcm
  • 1.384100 seconds for des-ofb
  • 1.386400 seconds for aes-256-gcm
  • 1.387200 seconds for camellia-256-cbc
  • 1.388000 seconds for camellia-192-cbc
  • 1.390700 seconds for id-aes256-GCM
  • 1.390900 seconds for id-aes128-GCM
  • 1.391000 seconds for des-cbc
  • 1.392500 seconds for des-cfb
  • 1.392600 seconds for aes-192-gcm
  • 1.415100 seconds for id-aes192-GCM
  • 1.417200 seconds for desx-cbc
  • 1.418500 seconds for id-smime-alg-CMS3DESwrap
  • 1.474400 seconds for seed-ofb
  • 1.475200 seconds for aes-256-cbc-hmac-sha256
  • 1.482000 seconds for rc2-64-cbc
  • 1.483200 seconds for rc2-40-cbc
  • 1.487000 seconds for seed-cbc
  • 1.494300 seconds for seed-cfb
  • 1.495200 seconds for rc2-cbc
  • 1.517300 seconds for rc2-cfb
  • 1.518200 seconds for rc2-ofb
  • 1.660400 seconds for aes-192-cfb8
  • 1.716400 seconds for aes-256-cfb8
  • 1.882800 seconds for des-ede-ofb
  • 1.896800 seconds for des-ede-cfb
  • 1.909800 seconds for des-ede-cbc
  • 1.919100 seconds for des-ede3-cfb
  • 1.923600 seconds for des-ede3-cbc
  • 1.929500 seconds for des-ede3-ofb
  • 2.746900 seconds for camellia-128-cfb8
  • 2.772500 seconds for des-cfb8
  • 3.152800 seconds for camellia-192-cfb8
  • 3.155100 seconds for camellia-256-cfb8
  • 5.566000 seconds for des-ede3-cfb8
  • 6.055100 seconds for des-ede3-cfb1
  • 6.226900 seconds for bf-ecb
  • 7.007100 seconds for bf-cbc
  • 7.041600 seconds for bf-ofb
  • 7.053700 seconds for bf-cfb
  • 10.65040 seconds for aes-192-cfb1
  • 11.06250 seconds for aes-256-cfb1
  • 17.27720 seconds for des-cfb1
  • 18.45350 seconds for camellia-128-cfb1
  • 21.79570 seconds for camellia-256-cfb1
  • 21.80420 seconds for camellia-192-cfb1
@KarelWintersky

This comment has been minimized.

Copy link
Owner Author

@KarelWintersky KarelWintersky commented Sep 20, 2017

for ($i = 0; $i < 10; $i++) {
    echo "[$i] ", openssl_random_pseudo_bytes( openssl_cipher_iv_length('seed-ofb') ), PHP_EOL;
}

// returns KiTTYKiTTYKiTTY at last iteration
O.O

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment