get('kty') !== 'oct') { return; } $k = $jwk->get('k'); if (! is_string($k)) { $bag->add(Message::high('The key is not valid')); return; } $k = Base64UrlSafe::decodeNoPadding($k); $strength = self::estimateStrength($k); switch (true) { case $strength < 3: $bag->add( Message::high( 'The octet string is weak and easily guessable. Please change your key as soon as possible.' ) ); break; case $strength === 3: $bag->add(Message::medium('The octet string is safe, but a longer key is preferable.')); break; default: break; } } /** * Returns the estimated strength of a password. * * The higher the value, the stronger the password. * * @return self::STRENGTH_* */ private static function estimateStrength(#[SensitiveParameter] string $password): int { if (! $length = strlen($password)) { return self::STRENGTH_VERY_WEAK; } $password = count_chars($password, 1); $chars = count($password); $control = $digit = $upper = $lower = $symbol = $other = 0; foreach ($password as $chr => $count) { match (true) { $chr < 32 || $chr === 127 => $control = 33, $chr >= 48 && $chr <= 57 => $digit = 10, $chr >= 65 && $chr <= 90 => $upper = 26, $chr >= 97 && $chr <= 122 => $lower = 26, $chr >= 128 => $other = 128, default => $symbol = 33, }; } $pool = $lower + $upper + $digit + $symbol + $control + $other; $entropy = $chars * log($pool, 2) + ($length - $chars) * log($chars, 2); return match (true) { $entropy >= 120 => self::STRENGTH_VERY_STRONG, $entropy >= 100 => self::STRENGTH_STRONG, $entropy >= 80 => self::STRENGTH_MEDIUM, $entropy >= 60 => self::STRENGTH_WEAK, default => self::STRENGTH_VERY_WEAK, }; } }