php cryptr 加密函数

Pillar

Pillar

发表于 2017-01-09 12:37:06
内容来源: 网络

 

class CryptHelper {
    /**
     * 加密
     * @param unknown $password
     * @param unknown $salt
     * @return string
     */
    public static function crypt($password,$salt){
        // $saltPrefix .= '$2y$'; // Blowfish 算法
        // $saltPrefix .= '13'; // 两位 cost 参数
        // $saltPrefix .= '$'; // 一个 $
        // $saltSuffix .= 'LGsF2ctmKKHE1yr2Py.vtu';
        return crypt($password, $salt);
    }
    
    /**
     * 字符比较
     * @param unknown $expected
     * @param unknown $actual
     * @return boolean
     */
    public static function compareString($expected, $actual)
    {
        $expected .= "";
        $actual .= "";
        $expectedLength = mb_strlen($expected, '8bit');
        $actualLength = mb_strlen($actual, '8bit');
        $diff = $expectedLength - $actualLength;
        for ($i = 0; $i < $actualLength; $i++) {
            $diff |= (ord($actual[$i]) ^ ord($expected[$i % $expectedLength]));
        }
        return $diff === 0;
    }
    
    /**
     * 校验密码
     * @param unknown $password
     * @param unknown $hash
     * @return boolean
     */
    public static function validatePassword($password, $hash)
    {
        if (!is_string($password) || $password === '') {
            return false;
        }
        
        if (!preg_match('/^$2[axy]$(dd)$[./0-9A-Za-z]{22}/', $hash, $matches)
            || $matches[1] < 4
            || $matches[1] > 30
        ) {
            return false;
        }
        
        $test = self::crypt($password, $hash);
        $n = strlen($test);
        if ($n !== 60) {
            return false;
        }
        return self::compareString($test, $hash);
    }
}

 

测试

// --------- 测试 -------- 
// 加密
$salt = '$2y$13$LGsF2ctmKKHE1yr2Py.vtu'; // 7 + 22 == 29
$password = 'Aa123456';
echo CryptHelper::crypt($password,$salt);
echo PHP_EOL;

// 校验
$hash = '$2y$13$LGsF2ctmKKHE1yr2Py.vtuiUR/A0C6tARkCxMO.LUlsiRISu7u53m';
echo CryptHelper::crypt($password,$hash);
echo PHP_EOL;

echo strlen($hash);
echo PHP_EOL;

echo CryptHelper::validatePassword($password, $hash);

 

内容来源:http://www.cnblogs.com/xiaoyaogege/p/6256570.html

用户评论
开源开发学习小组列表