Skip to content

Commit 0678e60

Browse files
author
Sergey Dryabzhinsky
authored
Merge pull request #6 from rusoft/fix-php4-crypt-segfault-check-algo
Ищем доступные алгоритмы хеширования для функции crypt
2 parents dc38e44 + 701116c commit 0678e60

File tree

1 file changed

+62
-3
lines changed

1 file changed

+62
-3
lines changed

bench.php

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636

3737
$emptyResult = array(0, '-.---', '-.--', '-.--', 0);
3838

39+
$cryptSalt = null;
40+
$cryptAlgoName = 'default';
41+
3942
// That gives around 256Mb memory use and reasonable test time
4043
$testMemoryFull = 256*1024*1024;
4144
// Arrays are matrix [$dimention] x [$dimention]
@@ -46,6 +49,9 @@
4649
// Nice dice roll
4750
$stringConcatLoopLimit = 7700000;
4851

52+
53+
/** ---------------------------------- Common functions -------------------------------------------- */
54+
4955
function get_microtime()
5056
{
5157
$time = microtime(true);
@@ -229,10 +235,53 @@ function getCpuInfo($fireUpCpu = false)
229235
return $cpu;
230236
}
231237

238+
/** ---------------------------------- Code for common variables, tune values -------------------------------------------- */
239+
240+
// Search most common available algo for SALT
241+
// http://php.net/manual/ru/function.crypt.php example #3
242+
$cryptSalt = null;
243+
if (defined('CRYPT_STD_DES') && CRYPT_STD_DES == 1) {
244+
$cryptSalt = 'rl';
245+
$cryptAlgoName = 'Std. DES';
246+
}
247+
if (defined('CRYPT_EXT_DES') && CRYPT_EXT_DES == 1) {
248+
$cryptSalt = '_J9..rasm';
249+
$cryptAlgoName = 'Ext. DES';
250+
}
251+
if (defined('CRYPT_MD5') && CRYPT_MD5 == 1) {
252+
$cryptSalt = '$1$rasmusle$';
253+
$cryptAlgoName = 'MD5';
254+
}
255+
256+
/**
257+
* These are available since 5.3+
258+
* MD5 should be available to all versions.
259+
*/
260+
261+
/*
262+
if (defined('CRYPT_BLOWFISH') && CRYPT_BLOWFISH == 1) {
263+
$cryptSalt = '$2a$07$usesomesillystringforsalt$';
264+
$cryptAlgoName = 'BlowFish';
265+
}
266+
if (defined('CRYPT_SHA256') && CRYPT_SHA256 == 1) {
267+
$cryptSalt = '$5$rounds=5000$usesomesillystringforsalt$';
268+
$cryptAlgoName = 'Sha256';
269+
}
270+
if (defined('CRYPT_SHA512') && CRYPT_SHA512 == 1) {
271+
$cryptSalt = '$6$rounds=5000$usesomesillystringforsalt$';
272+
$cryptAlgoName = 'Sha512';
273+
}
274+
*/
275+
276+
if ($cryptAlgoName != 'MD5' && $cryptAlgoName != 'default') {
277+
print("<pre>\n<<< WARNING >>>\nHashing algorithm MD5 not available for crypt() in this PHP build!\n It should be available in any PHP build.\n</pre>" . PHP_EOL);
278+
}
279+
280+
232281
$cpuInfo = getCpuInfo();
233282
// CPU throttling?
234283
if (abs($cpuInfo['mips'] - $cpuInfo['mhz']) > 400) {
235-
print("<pre>\n<<< WARNING >>>\nCPU is in powersaving mode? Set CPU governor to 'performance'!\nFire up CPU and recalculate MHz!\n</pre>" . PHP_EOL);
284+
print("<pre>\n<<< WARNING >>>\nCPU is in powersaving mode? Set CPU governor to 'performance'!\n Fire up CPU and recalculate MHz!\n</pre>" . PHP_EOL);
236285
$cpuInfo = getCpuInfo(true);
237286
}
238287

@@ -264,6 +313,8 @@ function getCpuInfo($fireUpCpu = false)
264313
}
265314

266315

316+
/** ---------------------------------- Common functions for tests -------------------------------------------- */
317+
267318
/**
268319
* @return array((int)seconds, (str)seconds, (str)operations/sec), (str)opterations/MHz)
269320
*/
@@ -292,6 +343,10 @@ function format_result_test($diffSeconds, $opCount, $memory = 0)
292343
}
293344
}
294345

346+
347+
/** ---------------------------------- Tests functions -------------------------------------------- */
348+
349+
295350
function test_01_Math($count = 1400000)
296351
{
297352
$time_start = get_microtime();
@@ -420,7 +475,7 @@ function test_07_1_Hashing($count = 1300000)
420475

421476
function test_07_2_Crypt($count = 10000)
422477
{
423-
global $stringTest;
478+
global $stringTest, $cryptSalt;
424479
$time_start = get_microtime();
425480
$stringFunctions = array('crypt');
426481
foreach ($stringFunctions as $key => $function) {
@@ -430,7 +485,7 @@ function test_07_2_Crypt($count = 10000)
430485
}
431486
for ($i = 0; $i < $count; $i++) {
432487
foreach ($stringFunctions as $function) {
433-
$r = call_user_func_array($function, array($stringTest, '_J9..rasm'));
488+
$r = call_user_func_array($function, array($stringTest, $cryptSalt));
434489
}
435490
}
436491
return format_result_test(get_microtime() - $time_start, $count, memory_get_usage(true));
@@ -645,6 +700,9 @@ function test_17_2_Loop_Undefined_Access($count = 20000000)
645700
include_once 'php5.inc';
646701
}
647702

703+
/** ---------------------------------- Common code -------------------------------------------- */
704+
705+
648706
$total = 0;
649707
$functions = get_defined_functions();
650708
sort($functions['user']);
@@ -661,6 +719,7 @@ function test_17_2_Loop_Undefined_Access($count = 20000000)
661719
. str_pad("Memory", $padInfo) . " : " . $memoryLimitMb . ' available' . "\n"
662720
. str_pad("PHP version:", $padInfo) . " : " . PHP_VERSION . "\n"
663721
. str_pad("Benchmark version:", $padInfo) . " : " . $scriptVersion . "\n"
722+
. str_pad("Crypt hash algo:", $padInfo) . " : " . $cryptAlgoName . "\n"
664723
. str_pad("Platform:", $padInfo) . " : " . PHP_OS . "\n"
665724
. "$line\n"
666725
. str_pad('TEST NAME', $padLabel) . " :"

0 commit comments

Comments
 (0)