Example #1
0
 public static function calculateAgreement($publicKey, $privateKey)
 {
     if ($publicKey->getType() != $privateKey->getType()) {
         throw new InvalidKeyException('Public and private keys must be of the same type!');
     }
     if ($publicKey->getType() == self::DJB_TYPE) {
         return curve25519_shared($privateKey->getPrivateKey(), $publicKey->getPublicKey());
     } else {
         throw new InvalidKeyException('Unknown type: ' . $publicKey->getType());
     }
 }
Example #2
0
 /**
  * @param \Jose\Object\JWKInterface $private_key
  * @param \Jose\Object\JWKInterface $public_key
  *
  * @throws \InvalidArgumentException
  *
  * @return int|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')));
     }
 }