- md5
- 5f50948fe11e5f5d45566b0712d0e1c9
- sha1
- 6b207f7423df630fcc655e45d450823dff6e459f
- sha256
- 4ce5dde0475508471d719dd55e7b1c9ea24755d3c2109d399290cf052032007c
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
<?php
declare(strict_types=1);
namespace Jose\Component\KeyManagement\Analyzer;
use Jose\Component\Core\JWK;
use Jose\Component\Core\Util\Base64UrlSafe;
use Throwable;
use ZxcvbnPhp\Zxcvbn;
use function is_string;
final class ZxcvbnKeyAnalyzer implements KeyAnalyzer
{
public function analyze(JWK $jwk, MessageBag $bag): void
{
if ($jwk->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);
if (! class_exists(Zxcvbn::class)) {
return;
}
$zxcvbn = new Zxcvbn();
try {
$strength = $zxcvbn->passwordStrength($k);
switch (true) {
case $strength['score'] < 3:
$bag->add(
Message::high(
'The octet string is weak and easily guessable. Please change your key as soon as possible.'
)
);
break;
case $strength['score'] === 3:
$bag->add(Message::medium('The octet string is safe, but a longer key is preferable.'));
break;
default:
break;
}
} catch (Throwable) {
$bag->add(Message::medium('The test of the weakness cannot be performed.'));
}
}
}
The same content is in 117 releases, Joomla 5.2.0-beta1 to 6.3.0-alpha1. See them all, or every version of this file.