Last active
October 22, 2015 16:01
-
-
Save Alanaktion/7bf4edae31b1f4d016dc to your computer and use it in GitHub Desktop.
Simple PHP entropy calculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Calculate entropy of a string | |
* | |
* @see https://github.com/jreesuk/entropizer | |
* | |
* @param string $str | |
* @return integer | |
*/ | |
function entropizer($str) { | |
$classes = array( | |
array("regex" => "/[a-z]/", "size" => 26), | |
array("regex" => "/[A-Z]/", "size" => 26), | |
array("regex" => "/[0-9]/", "size" => 10), | |
" ,.?!\"£$%^&*()-_=+[]{};:\'@#~<>/\\|`¬¦" | |
); | |
$size = 0; | |
$str = trim($str); | |
foreach($classes as $case) { | |
if(is_array($case)) { | |
if(preg_match($case["regex"], $str)) { | |
$size += $case["size"]; | |
} | |
} else { | |
foreach(str_split($case, 1) as $char) { | |
if(strpos($str, $char) !== false) { | |
$size += strlen($case); | |
break; | |
} | |
} | |
} | |
} | |
return floor(log($size, 2) * strlen($str)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment