<?php

/*
 * Copyright (c) 2018 Earworks.
 * Codeigniter Project Template Modification
 * Please contact our email for more information :
 * contact@earworks.co.id
 */

defined('BASEPATH') OR exit('No direct script access allowed');
if (!function_exists('run_hash')) {

    function run_hash($password) {
        return password_hash($password, PASSWORD_DEFAULT, ['cost' => COST]);
    }

}
if (!function_exists('run_verify')) {

    function run_verify($password, $hashed) {
        $newhased = false;
        if (password_verify($password, $hashed)) {
            // Check if a newer hashing algorithm is available
            // or the cost has changed
            if (password_needs_rehash($hashed, PASSWORD_DEFAULT, ['cost' => COST])) {
                // If so, create a new hash, and replace the old one
                $newhased = run_hash($password);
            } else {
                $newhased = true;
            }
        }
        return $newhased;
    }

}
if (!function_exists('get_cost')) {

    function get_cost($min_ms = 250) {
        for ($i = 4; $i < 31; $i++) {
            $options = ['cost' => $i];
            $time_start = microtime(true);
            password_hash(APPLICATION_NAME, PASSWORD_DEFAULT, $options);
            $time_end = microtime(true);
            if (($time_end - $time_start) * 1000 > $min_ms) {
                return $i;
            }
        }
    }

}