Subversion Repositories oidplus

Rev

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

#!/usr/bin/env php
<?php

/*
 * OIDplus 2.0
 * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @param int $treshold_secs
 * @param bool $verbose
 * @return int
 */
function bcrypt_rounds(int $treshold_secs, bool $verbose=true): int {
        if ($verbose) echo "Searching BCrypt cost (rounds) for <$treshold_secs secs...\n";
        $found = 31;
        for ($rounds=4; $rounds<=31; $rounds++) { // 4..31 is the valid range of PHP Bcrypt
                $x = time();
                $start = microtime(true);
                password_hash("TEST", PASSWORD_BCRYPT, array("cost" => $rounds));
                $time_elapsed_secs = microtime(true) - $start;
                if ($verbose) echo "Cost = $rounds Rounds => ".round($time_elapsed_secs,5)." sec\n";
                if ($time_elapsed_secs == $treshold_secs) { $found = $rounds; break; }
                if ($time_elapsed_secs > $treshold_secs) { $found = $rounds-1; break; }
        }
        if ($verbose) echo "RESULT: Cost = $found\n";
        return $found;
}

if ($argc > 1) {
        if (($argv[1] = '-h') || ($argv[1] = '--help')) {
                echo "$argv[0] <seconds>\n";
        } else {
                bcrypt_rounds($argv[1], true);
        }
} else {
        bcrypt_rounds(1.00, true);
}