Subversion Repositories oidplus

Rev

Rev 797 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
619 daniel-mar 1
#!/usr/bin/env php
2
<?php
3
 
1130 daniel-mar 4
/*
5
 * OIDplus 2.0
6
 * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
 
21
/**
22
 * @param int $treshold_secs
23
 * @param bool $verbose
24
 * @return int
25
 */
26
function bcrypt_rounds(int $treshold_secs, bool $verbose=true): int {
619 daniel-mar 27
	if ($verbose) echo "Searching BCrypt cost (rounds) for <$treshold_secs secs...\n";
28
	$found = 31;
29
	for ($rounds=4; $rounds<=31; $rounds++) { // 4..31 is the valid range of PHP Bcrypt
30
		$x = time();
31
		$start = microtime(true);
32
		password_hash("TEST", PASSWORD_BCRYPT, array("cost" => $rounds));
33
		$time_elapsed_secs = microtime(true) - $start;
34
		if ($verbose) echo "Cost = $rounds Rounds => ".round($time_elapsed_secs,5)." sec\n";
35
		if ($time_elapsed_secs == $treshold_secs) { $found = $rounds; break; }
36
		if ($time_elapsed_secs > $treshold_secs) { $found = $rounds-1; break; }
37
	}
38
	if ($verbose) echo "RESULT: Cost = $found\n";
39
	return $found;
40
}
41
 
42
if ($argc > 1) {
43
	if (($argv[1] = '-h') || ($argv[1] = '--help')) {
44
		echo "$argv[0] <seconds>\n";
45
	} else {
46
		bcrypt_rounds($argv[1], true);
47
	}
48
} else {
49
	bcrypt_rounds(1.00, true);
1130 daniel-mar 50
}