Example #1
0
File: JWE.php Project: sop/jwx
 /**
  * Initialize by encrypting the given payload.
  *
  * @param string $payload Payload
  * @param KeyManagementAlgorithm $key_algo Key management algorithm
  * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm
  * @param CompressionAlgorithm|null $zip_algo Optional compression algorithm
  * @param Header|null $header Optional desired header. Algorithm specific
  *        parameters are automatically added.
  * @param string|null $cek Optional content encryption key. Randomly
  *        generated if not set.
  * @param string|null $iv Optional initialization vector. Randomly generated
  *        if not set.
  * @throws \RuntimeException If encrypt fails
  * @return self
  */
 public static function encrypt($payload, KeyManagementAlgorithm $key_algo, ContentEncryptionAlgorithm $enc_algo, CompressionAlgorithm $zip_algo = null, Header $header = null, $cek = null, $iv = null)
 {
     // if header was not given, initialize empty
     if (!isset($header)) {
         $header = new Header();
     }
     // generate random CEK
     if (!isset($cek)) {
         $cek = $key_algo->cekForEncryption($enc_algo->keySize());
     }
     // generate random IV
     if (!isset($iv)) {
         $iv = openssl_random_pseudo_bytes($enc_algo->ivSize());
     }
     // compress
     if (isset($zip_algo)) {
         $payload = $zip_algo->compress($payload);
         $header = $header->withParameters(...$zip_algo->headerParameters());
     }
     return self::_encryptContent($payload, $cek, $iv, $key_algo, $enc_algo, $header);
 }