hash - PHP Compare a crypted password from db with an inserted password from a form -


i've db crypted password. when user logs in, make this:

$result = mysqli_fetch_assoc(mysqli_query($conn,$query)); $cryptedpass = $result['password']; $pass = $_post['password']; if(strcmp($cryptedpass,md5($pass))==0)    echo "yeah!"; 

it works, know if right manner, or if there of safer!

don't use md5. there plenty of online documents explain how insecure is. example:

https://en.wikipedia.org/wiki/md5

i recommend using crypt() function.

read here: http://php.net/crypt

a 1 use crypt_blowfish

here's function found while back, use. unfortunately can't remember found it, can't reference author.

function blowfishencrypt($string,$rounds) {         $salt = "";         $saltcharacters = array_merge(range('a','z'),range('a','z'),range(0,9));         ($i=0;$i<22;$i++) {             $salt .= $saltcharacters[array_rand($saltcharacters)];         }         $hashstring = crypt($string,'$2y$' . $rounds . '$' . $salt);          return $hashstring;     } 

to create encrypted password, use so:

$cryptedpass=blowfishencrypt($clearpass,'07'); 

then compare, use:

if($cryptedpass==crypt($pass,$cryptedpass)) {     echo 'yeah!'; } 

note: if using version of php before 5.3.7, salt prefix must $2a$.

php 5.3.7 introduced new prefix $2y$ fix security weakness in blowfish implementation.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -