mirror of
https://git.code.sf.net/p/seeddms/code
synced 2025-02-06 15:14:58 +00:00
reformating of code
more steps in simple password strength calculation
This commit is contained in:
parent
da33fb1de7
commit
93ff0f13aa
|
@ -22,11 +22,10 @@
|
||||||
* *
|
* *
|
||||||
*************************************************************************/
|
*************************************************************************/
|
||||||
|
|
||||||
class Password_Strength
|
class Password_Strength {
|
||||||
{
|
private $class_name = "password strength";
|
||||||
private $class_name = "Password Strength";
|
|
||||||
private $class_version = "1.0.0";
|
private $class_version = "1.0.0";
|
||||||
private $class_author = "Wolf Software";
|
private $class_author = "wolf software";
|
||||||
private $class_source = "http://www.wolf-software.com/downloads/php-classes/security-classes/password-strength-class/";
|
private $class_source = "http://www.wolf-software.com/downloads/php-classes/security-classes/password-strength-class/";
|
||||||
|
|
||||||
private $password = '';
|
private $password = '';
|
||||||
|
@ -34,70 +33,66 @@ class Password_Strength
|
||||||
private $password_length = 0;
|
private $password_length = 0;
|
||||||
private $score_precision = 2;
|
private $score_precision = 2;
|
||||||
|
|
||||||
public function class_name()
|
public function class_name() {
|
||||||
{
|
|
||||||
return $this->class_name;
|
return $this->class_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function class_version()
|
public function class_version() {
|
||||||
{
|
|
||||||
return $this->class_version;
|
return $this->class_version;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function class_author()
|
public function class_author() {
|
||||||
{
|
|
||||||
return $this->class_author;
|
return $this->class_author;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function class_source()
|
public function class_source() {
|
||||||
{
|
|
||||||
return $this->class_source;
|
return $this->class_source;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __construct()
|
public function __construct() {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function simple_calculate()
|
public function simple_calculate() {
|
||||||
{
|
|
||||||
$password = $this->password;
|
$password = $this->password;
|
||||||
if($this->password_length < 8 ||
|
$score = 0;
|
||||||
!preg_match('/[0-9]+/', $password) ||
|
if(preg_match('/[0-9]+/', $password))
|
||||||
!preg_match('/[a-z]+/', $password) ||
|
$score += 25;
|
||||||
!preg_match('/[A-Z]+/', $password) ||
|
if(preg_match('/[a-z]+/', $password))
|
||||||
!preg_match('/[^0-9a-zA-Z]+/', $password))
|
$score += 25;
|
||||||
{
|
if(preg_match('/[A-Z]+/', $password))
|
||||||
|
$score += 25;
|
||||||
|
if(preg_match('/[^0-9a-zA-Z]+/', $password))
|
||||||
|
$score += 25;
|
||||||
|
if($this->password_length < 8)
|
||||||
|
$score *= ($this->password_length/8);
|
||||||
|
|
||||||
|
$this->password_info['total_score'] = $score;
|
||||||
|
$this->password_info['rating_score'] = $score;
|
||||||
|
$this->password_info['rating'] = $this->get_score_info($score);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function calculate() {
|
||||||
|
$this->password_info = array();
|
||||||
|
|
||||||
|
$this->password_info['password'] = $this->password;
|
||||||
|
$this->password_info['password_length'] = $this->password_length;
|
||||||
|
if($this->password_length == 0) {
|
||||||
$this->password_info['total_score'] = 0;
|
$this->password_info['total_score'] = 0;
|
||||||
$this->password_info['rating_score'] = 0;
|
$this->password_info['rating_score'] = 0;
|
||||||
$this->password_info['rating'] = 'Insufficient';
|
$this->password_info['rating'] = 'Very Bad';
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
$this->password_info['total_score'] = 100;
|
|
||||||
$this->password_info['rating_score'] = 100;
|
|
||||||
$this->password_info['rating'] = 'Good';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function calculate()
|
|
||||||
{
|
|
||||||
$this->password_info = array();
|
|
||||||
|
|
||||||
$this->calculate_length();
|
$this->calculate_length();
|
||||||
$this->calculate_complexity();
|
$this->calculate_complexity();
|
||||||
$this->calculate_charset_complexity();
|
$this->calculate_charset_complexity();
|
||||||
$this->calculate_entropy();
|
$this->calculate_entropy();
|
||||||
|
|
||||||
$this->password_info['password'] = $this->password;
|
|
||||||
$this->password_info['password_length'] = $this->password_length;
|
|
||||||
|
|
||||||
$total = 0;
|
$total = 0;
|
||||||
$scoreCount = 0;
|
$scoreCount = 0;
|
||||||
$keys = array_keys($this->password_info['details']);
|
$keys = array_keys($this->password_info['details']);
|
||||||
foreach ($keys as $key)
|
foreach ($keys as $key) {
|
||||||
{
|
if (preg_match('/score+$/', $key)) {
|
||||||
if (preg_match('/score+$/', $key))
|
|
||||||
{
|
|
||||||
$total += intval($this->password_info['details'][$key]);
|
$total += intval($this->password_info['details'][$key]);
|
||||||
$scoreCount ++;
|
$scoreCount ++;
|
||||||
}
|
}
|
||||||
|
@ -113,29 +108,24 @@ class Password_Strength
|
||||||
ksort($this->password_info['details']);
|
ksort($this->password_info['details']);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get_all_info()
|
public function get_all_info() {
|
||||||
{
|
|
||||||
return $this->password_info;
|
return $this->password_info;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get_score()
|
public function get_score() {
|
||||||
{
|
|
||||||
return $this->password_info['rating_score'];
|
return $this->password_info['rating_score'];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get_rating()
|
public function get_rating() {
|
||||||
{
|
|
||||||
return $this->password_info['rating'];
|
return $this->password_info['rating'];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function set_password($password)
|
public function set_password($password) {
|
||||||
{
|
|
||||||
$this->password = $password;
|
$this->password = $password;
|
||||||
$this->password_length = strlen($password);
|
$this->password_length = strlen($password);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function calculate_charset_complexity()
|
private function calculate_charset_complexity() {
|
||||||
{
|
|
||||||
$password = $this->password;
|
$password = $this->password;
|
||||||
$len = strlen($password);
|
$len = strlen($password);
|
||||||
|
|
||||||
|
@ -144,182 +134,118 @@ class Password_Strength
|
||||||
$different_count = 0;
|
$different_count = 0;
|
||||||
$score = 0;
|
$score = 0;
|
||||||
|
|
||||||
if ($len <= 3)
|
if ($len <= 3) {
|
||||||
{
|
|
||||||
$score = 2;
|
$score = 2;
|
||||||
}
|
} else {
|
||||||
else
|
for ($i = 0; $i < $len; $i++) {
|
||||||
{
|
|
||||||
for ($i = 0; $i < $len; $i++)
|
|
||||||
{
|
|
||||||
$char = substr($password, $i, 1);
|
$char = substr($password, $i, 1);
|
||||||
if ($i > 0)
|
if ($i > 0) {
|
||||||
{
|
|
||||||
$last_char = substr($password, $i - 1, 1);
|
$last_char = substr($password, $i - 1, 1);
|
||||||
}
|
}
|
||||||
if ($char != $last_char)
|
if ($char != $last_char) {
|
||||||
{
|
|
||||||
$different_count++;
|
$different_count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($len <= 5)
|
if ($len <= 5) {
|
||||||
{
|
|
||||||
$score = 10;
|
$score = 10;
|
||||||
}
|
} elseif ($different_count == 1) {
|
||||||
else if ($different_count == 1)
|
|
||||||
{
|
|
||||||
$score = 1;
|
$score = 1;
|
||||||
$this->password_info['details']['length_score'] = min(min(floor(10 * $this->password_length / 10), 20), $this->password_info['details']['length_score']);
|
$this->password_info['details']['length_score'] = min(min(floor(10 * $this->password_length / 10), 20), $this->password_info['details']['length_score']);
|
||||||
}
|
} elseif ($different_count == 2) {
|
||||||
else if ($different_count == 2)
|
|
||||||
{
|
|
||||||
$score = 5;
|
$score = 5;
|
||||||
$this->password_info['details']['length_score'] = min(min(floor(20 * $this->password_length / 10), 40), $this->password_info['details']['length_score']);
|
$this->password_info['details']['length_score'] = min(min(floor(20 * $this->password_length / 10), 40), $this->password_info['details']['length_score']);
|
||||||
}
|
} elseif ($different_count == 3) {
|
||||||
else if ($different_count == 3)
|
|
||||||
{
|
|
||||||
$score = 10;
|
$score = 10;
|
||||||
$this->password_info['details']['length_score'] = min(min(floor(30 * $this->password_length / 10), 50), $this->password_info['details']['length_score']);
|
$this->password_info['details']['length_score'] = min(min(floor(30 * $this->password_length / 10), 50), $this->password_info['details']['length_score']);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
$score = round(max($this->password_info['details']['length_score'] / 10, $different_count / $len * 100), $this->score_precision);
|
$score = round(max($this->password_info['details']['length_score'] / 10, $different_count / $len * 100), $this->score_precision);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->password_info['details']['charset_complexity_score'] = $score;
|
$this->password_info['details']['charset_complexity_score'] = $score;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function calculate_complexity()
|
private function calculate_complexity() {
|
||||||
{
|
|
||||||
$password = $this->password;
|
$password = $this->password;
|
||||||
$score = 0;
|
$score = 0;
|
||||||
|
|
||||||
if (preg_match('/^([0-9]+)+$/', $password))
|
if (preg_match('/^([0-9]+)+$/', $password)) {
|
||||||
{
|
|
||||||
$score = 10;
|
$score = 10;
|
||||||
$this->password_info['details']['charset'] = 'numeric';
|
$this->password_info['details']['charset'] = 'numeric';
|
||||||
}
|
} elseif (preg_match('/^([a-z]+)+$/', $password)) {
|
||||||
else if (preg_match('/^([a-z]+)+$/', $password))
|
|
||||||
{
|
|
||||||
$score = 30;
|
$score = 30;
|
||||||
$this->password_info['details']['charset'] = 'alphabetic';
|
$this->password_info['details']['charset'] = 'alphabetic';
|
||||||
}
|
} elseif (preg_match('/^([a-z0-9]+)+$/i', $password)) {
|
||||||
else if (preg_match('/^([a-z0-9]+)+$/i', $password))
|
if ((preg_match('/^([a-z]+)([0-9]+)+$/i', $password, $match)) || (preg_match('/^([0-9]+)([a-z]+)+$/i', $password, $match))) {
|
||||||
{
|
|
||||||
if ((preg_match('/^([a-z]+)([0-9]+)+$/i', $password, $match)) || (preg_match('/^([0-9]+)([a-z]+)+$/i', $password, $match)))
|
|
||||||
{
|
|
||||||
$alpha = $match[1];
|
$alpha = $match[1];
|
||||||
$numeric = $match[2];
|
$numeric = $match[2];
|
||||||
$numeric_length = strlen($numeric);
|
$numeric_length = strlen($numeric);
|
||||||
|
|
||||||
if (($numeric == 111) || ($numeric == 123))
|
if (($numeric == 111) || ($numeric == 123)) {
|
||||||
{
|
if (preg_match('/^([a-z]+)([0-9]+)+$/i', $password, $match)) {
|
||||||
if (preg_match('/^([a-z]+)([0-9]+)+$/i', $password, $match))
|
|
||||||
{
|
|
||||||
$score = 31;
|
$score = 31;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
$score = 35;
|
$score = 35;
|
||||||
}
|
}
|
||||||
$this->password_info['details']['common_numeric'] = true;
|
$this->password_info['details']['common_numeric'] = true;
|
||||||
}
|
} elseif ($numeric_length == 1) {
|
||||||
else if ($numeric_length == 1)
|
|
||||||
{
|
|
||||||
$score = 30;
|
$score = 30;
|
||||||
}
|
} elseif ($numeric_length <= 3) {
|
||||||
else if ($numeric_length <= 3)
|
|
||||||
{
|
|
||||||
$score = 35;
|
$score = 35;
|
||||||
}
|
} elseif ($numeric_length <= 5) {
|
||||||
else if ($numeric_length <= 5)
|
|
||||||
{
|
|
||||||
$score = 40;
|
$score = 40;
|
||||||
}
|
} elseif ($numeric_length <= 10) {
|
||||||
else if ($numeric_length <= 10)
|
|
||||||
{
|
|
||||||
$score = 50;
|
$score = 50;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
$score = 60;
|
$score = 60;
|
||||||
}
|
}
|
||||||
$this->password_info['details']['charset'] = 'alphanumeric';
|
$this->password_info['details']['charset'] = 'alphanumeric';
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
$score = 80;
|
$score = 80;
|
||||||
$this->password_info['details']['charset'] = 'alphanumeric';
|
$this->password_info['details']['charset'] = 'alphanumeric';
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
$score = 100;
|
$score = 100;
|
||||||
$this->password_info['details']['charset'] = 'alphanumeric + others';
|
$this->password_info['details']['charset'] = 'alphanumeric + others';
|
||||||
}
|
}
|
||||||
$this->password_info['details']['charset_score'] = $score;
|
$this->password_info['details']['charset_score'] = $score;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function calculate_length()
|
private function calculate_length() {
|
||||||
{
|
|
||||||
$len = $this->password_length;
|
$len = $this->password_length;
|
||||||
$score = 0;
|
$score = 0;
|
||||||
|
|
||||||
if ($len == 0)
|
if ($len == 0) {
|
||||||
{
|
|
||||||
$score = 0;
|
$score = 0;
|
||||||
}
|
} elseif ($len <= 3) {
|
||||||
else if ($len <= 3)
|
|
||||||
{
|
|
||||||
$score = 1;
|
$score = 1;
|
||||||
}
|
} elseif ($len <= 4) {
|
||||||
else if ($len <= 4)
|
|
||||||
{
|
|
||||||
$score = 2;
|
$score = 2;
|
||||||
}
|
} elseif ($len <= 5) {
|
||||||
else if ($len <= 5)
|
|
||||||
{
|
|
||||||
$score = 10;
|
$score = 10;
|
||||||
}
|
} elseif ($len <= 6) {
|
||||||
else if ($len <= 6)
|
|
||||||
{
|
|
||||||
$score = 20;
|
$score = 20;
|
||||||
}
|
} elseif ($len <= 8) {
|
||||||
else if ($len <= 8)
|
|
||||||
{
|
|
||||||
$score = 30;
|
$score = 30;
|
||||||
}
|
} elseif ($len <= 10) {
|
||||||
else if ($len <= 10)
|
|
||||||
{
|
|
||||||
$score = 45;
|
$score = 45;
|
||||||
}
|
} elseif ($len <= 15) {
|
||||||
else if ($len <= 15)
|
|
||||||
{
|
|
||||||
$score = 75;
|
$score = 75;
|
||||||
}
|
} elseif ($len <= 18) {
|
||||||
else if ($len <= 18)
|
|
||||||
{
|
|
||||||
$score = 80;
|
$score = 80;
|
||||||
}
|
} elseif ($len <= 20) {
|
||||||
else if ($len <= 20)
|
|
||||||
{
|
|
||||||
$score = 90;
|
$score = 90;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
$score = 100;
|
$score = 100;
|
||||||
}
|
}
|
||||||
$this->password_info['details']['length_score'] = $score;
|
$this->password_info['details']['length_score'] = $score;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function calculate_entropy()
|
private function calculate_entropy() {
|
||||||
{
|
|
||||||
$score = 0;
|
$score = 0;
|
||||||
$password = $this->password;
|
$password = $this->password;
|
||||||
$length = $this->password_length;
|
$length = $this->password_length;
|
||||||
|
|
||||||
foreach (count_chars($password, 1) as $v)
|
foreach (count_chars($password, 1) as $v) {
|
||||||
{
|
|
||||||
$p = $v / $length;
|
$p = $v / $length;
|
||||||
$score -= $p * log($p)/log(2);
|
$score -= $p * log($p)/log(2);
|
||||||
}
|
}
|
||||||
|
@ -327,38 +253,22 @@ class Password_Strength
|
||||||
$this->password_info['details']['entropy_score'] = round(($score * $length), $this->score_precision);
|
$this->password_info['details']['entropy_score'] = round(($score * $length), $this->score_precision);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function get_score_info($score)
|
private function get_score_info($score) {
|
||||||
{
|
if ($score <= 15) {
|
||||||
if ($score <= 15)
|
|
||||||
{
|
|
||||||
$score_info = 'Very Bad';
|
$score_info = 'Very Bad';
|
||||||
}
|
} elseif ($score <= 35) {
|
||||||
else if ($score <= 35)
|
|
||||||
{
|
|
||||||
$score_info = 'Bad';
|
$score_info = 'Bad';
|
||||||
}
|
} elseif ($score <= 45) {
|
||||||
else if ($score <= 45)
|
|
||||||
{
|
|
||||||
$score_info = 'Medium - Bad';
|
$score_info = 'Medium - Bad';
|
||||||
}
|
} elseif ($score <= 55) {
|
||||||
else if ($score <= 55)
|
|
||||||
{
|
|
||||||
$score_info = 'Medium';
|
$score_info = 'Medium';
|
||||||
}
|
} elseif ($score <= 65) {
|
||||||
else if ($score <= 65)
|
|
||||||
{
|
|
||||||
$score_info = 'Medium - Good';
|
$score_info = 'Medium - Good';
|
||||||
}
|
} elseif ($score <= 75) {
|
||||||
else if ($score <= 75)
|
|
||||||
{
|
|
||||||
$score_info = 'Good';
|
$score_info = 'Good';
|
||||||
}
|
} elseif ($score <= 90) {
|
||||||
else if ($score <= 90)
|
|
||||||
{
|
|
||||||
$score_info = 'Very Good';
|
$score_info = 'Very Good';
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
$score_info = 'Excellent';
|
$score_info = 'Excellent';
|
||||||
}
|
}
|
||||||
return $score_info;
|
return $score_info;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user