CoreFiles by mySites.guru
RSA.php49 lines, 1,249 bytes, from Joomla_6.2.0-Stable-Full_Package.zip
Raw History Compare with 6.1.4
md5
4bb503bbe62f0b8fccc05a2bd1456175
sha1
b65edc15cfacaf41010d1001721ce6f109351fb8
sha256
2133e87df8460ee5575c673171d2e1b8aecf304b5b672a6a5e1bd298d1d9dcf1
<?php

declare(strict_types=1);

namespace Cose\Algorithm\Signature\RSA;

use Cose\Algorithm\Signature\Signature;
use Cose\Key\Key;
use Cose\Key\RsaKey;
use InvalidArgumentException;
use function openssl_sign;
use function openssl_verify;
use Throwable;

/**
 * @see \Cose\Tests\Algorithm\Signature\RSA\RSATest
 */
abstract class RSA implements Signature
{
    public function sign(string $data, Key $key): string
    {
        $key = $this->handleKey($key);
        if (! $key->isPrivate()) {
            throw new InvalidArgumentException('The key is not private.');
        }

        try {
            openssl_sign($data, $signature, $key->asPem(), $this->getHashAlgorithm());
        } catch (Throwable $e) {
            throw new InvalidArgumentException('Unable to sign the data', 0, $e);
        }

        return $signature;
    }

    public function verify(string $data, Key $key, string $signature): bool
    {
        $key = $this->handleKey($key);

        return openssl_verify($data, $signature, $key->toPublic()->asPem(), $this->getHashAlgorithm()) === 1;
    }

    abstract protected function getHashAlgorithm(): int;

    private function handleKey(Key $key): RsaKey
    {
        return RsaKey::create($key->getData());
    }
}

The same content is in 9 releases, Joomla 6.2.0-alpha2 to 6.2.1-rc1. See them all, or every version of this file.