Rumah >php教程 >PHP源码 >一种密码复杂度的简单计算方式

一种密码复杂度的简单计算方式

PHP中文网
PHP中文网asal
2016-05-25 17:08:351189semak imbas


 1, 2, 3, 4);
	private static $typeChange = array(array(1, 2), array(1, 3), array(1, 4), array(2, 3), array(2, 4), array(3, 4));
	private static $typeChangeComplexs = array(1, 1, 2, 1, 2, 2);
	
	public static function computeComplex($password)
	{
		$complex = 0;
		$password = (string)$password;
		$length = strlen($password);
		
		$prefixType = 0;
		$regularChars = array();
		for($i = 0; $i < $length; $i++){
			if(ctype_digit($password[$i])){
				$type = 1;
			}elseif(ctype_lower($password[$i])){
				$type = 2;
			}elseif(ctype_upper($password[$i])){
				$type = 3;
			}else{
				$type = 4;
			}

			if(!in_array($password[$i], $regularChars)){ //规律性字符,不计入复杂度
				$complex += self::$typeComplexs[$type];
				if($prefixType){
					$changeType = array($type, $prefixType);
					sort($changeType);
					$change = array_search($changeType, self::$typeChange);
					if($change !== false){
						$complex += self::$typeChangeComplexs[$change];
					}
				}
			}

			$prefixType = $type;
			$regularChars = self::getRegularChars($password[$i], $prefixType);
		}
		
		return $complex;
	}
	
	/**
	 * 获取一个字符的规律字符集
	 * 下列规则被认为是有规则的
	 * 1)重复字符
	 * 2)数字递增或者递减
	 * 3)字母递增或者递减
	 * @param unknown_type $prefixChar
	 */
	private static function getRegularChars($prefix, $prefixType)
	{
		$regularChars = array($prefix);
		
		switch($prefixType){
			case 1:
				$regularChars[] = $prefix + 1;
				$regularChars[] = $prefix - 1;
				break;
				
			case 2:
			case 3:
				$regularChars[] = chr(ord($prefix) + 1);
				$regularChars[] = chr(ord($prefix) - 1);
				$regularChars = array_filter($regularChars, &#39;ctype_alnum&#39;);
				break;
		}
		
		return $regularChars;
	}
}
?>

                   

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:百度网盘文件直链Artikel seterusnya:EasySite FireWall 防火墙模块