* @return string The crypted password */ function auth_cryptPassword($clear,$method='',$salt=''){ global $conf; if(empty($method)) $method = $conf['passcrypt']; //prepare a salt if(empty($salt)) $salt = md5(uniqid(rand(), true)); switch(strtolower($method)){ case 'smd5': return crypt($clear,'$1$'.substr($salt,0,8).'$'); case 'md5': return md5($clear); case 'sha1': return sha1($clear); case 'ssha': $salt=substr($salt,0,4); return '{SSHA}'.base64_encode(pack("H*", sha1($clear.$salt)).$salt); case 'crypt': return crypt($clear,substr($salt,0,2)); case 'mysql': //from http://www.php.net/mysql comment by $nr=0x50305735; $nr2=0x12345671; $add=7; $charArr = preg_split("//", $clear); foreach ($charArr as $char) { if (($char == '') || ($char == ' ') || ($char == '\t')) continue; $charVal = ord($char); $nr ^= ((($nr & 63) + $add) * $charVal) + ($nr << 8); $nr2 += ($nr2 << 8) ^ $nr; $add += $charVal; } return sprintf("%08x%08x", ($nr & 0x7fffffff), ($nr2 & 0x7fffffff)); case 'my411': return '*'.sha1(pack("H*", sha1($clear))); default: msg("Unsupported crypt method $method",-1); } } /** * Verifies a cleartext password against a crypted hash * * The method and salt used for the crypted hash is determined automatically * then the clear text password is crypted using the same method. If both hashs * match true is is returned else false * * @author Andreas Gohr * @return bool */ function auth_verifyPassword($clear,$crypt){ $method=''; $salt=''; //determine the used method and salt $len = strlen($crypt); if(substr($crypt,0,3) == '$1$'){ $method = 'smd5'; $salt = substr($crypt,3,8); }elseif(substr($crypt,0,6) == '{SSHA}'){ $method = 'ssha'; $salt = substr(base64_decode(substr($crypt, 6)),20); }elseif($len == 32){ $method = 'md5'; }elseif($len == 40){ $method = 'sha1'; }elseif($len == 16){ $method = 'mysql'; }elseif($len == 41 && $crypt[0] == '*'){ $method = 'my411'; }else{ $method = 'crypt'; $salt = substr($crypt,0,2); } //crypt and compare if(auth_cryptPassword($clear,$method,$salt) === $crypt){ return true; } return false; } //Setup VIM: ex: et ts=4 enc=utf-8 : ?>