vmailadmin/app/Entropy.php

60 lines
1.4 KiB
PHP
Raw Permalink Normal View History

2020-05-23 15:20:01 +02:00
<?php
namespace App;
class Entropy
{
public static function CharPoolSize($password)
{
$n_options = 0;
if(preg_match("/[0-9]+/",$password)) // check numbers
$n_options += 10;
if(preg_match("/[a-z]+/",$password)) // check lowercase
$n_options += 26;
if(preg_match("/[A-Z]+/",$password)) // check
$n_options += 26;
if(preg_match("/[\W_ ]+/",$password)) // check specialchars
$n_options += 32;
return $n_options;
}
public static function CountUniqueChars($str)
{
$characters = array();
$count = 0;
$length= strlen($str);
//loop, figure it out
for($x = 0; $x < $length; $x++)
{
$c = $str[$x];
if(!in_array($c,$characters))
{
$characters[] = $c;
$count += 1;
}
}
return $count;
}
public static function Calculate($password,$base=false)
{
// first we check which sets of characters are available in the password
if($base === false || !is_numeric($base))
$base = static::CharPoolSize($password);
return static::CalculateEntropy(strlen($password),$base);
}
public static function CalculateEntropy($length,$poolsize)
{
if($poolsize == 1)
return log($length,2);
else
return $length * log($poolsize,2);
}
}