Skip to content

Instantly share code, notes, and snippets.

@Alanaktion
Last active October 22, 2015 16:01
Show Gist options
  • Save Alanaktion/7bf4edae31b1f4d016dc to your computer and use it in GitHub Desktop.
Save Alanaktion/7bf4edae31b1f4d016dc to your computer and use it in GitHub Desktop.
Simple PHP entropy calculator
<?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