一个用php实现密码强度检测的类

首先,定义出密码强度的分类:   弱密码:
  1. 长度至少为6个字符

中等强度密码:
  1. 长度至少为6个字符
  2. 不能包含用户的帐户名
  3. 包含密码字符集(数字,小写字母,大写字母,特殊字符)3类字符

高等强度密码:

  1. 长度至少为7个字符
  2. 包含密码字符集中4类字符

为了让密码检测更具有可扩展性,决定按一下方式构建类:

pass_rule.php密码强度规则文件,修改此文件就可以实现不同的密码强度检测方式

define("UNVALID", 0);

define("WEAK", 1);

define("MIDDLE", 2);

define("STRONG", 3);



/*

 * pass rule array(

 *				rule_name => array("type", "param", "judge condition"),

 *				 )

 */



$pass_rules = array(

				"r1" => array("len", "6", ">="),

				"r2" => array("len", "7", ">="),

				"r3" => array("match", "", "exclude"),

				"r4" => array("char_class", "[a-z]", "include"),

				"r5" => array("char_class", "[A-Z]", "include"),

				"r6" => array("char_class", "[0-9]", "include"),

				"r7" => array("char_class", "[~`!@#$%^&*()/-_+={}/[/]|//:;/"'<>,.?//]", "include"),

			  );



//basic conditions of diff level

$pass_levels = array(

				UNVALID => array(),

				WEAK => array("len" => "r1"),

				MIDDLE => array("len" => "r1", "match" => "r3", "char_class" => "3"),

				STRONG => array("len" => "r2", "match" => "r3", "char_class" => "4"),

			   );

pass_policy.php密码策略文件,定义了通常的检测操作,可根据传入的规则进行检测动作
class pass_intensity_policy

{

	private $rules;



	function pass_intensity_policy($rules){

		$this -> rules = $rules;

	}



	function check($pass, $user){

		$result = array("len" => "", "match" => "", "char_class" => "");

		$i = 0;

		foreach ($this -> rules as $k => $v) {

			if ($v[0] == "len") {

				if ($this -> opt_len($pass, $v[1], $v[2]))

					$result["len"] .= $k;

			}

			if ($v[0] == "match") {

				if ($this -> opt_match($pass, $user, $v[2]))

					$result["match"] = $k;

			}

			if ($v[0] == "char_class") {

				if ($this -> opt_char_class($pass, $v[1], $v[2]))

					$result["char_class"] = ++$i;

			}

		}

		return $result;

	}



	function opt_len($str, $length=0, $option){

		if ($option == ">=") {

			if (strlen($str) >= $length)

				return true;

		}

		if ($option == "<=") {

			if (strlen($str) <= $length)

				return true;

		}

		if ($option == "<") {

			if (strlen($str) < $length)

				return true;

		}

		if ($option == ">") {

			if (strlen($str) > $length)

				return true;

		}

		if ($option == "==") {

			if (strlen($str) == $length)

				return true;

		}

		if ($option == "!=") {

			if (strlen($str) != $length)

				return true;

		}

		return false;

	}



	function opt_match($str, $substr, $option){

		if ($option == "exclude") {

			//strpos() is faster than preg_match()

			return !(strpos($str, $substr) !== false);

			//return !preg_match("/$substr/", $str);

		}

		if ($option == "include") {

			return (strpos($str, $substr) !== false);

			//return preg_match("/$substr/", $str);

		}

		if ($option == "equals") {

			return ($str == $substr);

			//return preg_match("/^$substr$/", $str);

		}

		if ($option == "noequals") {

			return ($str != $substr);

			//return !preg_match("/^$substr$/", $str);

		}

		return false;

	}



	function opt_char_class($str, $reg, $option){

		if ($option == "exclude") {

			return !preg_match("/$reg/", $str);

		}

		if ($option == "include") {

			return preg_match("/$reg/", $str);

		}

		return false;

	}

};
p
ass_check.php密码检测类文件,此文件接受规则,生成策略,执行检测,最后返回结果
class pass_intensity_check

{

	private $intensity_policy;

	private $check_result;

	private $stand;

	

	function pass_intensity_check($rules, $stand){

		$this -> intensity_policy = new pass_intensity_policy($rules);

		$this -> stand = $stand;

	}



	function do_check($pass, $user){

		$this -> check_result = $this -> intensity_policy -> check($pass, $user);

	}



	function get_level(){

		//print_r($this -> check_result);



		//decide pass level from 'STRONG' to 'UNVALID'

		if (strpos($this -> check_result["len"], $this -> stand[STRONG]["len"])!==false 

			&& $this -> check_result["match"]==$this -> stand[STRONG]["match"] 

			&& $this -> check_result["char_class"]>=$this -> stand[STRONG]["char_class"]) {

			return STRONG;

		}

		if ((strpos($this -> check_result["len"], $this -> stand[MIDDLE]["len"])!==false 

			|| strpos($this -> check_result["len"], $this -> stand[MIDDLE]["len"])!==false) 

			&& $this -> check_result["match"]==$this -> stand[MIDDLE]["match"] 

			&& $this -> check_result["char_class"]>=$this -> stand[MIDDLE]["char_class"]) {

			return MIDDLE;

		}

		if (strpos($this -> check_result["len"], $this -> stand[WEAK]["len"])!==false 

			|| strpos($this -> check_result["len"], $this -> stand[WEAK]["len"])!==false) {

			return WEAK;

		}

		return UNVALID;

	}

};
    编写此程序的基础是需要了解常规的正则表达式匹配,同时按照面向对象的方法组织,使程序更具扩展性。第一次写文章,不清楚的地方敬请原谅哈

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值