<?php// See the password_hash() example to see where this came from.$hash = '$2y$07$BCryptRequires22Chrcte/VlQH0piJtjXl.0t1XkA8pw9dMXTpOq';
if (password_verify('rasmuslerdorf', $hash)) {echo 'Password is valid!';} else {echo 'Invalid password.';}
function blowfishCrypt($password,$cost){$chars='./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';$salt=sprintf('$2y$%02d$',$cost);//For PHP < PHP 5.3.7 use this instead// $salt=sprintf('$2a$%02d$',$cost);//Create a 22 character salt -edit- 2013.01.15 - replaced rand with mt_randmt_srand();for($i=0;$i<22;$i++) $salt.=$chars[mt_rand(0,63)];return crypt($password,$salt);}
示例:
$hash=blowfishCrypt('password',10); //This creates the hash$hash=blowfishCrypt('password',12); //This creates a more secure hashif(crypt('password',$hash)==$hash){ /*ok*/ } //This checks a password