From c2ae3b4082e78c18a27496e84a2504aa14a4b20f Mon Sep 17 00:00:00 2001 From: Sergey Dryabzhinsky Date: Thu, 18 May 2017 06:55:04 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=98=D1=89=D0=B5=D0=BC=20=D0=B4=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D1=83=D0=BF=D0=BD=D1=8B=D0=B5=20=D0=B0=D0=BB=D0=B3?= =?UTF-8?q?=D0=BE=D1=80=D0=B8=D1=82=D0=BC=D1=8B=20=D1=85=D0=B5=D1=88=D0=B8?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F=20=D0=B4=D0=BB=D1=8F?= =?UTF-8?q?=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B8=20crypt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - поиск алгоритма по примеру из офиц. документации - добавил вывод найденного алгоритма - добавил "отбивки" кода на "секции" --- bench.php | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/bench.php b/bench.php index 5a4e2f7..edc9ab1 100644 --- a/bench.php +++ b/bench.php @@ -36,6 +36,9 @@ $emptyResult = array(0, '-.---', '-.--', '-.--', 0); +$cryptSalt = null; +$cryptAlgoName = 'default'; + // That gives around 256Mb memory use and reasonable test time $testMemoryFull = 256*1024*1024; // Arrays are matrix [$dimention] x [$dimention] @@ -46,6 +49,9 @@ // Nice dice roll $stringConcatLoopLimit = 7700000; + +/** ---------------------------------- Common functions -------------------------------------------- */ + function get_microtime() { $time = microtime(true); @@ -229,6 +235,37 @@ function getCpuInfo($fireUpCpu = false) return $cpu; } +/** ---------------------------------- Code for common variables, tune values -------------------------------------------- */ + +// Search most advanced algo for SALT +// http://php.net/manual/ru/function.crypt.php example #3 +$cryptSalt = null; +if (defined('CRYPT_STD_DES') && CRYPT_STD_DES == 1) { + $cryptSalt = 'rl'; + $cryptAlgoName = 'Std. DES'; +} +if (defined('CRYPT_EXT_DES') && CRYPT_EXT_DES == 1) { + $cryptSalt = '_J9..rasm'; + $cryptAlgoName = 'Ext. DES'; +} +if (defined('CRYPT_MD5') && CRYPT_MD5 == 1) { + $cryptSalt = '$1$rasmusle$'; + $cryptAlgoName = 'MD5'; +} +if (defined('CRYPT_BLOWFISH') && CRYPT_BLOWFISH == 1) { + $cryptSalt = '$2a$07$usesomesillystringforsalt$'; + $cryptAlgoName = 'BlowFish'; +} +if (defined('CRYPT_SHA256') && CRYPT_SHA256 == 1) { + $cryptSalt = '$5$rounds=5000$usesomesillystringforsalt$'; + $cryptAlgoName = 'Sha256'; +} +if (defined('CRYPT_SHA512') && CRYPT_SHA512 == 1) { + $cryptSalt = '$6$rounds=5000$usesomesillystringforsalt$'; + $cryptAlgoName = 'Sha512'; +} + + $cpuInfo = getCpuInfo(); // CPU throttling? if (abs($cpuInfo['mips'] - $cpuInfo['mhz']) > 400) { @@ -264,6 +301,8 @@ function getCpuInfo($fireUpCpu = false) } +/** ---------------------------------- Common functions for tests -------------------------------------------- */ + /** * @return array((int)seconds, (str)seconds, (str)operations/sec), (str)opterations/MHz) */ @@ -292,6 +331,10 @@ function format_result_test($diffSeconds, $opCount, $memory = 0) } } + +/** ---------------------------------- Tests functions -------------------------------------------- */ + + function test_01_Math($count = 1400000) { $time_start = get_microtime(); @@ -420,7 +463,7 @@ function test_07_1_Hashing($count = 1300000) function test_07_2_Crypt($count = 10000) { - global $stringTest; + global $stringTest, $cryptSalt; $time_start = get_microtime(); $stringFunctions = array('crypt'); foreach ($stringFunctions as $key => $function) { @@ -430,7 +473,7 @@ function test_07_2_Crypt($count = 10000) } for ($i = 0; $i < $count; $i++) { foreach ($stringFunctions as $function) { - $r = call_user_func_array($function, array($stringTest, '_J9..rasm')); + $r = call_user_func_array($function, array($stringTest, $cryptSalt)); } } return format_result_test(get_microtime() - $time_start, $count, memory_get_usage(true)); @@ -645,6 +688,9 @@ function test_17_2_Loop_Undefined_Access($count = 20000000) include_once 'php5.inc'; } +/** ---------------------------------- Common code -------------------------------------------- */ + + $total = 0; $functions = get_defined_functions(); sort($functions['user']); @@ -661,6 +707,7 @@ function test_17_2_Loop_Undefined_Access($count = 20000000) . str_pad("Memory", $padInfo) . " : " . $memoryLimitMb . ' available' . "\n" . str_pad("PHP version:", $padInfo) . " : " . PHP_VERSION . "\n" . str_pad("Benchmark version:", $padInfo) . " : " . $scriptVersion . "\n" + . str_pad("Crypt hash algo:", $padInfo) . " : " . $cryptAlgoName . "\n" . str_pad("Platform:", $padInfo) . " : " . PHP_OS . "\n" . "$line\n" . str_pad('TEST NAME', $padLabel) . " :" From 701116c20231c21c2434b2ab3ab0f83e12052bca Mon Sep 17 00:00:00 2001 From: Sergey Dryabzhinsky Date: Thu, 18 May 2017 07:10:19 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=98=D1=81=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7?= =?UTF-8?q?=D1=83=D0=B5=D0=BC=20MD5=20=D0=B0=D0=BB=D0=B3=D0=BE=D1=80=D0=B8?= =?UTF-8?q?=D1=82=D0=BC=20=D0=B4=D0=BB=D1=8F=20crypt()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bench.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/bench.php b/bench.php index edc9ab1..428fc50 100644 --- a/bench.php +++ b/bench.php @@ -237,7 +237,7 @@ function getCpuInfo($fireUpCpu = false) /** ---------------------------------- Code for common variables, tune values -------------------------------------------- */ -// Search most advanced algo for SALT +// Search most common available algo for SALT // http://php.net/manual/ru/function.crypt.php example #3 $cryptSalt = null; if (defined('CRYPT_STD_DES') && CRYPT_STD_DES == 1) { @@ -252,6 +252,13 @@ function getCpuInfo($fireUpCpu = false) $cryptSalt = '$1$rasmusle$'; $cryptAlgoName = 'MD5'; } + +/** + * These are available since 5.3+ + * MD5 should be available to all versions. + */ + +/* if (defined('CRYPT_BLOWFISH') && CRYPT_BLOWFISH == 1) { $cryptSalt = '$2a$07$usesomesillystringforsalt$'; $cryptAlgoName = 'BlowFish'; @@ -264,12 +271,17 @@ function getCpuInfo($fireUpCpu = false) $cryptSalt = '$6$rounds=5000$usesomesillystringforsalt$'; $cryptAlgoName = 'Sha512'; } +*/ + +if ($cryptAlgoName != 'MD5' && $cryptAlgoName != 'default') { + print("
\n<<< WARNING >>>\nHashing algorithm MD5 not available for crypt() in this PHP build!\n It should be available in any PHP build.\n
" . PHP_EOL); +} $cpuInfo = getCpuInfo(); // CPU throttling? if (abs($cpuInfo['mips'] - $cpuInfo['mhz']) > 400) { - print("
\n<<< WARNING >>>\nCPU is in powersaving mode? Set CPU governor to 'performance'!\nFire up CPU and recalculate MHz!\n
" . PHP_EOL); + print("
\n<<< WARNING >>>\nCPU is in powersaving mode? Set CPU governor to 'performance'!\n Fire up CPU and recalculate MHz!\n
" . PHP_EOL); $cpuInfo = getCpuInfo(true); }