Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 地窖、河豚和散列_Php_Blowfish_Crypt - Fatal编程技术网

Php 地窖、河豚和散列

Php 地窖、河豚和散列,php,blowfish,crypt,Php,Blowfish,Crypt,我打赌已经有关于这个的剧本了,但我创建这个项目只是为了好玩,也是为了测试我的知识,现在我只想听听公众的意见,如果你们能找到我可以改进的方法,请随意分享,也可以发表评论 我的问题只是如何制作一种好的盐。在阅读了手册和几章书后,我想到了这一点。虽然我觉得为了安全起见,我的盐应该更长一些。我应该换什么 这是我的用户类。请检查genSalt函数并指导我找出如何改进结果 <?php if(!defined('ACCESS_CORE')){ echo 'Permission Not G

我打赌已经有关于这个的剧本了,但我创建这个项目只是为了好玩,也是为了测试我的知识,现在我只想听听公众的意见,如果你们能找到我可以改进的方法,请随意分享,也可以发表评论

我的问题只是如何制作一种好的盐。在阅读了手册和几章书后,我想到了这一点。虽然我觉得为了安全起见,我的盐应该更长一些。我应该换什么

这是我的用户类。请检查genSalt函数并指导我找出如何改进结果

    <?php
if(!defined('ACCESS_CORE')){
    echo 'Permission Not Granted';
    exit;
}

class user{
    private $_email;
    private $_pass;
    private $_db;   
    private $_url;


    function __construct(){
        $this->_db = $this->db();
        $this->_url = 'localhost'; //change this to ur url 
        if(isset($_POST['user_login'])){
            $this->_email = $this->clean($_POST['user_email']); //sanitize later
            $this->_pass = $this->clean($_POST['user_password']);
        }
    }

    protected function db(){
        $db = parse_ini_file('../contra.conf');
        $this->_db = new mysqli($db['host'], $db['user'], $db['pass'], $db['name']);
        if ($this->_db->connect_errno) {
         trigger_error("Failed to connect to MySQL".$mysqli->connect_errno). $mysqli->connect_error;
        }   
    }

    protected function clean($string){
        return mysql_real_escape_string($string); #TODO: add more options html etc
    }

    public function safeReferer(){ 
        $ref = (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''); //if there is a ref..
        if(empty($ref) || strpos($ref, $this->_url)){
            return true;
        } else {
            return false;
        }
    }

    public function includeForm($message = ""){ #TODO: finish form view page
         ?>
        <div id="logForm">
            <h3>User Authentication Form</h3>
        <?php  echo ($message === "") ? '' : $message; ?>
                <form id="loginForm" method="post" action="login.php">
                    <input type="text" name="user_email" />
                    <input type="password" name="user_password" />
                    <input type="submit" value="Login" name="user_login" />
                    <a href="/" >Forgot password?</a>
                </form>
            </div>

        <?php ;
    }

    protected function genSalt($length) {  #TODO: improve something is fishy
        $prefix = '$2a$'.$length.'$'; //blowfish prefix
        //base64 unique random alphanumeric
        $uniqRand = base64_encode(mcrypt_create_iv($length, MCRYPT_DEV_URANDOM)); 
        $modified_string = str_replace('+', '.', $uniqRand);  
        $salt = substr($modified_string, 0, $length);
        return $prefix.$salt.'$';
    }


     protected function correctPass($password, $salt){ #TODO: change to prepared statement. best method?
        $sql = "SELECT pass, s FROM users WHERE email = '$this->_email'";
            if($result = $this->_db->query($sql)){
                while ($row = $result->fetch_object()) {
                    if(cript($row['pass'], $row['s']) === $row['s']){
                        return true;
                    } else {
                        return false;
                    }
                }    
            }
    }


    public function login(){
        if($this->correctPass($this->_email, $this->_pass)){
            echo 'create session, session cookie, start timeout, and redirect'; #TODO: copy login, finish page on form view
        } else {
            $message = '<h5>Please try again</h5>';
            $message .= '<p>It looks like you have either entered a wrong user name or password.';
            $this->includeForm($message);
        }
    }

// test function, similar function in register class
   public function createPass($pass){
       $salt = $this->genSalt(10);
       $hash = crypt($pass, $salt);
       echo $salt. '--';
       echo 'hashed pass : '. $hash;
       echo '<br> entered pass : '.$pass.'<br>';
       if(crypt($pass, $hash) == $hash ){
                        echo  'true';
                    } else {
                        echo 'false';
                    }
   }
}

?>
测试功能结果。。。 $2a$10$WlUvRqsgZl$- 散列传递:$2a$10$WlUvRqsgZl$$$$$$$$$。tRNdwECDQXhN07g4mIp82xxFCTUev3m 输入密码:mypassword
真正的

为什么不考虑函数?它也会散列,但每次都会生成一个随机的盐,默认情况下使用河豚。但是,它需要PHP5.5或更高版本。

如果您没有特定的问题,这不是合适的平台。然而,有一件事你可能会混合使用MySQL API,但它们不会混合。返回mysql\u real\u escape\u string$string;好吧,我的问题只是如何用我的脚本创造一个更好的盐,我知道我很接近了。就这些。我刚刚发布了整个文件如果你们中有人想教我一些东西,请引导我。我在这个平台上看到过这种情况。你可以添加:或者,如果PHP<5.5,使用兼容包