可能对密码执行速度有用。
<?php
const TEST_COUNT = 100000;
const SOURCE = '注意,帖子中不允许使用 HTML 标签,但笔记格式将保留。';
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), " 耗时 ", str_pad($time, 8, STR_PAD_LEFT), ' 秒。 ', 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 "总测试次数: ", count($timings), PHP_EOL;
echo "最大耗时: "{$max_time} 秒,使用 `{$max_cypher}` 算法。", PHP_EOL;
echo "最小耗时: "{$min_time} 秒,使用 `{$min_cypher}` 算法。", PHP_EOL;
echo '详细信息: ', PHP_EOL;
foreach ($timings as $m => $t) {
echo '- ', str_pad($t, 8, STR_PAD_LEFT), " 秒,使用 `{$m}`", PHP_EOL;
}
echo PHP_EOL;