Jose\Algorithm\KeyEncryption\ECDHES::calculateAgreementKey PHP Method

calculateAgreementKey() public method

public calculateAgreementKey ( Jose\Object\JWKInterface $private_key, Jose\Object\JWKInterface $public_key ) : integer | string | void
$private_key Jose\Object\JWKInterface
$public_key Jose\Object\JWKInterface
return integer | string | void
    public function calculateAgreementKey(JWKInterface $private_key, JWKInterface $public_key)
    {
        switch ($public_key->get('crv')) {
            case 'P-256':
            case 'P-384':
            case 'P-521':
                $p = $this->getGenerator($private_key);
                $rec_x = $this->convertBase64ToGmp($public_key->get('x'));
                $rec_y = $this->convertBase64ToGmp($public_key->get('y'));
                $sen_d = $this->convertBase64ToGmp($private_key->get('d'));
                $priv_key = $p->getPrivateKeyFrom($sen_d);
                $pub_key = $p->getPublicKeyFrom($rec_x, $rec_y);
                $ecdh = new EcDH(EccFactory::getAdapter());
                $ecdh->setSenderKey($priv_key);
                $ecdh->setRecipientKey($pub_key);
                return $this->convertDecToBin($ecdh->calculateSharedKey());
            case 'X25519':
                return curve25519_shared(Base64Url::decode($private_key->get('d')), Base64Url::decode($public_key->get('x')));
            default:
                throw new \InvalidArgumentException(sprintf('The curve "%s" is not supported', $public_key->get('crv')));
        }
    }