![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/captainhook/secrets/src/Entropy/ |
<?php /** * This file is part of CaptainHook Secrets. * * (c) Sebastian Feldmann <[email protected]> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ declare(strict_types=1); namespace CaptainHook\Secrets\Entropy; /** * Secret Detector * * @package CaptainHook-Secrets * @author Sebastian Feldmann <[email protected]> * @link https://github.com/captainhookphp/secrets * @since Class available since Release 0.9.1 */ class Shannon { /** * Return the entropy of a given string * * @param string $text * @return float */ public static function entropy(string $text): float { if (empty($text)) { return 0; } $charMap = []; foreach (mb_str_split($text) as $char) { $charMap[$char] = isset($charMap[$char]) ? $charMap[$char] + 1 : 1; } $entropy = 0; $length = strlen($text); foreach ($charMap as $char => $amount) { $p = $amount / $length; $entropy = $entropy - ($p * log($p, 2)); } return $entropy; } }