- crypt: documentation ( source)
- array_merge: documentation ( source)
- array_rand: documentation ( source)
- range: documentation ( source)
<?php
// generate random string given length
function genString($length = 22){
$s = '';
$chars = array_merge(range('A','Z'), range('a','z'), range(0,9));
while($length > 0){
$randKey = array_rand($chars, 1);
$s .= $chars[$randKey];
$length--;
}
return $s;
}
$prefix = (PHP_VERSION_ID >= 50307) ? '$2y$' : '$2a$';
$salt = $prefix.genString();
//echo 'Salt: '.$salt.'<br>';
$password = 'password';
$hash = crypt($password, $salt);
echo 'Strlen: '.strlen($hash).' ------ ';
if (crypt($password, $hash) == $hash) {
echo "Password verified!";
}