Jose\JWTCreator::encrypt PHP Method

encrypt() public method

public encrypt ( $payload, array $encryption_protected_headers, Jose\Object\JWKInterface $encryption_key )
$encryption_protected_headers array
$encryption_key Jose\Object\JWKInterface
    public function encrypt($payload, array $encryption_protected_headers, Object\JWKInterface $encryption_key)
    {
        Assertion::true($this->isEncryptionSupportEnabled(), 'The encryption support is not enabled');
        $jwe = Factory\JWEFactory::createJWE($payload, $encryption_protected_headers);
        $jwe = $jwe->addRecipientInformation($encryption_key);
        $this->encrypter->encrypt($jwe);
        return $jwe->toCompactJSON(0);
    }

Usage Example

 /**
  * @param string $jwt
  *
  * @return string
  */
 public function encrypt($jwt)
 {
     $headers = $this->getEncryptionHeaders();
     $encryption_key = $this->encryption_jwkset[0];
     if ($encryption_key->has('kid')) {
         $headers['kid'] = $encryption_key->get('kid');
     }
     return $this->jwt_creator->encrypt($jwt, $headers, $encryption_key);
 }