pub fn encrypt<K: Serialize + DeserializeOwned>(
        &self,
        key: &jwk::JWK<K>,
        options: &EncryptionOptions,
    ) -> Result<Self, Error> {
        match *self {
            Compact::Encrypted(_) => Err(Error::UnsupportedOperation),
            Compact::Decrypted {
                ref header,
                ref payload,
            } => {
                use std::borrow::Cow;

                // Resolve encryption option
                let (key_option, content_option): (_, Cow<'_, _>) =
                    match header.registered.cek_algorithm {
                        KeyManagementAlgorithm::DirectSymmetricKey => {
                            (jwa::NONE_ENCRYPTION_OPTIONS, Cow::Borrowed(options))
                        }
                        _ => (
                            options,
                            Cow::Owned(
                                header
                                    .registered
                                    .enc_algorithm
                                    .random_encryption_options()?,
                            ),
                        ),
                    };

                // RFC 7516 Section 5.1 describes the steps involved in encryption.
                // From steps 1 to 8, we will first determine the CEK, and then encrypt the CEK.
                let cek = header
                    .registered
                    .cek_algorithm
                    .cek(header.registered.enc_algorithm, key)?;
                let encrypted_cek = header.registered.cek_algorithm.wrap_key(
                    cek.algorithm.octet_key()?,
                    key,
                    key_option,
                )?;
                // Update header
                let mut header = header.clone();
                header.update_cek_algorithm(&encrypted_cek);

                // Steps 9 and 10 involves calculating an initialization vector (nonce) for content encryption. We do
                // this as part of the encryption process later

                // Step 11 involves compressing the payload, which we do not support at the moment
                let payload = payload.to_bytes()?;
                if header.registered.compression_algorithm.is_some() {
                    Err(Error::UnsupportedOperation)?
                }

                // Steps 12 to 14 involves the calculation of `Additional Authenticated Data` for encryption. In
                // our compact example, our header is the AAD.
                let encoded_protected_header = BASE64URL_NOPAD.encode(&header.to_bytes()?);
                // Step 15 involves the actual encryption.
                let encrypted_payload = header.registered.enc_algorithm.encrypt(
                    &payload,
                    encoded_protected_header.as_bytes(),
                    &cek,
                    &content_option,
                )?;

                // Finally create the JWE
                let mut compact = crate::Compact::with_capacity(5);
                compact.push(&header)?;
                compact.push(&encrypted_cek.encrypted)?;
                compact.push(&encrypted_payload.nonce)?;
                compact.push(&encrypted_payload.encrypted)?;
                compact.push(&encrypted_payload.tag)?;

                Ok(Compact::Encrypted(compact))
            }
        }
    }
/// Encrypt an Decrypted JWE.
///
/// You will need to provide a `jwa::EncryptionOptions` that will differ based on your chosen
/// algorithms.
///
/// If your `cek_algorithm` is not `dir` or direct, the options provided will be used to
/// encrypt your content encryption key.
///
/// If your `cek_algorithm` is `dir` or Direct, then the options will be used to encrypt
/// your content directly.
Encrypt an Decrypted JWE.
You will need to provide a `jwa::EncryptionOptions` that will differ based on your chosen
algorithms.

If your `cek_algorithm` is not `dir` or direct, the options provided will be used to
encrypt your content encryption key.

If your `cek_algorithm` is `dir` or Direct, then the options will be used to encrypt
your content directly.
Encrypt an Decrypted JWE . You will need to provide a ` jwa :: EncryptionOptions ` that will differ based on your chosen algorithms . If your ` cek_algorithm ` is not ` dir ` or direct the options provided will be used to encrypt your content encryption key . If your ` cek_algorithm ` is ` dir ` or Direct then the options will be used to encrypt your content directly .
pub fn encrypt<K: Serialize + DeserializeOwned>(
        &self,
        key: &jwk::JWK<K>,
        options: &EncryptionOptions,
    ) -> Result<Self, Error> {
        match *self {
            Compact::Encrypted(_) => Err(Error::UnsupportedOperation),
            Compact::Decrypted {
                ref header,
                ref payload,
            } => {
                use std::borrow::Cow;
                // Resolve encryption option
                let (key_option, content_option): (_, Cow<'_, _>) =
                    match header.registered.cek_algorithm {
                        KeyManagementAlgorithm::DirectSymmetricKey => {
                            (jwa::NONE_ENCRYPTION_OPTIONS, Cow::Borrowed(options))
                        }
                        _ => (
                            options,
                            Cow::Owned(
                                header
                                    .registered
                                    .enc_algorithm
                                    .random_encryption_options()?,
                            ),
                        ),
                    };
                // RFC 7516 Section 5.1 describes the steps involved in encryption.
                // From steps 1 to 8, we will first determine the CEK, and then encrypt the CEK.
                let cek = header
                    .registered
                    .cek_algorithm
                    .cek(header.registered.enc_algorithm, key)?;
                let encrypted_cek = header.registered.cek_algorithm.wrap_key(
                    cek.algorithm.octet_key()?,
                    key,
                    key_option,
                )?;
                // Update header
                let mut header = header.clone();
                header.update_cek_algorithm(&encrypted_cek);
                // Steps 9 and 10 involves calculating an initialization vector (nonce) for content encryption. We do
                // this as part of the encryption process later
                // Step 11 involves compressing the payload, which we do not support at the moment
                let payload = payload.to_bytes()?;
                if header.registered.compression_algorithm.is_some() {
                    Err(Error::UnsupportedOperation)?
                }
                // Steps 12 to 14 involves the calculation of `Additional Authenticated Data` for encryption. In
                // our compact example, our header is the AAD.
                let encoded_protected_header = BASE64URL_NOPAD.encode(&header.to_bytes()?);
                // Step 15 involves the actual encryption.
                let encrypted_payload = header.registered.enc_algorithm.encrypt(
                    &payload,
                    encoded_protected_header.as_bytes(),
                    &cek,
                    &content_option,
                )?;
                // Finally create the JWE
                let mut compact = crate::Compact::with_capacity(5);
                compact.push(&header)?;
                compact.push(&encrypted_cek.encrypted)?;
                compact.push(&encrypted_payload.nonce)?;
                compact.push(&encrypted_payload.encrypted)?;
                compact.push(&encrypted_payload.tag)?;
                Ok(Compact::Encrypted(compact))
            }
        }
    }
pub fn encrypt < K : Serialize + DeserializeOwned > ( & self , key : & jwk :: JWK < K > , options : & EncryptionOptions , ) -> Result < Self , Error > { match * self { Compact :: Encrypted ( _ ) => Err ( Error :: UnsupportedOperation ) , Compact :: Decrypted { ref header , ref payload , } => { use std :: borrow :: Cow ; // Resolve encryption option let ( key_option , content_option ) : ( _ , Cow < ' _ , _ > ) = match header . registered . cek_algorithm { KeyManagementAlgorithm :: DirectSymmetricKey => { ( jwa :: NONE_ENCRYPTION_OPTIONS , Cow :: Borrowed ( options ) ) } _ => ( options , Cow :: Owned ( header . registered . enc_algorithm . random_encryption_options ( ) ? , ) , ) , } ; // RFC 7516 Section 5.1 describes the steps involved in encryption. // From steps 1 to 8, we will first determine the CEK, and then encrypt the CEK. let cek = header . registered . cek_algorithm . cek ( header . registered . enc_algorithm , key ) ? ; let encrypted_cek = header . registered . cek_algorithm . wrap_key ( cek . algorithm . octet_key ( ) ? , key , key_option , ) ? ; // Update header let mut header = header . clone ( ) ; header . update_cek_algorithm ( & encrypted_cek ) ; // Steps 9 and 10 involves calculating an initialization vector (nonce) for content encryption. We do // this as part of the encryption process later // Step 11 involves compressing the payload, which we do not support at the moment let payload = payload . to_bytes ( ) ? ; if header . registered . compression_algorithm . is_some ( ) { Err ( Error :: UnsupportedOperation ) ? } // Steps 12 to 14 involves the calculation of `Additional Authenticated Data` for encryption. In // our compact example, our header is the AAD. let encoded_protected_header = BASE64URL_NOPAD . encode ( & header . to_bytes ( ) ? ) ; // Step 15 involves the actual encryption. let encrypted_payload = header . registered . enc_algorithm . encrypt ( & payload , encoded_protected_header . as_bytes ( ) , & cek , & content_option , ) ? ; // Finally create the JWE let mut compact = crate :: Compact :: with_capacity ( 5 ) ; compact . push ( & header ) ? ; compact . push ( & encrypted_cek . encrypted ) ? ; compact . push ( & encrypted_payload . nonce ) ? ; compact . push ( & encrypted_payload . encrypted ) ? ; compact . push ( & encrypted_payload . tag ) ? ; Ok ( Compact :: Encrypted ( compact ) ) } } }
Encrypt an Decrypted JWE.
Encrypt an Decrypted JWE .
&self
key
&jwk::JWK<K>
options
&EncryptionOptions
&self
key
&jwk::JWK<K>
options
&EncryptionOptions