<?php
/**
* @link https://github.com/rogertiongdev/RTphp RTphp GitHub project
* @license https://rogertiongdev.github.io/MIT-License/
*/
/**
* Testing RTpassword on PHP 5.3.29
*
* @version 0.1
* @author Roger Tiong RTdev
*/
/**
* Print values in new line
*
* @param type $v
*/
function nl($v) {
print sprintf('%s<br><br>', $v);
}
/**
* Print hr
*/
function nlhr() {
nl('---------------------------------------------------------------------');
}
require_once '../src/RTpassword.php';
$obj = new RTdev\RTphp\RTpassword();
nlhr();
nl('Start process ...');
nlhr();
if (!$obj->test()) {
nl('Sorry, RTpassword is not compatible with current environment.');
}
else {
nl('RTpassword is compatible with current environment.');
nlhr();
nl('Test 1: With default cost');
nlhr();
nl(sprintf('Password: %s', $password = 'passwordfortesting_1'));
nl(sprintf('Hash: %s', $hash = $obj->bcryptHash($password)));
nl(sprintf('Info: %s', json_encode($obj->getHashInfo($hash))));
nl(sprintf('Need rehash? With no new cost: %s', ($obj->isNeedReHash($hash) ? 'Yes' : 'No')));
nl(sprintf('Need rehash? With new cost (12): %s', ($obj->isNeedReHash($hash, 12) ? 'Yes' : 'No')));
nl(sprintf('Verify with password (%s): %s', $password, (($obj->verify($password, $hash) ? 'Success' : 'Fail'))));
nl(sprintf('Verify with password (%s): %s', 'thisisfakepassword', (($obj->verify('thisisfakepassword', $hash) ? 'Success' : 'Fail'))));
nlhr();
nl('Test 2: With custom cost (13)');
nlhr();
nl(sprintf('Password: %s', $password2 = 'passwordfortesting_2'));
nl(sprintf('Hash: %s', $hash2 = $obj->bcryptHash($password2, 13)));
nl(sprintf('Info: %s', json_encode($obj->getHashInfo($hash2))));
nl(sprintf('Need rehash? With default cost: %s', ($obj->isNeedReHash($hash2) ? 'Yes' : 'No')));
nl(sprintf('Need rehash? With current cost (13): %s', ($obj->isNeedReHash($hash2, 13) ? 'Yes' : 'No')));
nl(sprintf('Need rehash? With new cost (12): %s', ($obj->isNeedReHash($hash, 12) ? 'Yes' : 'No')));
nl(sprintf('Verify with password (%s): %s', $password2, (($obj->verify($password2, $hash2) ? 'Success' : 'Fail'))));
nl(sprintf('Verify with password (%s): %s', 'thisisfakepassword', (($obj->verify('thisisfakepassword', $hash2) ? 'Success' : 'Fail'))));
nlhr();
nl('Test 3: Speed');
nlhr();
$max = 16;
$longpassword = 'Ju$+U$ingL0ngP@$$W0rdT0T3$tH@$hingSp33d!#?';
nlhr();
nl(sprintf('Password: %s', $longpassword));
nlhr();
for ($i = 7; ++$i <= $max;) {
nl(sprintf('Cost: %d', $i));
$start = time();
nl(sprintf('Hash: %s', $hash = $obj->bcryptHash($longpassword, $i)));
nl(sprintf('Duration (H:i:s): %s', (date('H:i:s', (time() - $start)))));
nl(sprintf('Verify: %s', ($obj->verify($longpassword, $hash) ? 'Success' : 'Fail')));
nlhr();
}
}
nlhr();
nl('End process!');
nlhr();
/**
* Code tested by PHP built-in password function on http://sandbox.onlinephpfunctions.com/ with PHP 7.0.5<br>
* Longer time is needed for higher cost. Suggest to use default cost.<br>
* Hash generated by RTpassword with default cost: 10<br>
* print (password_verify('Ju$+U$ingL0ngP@$$W0rdT0T3$tH@$hingSp33d!#?', '$2y$10$KUd63hYCG5gt4o9tL1beW.n2kssvhEwX/uahz2aaG/j95uzv2F8Qu') ? 'Success' : 'Fail');
*/
|