/**
  * Estimates the entropy of the given string.
  * Uses a simple entropy approach. H = L * log_2(N) | L = length, N = possible symbols.
  *
  * @see https://pthree.org/2011/03/07/strong-passwords-need-entropy/ Strong Passwords NEED Entropy by Aaron Toponce
  * @param string $str The string to estimate the entropy of.
  * @return int Returns the estimated entropy in number of bits.
  * @since 0.2
  */
 public function estimateEntropy(string $str) : int
 {
     $spatial = Entropy::spatialDimension($str);
     return (int) floor(Sikker::strlen($str) * log($spatial, 2));
 }
Example #2
0
 /**
  * Gets the spatial dimension of the given string.
  *
  * @param string $str The string toi get the spatial dimension of.
  * @return int Returns the spatial dimension.
  * @since 0.2
  */
 public static function spatialDimension(string $str) : int
 {
     $spatial = 0;
     $charClasses = [Entropy::CHAR_CLASS_DIGITS, Entropy::CHAR_CLASS_LOWERCASE_ASCII, Entropy::CHAR_CLASS_UPPERCASE_ASCII, Entropy::CHAR_CLASS_SYMBOLS_ASCII];
     foreach ($charClasses as $charClass) {
         if (preg_match('/[' . preg_quote($charClass, '/') . ']/', $str) === 1) {
             $spatial += Sikker::strlen($charClass);
         }
     }
     return $spatial;
 }