PassHash.php 3.03 KB
<?php

namespace Hood\Utils;
class PassHash
{

    /**
     * 生成密码
     * @param $pass
     * @return string
     */
    public static function makePass($pass)
    {
        $salt = self::genSalt();
        return self::getCrypted($pass, $salt) . ':' . $salt;
    }

    /**
     * 获取干扰码
     * @param int $length
     * @return string
     */
    public static function genSalt($length = 32)
    {
        $salt = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $saltLen = strlen($salt);
        $makePass = '';
        for ($i = 0; $i < $length; $i++) {
            $makePass .= $salt[mt_rand(0, $saltLen - 1)];
        }
        return $makePass;
    }

    /**
     * 验证密码
     * @param $inputPassword
     * @param $password
     * @return bool
     */
    public static function authPassword($inputPassword, $password)
    {
        if (empty($password) || empty($inputPassword)) {
            return false;
        }
        $passwordList = explode(':', trim($password));
        if (count($passwordList) != 2) {
            return false;
        }
        list($crypt, $salt) = $passwordList;
        $decode = self::getCrypted($inputPassword, $salt);
        if ($crypt != $decode) {
            return false;
        }
        return true;
    }

    public static function getCrypted($str, $salt = null)
    {
        return md5(crypt($str, $salt));
    }


    private static $algo = '$2a';

    private static $cost = '$10';

    public static function uniqueSalt()
    {
        return substr(sha1(mt_rand()), 0, 22);
    }

    /**
     * hash加密
     * @param $password
     * @return string
     */
    public static function hash($password)
    {
        return crypt($password,
            self::$algo .
            self::$cost .
            '$' . self::uniqueSalt());
    }

    /**
     * hash验证输入密码
     * @param $hash
     * @param $password
     * @return bool
     */
    public static function checkPassword($hash, $password)
    {
        $full_salt = substr($hash, 0, 29);
        $new_hash = crypt($password, $full_salt);
        return ($hash == $new_hash);
    }

    /**
     * 双向加密
     * @param $pass
     * @return string
     */
    public static function makeHashPass($pass)
    {
        $salt = self::hash($pass);
        return self::getCrypted($pass, $salt) . ':' . $salt;
    }

    /**
     * 双向验证
     * @param $inputPassword
     * @param $checkPass
     * @return bool
     */
    public static function checkHashPass($inputPassword, $checkPass)
    {
        if (empty($checkPass) || empty($inputPassword)) {
            return false;
        }
        $passwordList = explode(':', trim($checkPass));
        if (count($passwordList) != 2) {
            return false;
        }
        list($crypt, $salt) = $passwordList;
        $decode = self::getCrypted($inputPassword, $salt);

        $hashCheck = self::checkPassword($salt, $inputPassword);
        if (($crypt == $decode) && $hashCheck == true) {
            return true;
        }
        return false;
    }
}