pub fn serialize_compact_with_selector<'a, F>(
        &self,
        payload: &[u8],
        header: &JweHeader,
        selector: F,
    ) -> Result<String, JoseError>
    where
        F: Fn(&JweHeader) -> Option<&'a dyn JweEncrypter>,
    {
        (|| -> anyhow::Result<String> {
            let encrypter = match selector(header) {
                Some(val) => val,
                None => bail!("A encrypter is not found."),
            };

            let cencryption = match header.content_encryption() {
                Some(enc) => match self.get_content_encryption(enc) {
                    Some(val) => val,
                    None => bail!("A content encryption is not registered: {}", enc),
                },
                None => bail!("A enc header claim is required."),
            };

            let compression = match header.compression() {
                Some(zip) => match self.get_compression(zip) {
                    Some(val) => Some(val),
                    None => bail!("A compression algorithm is not registered: {}", zip),
                },
                None => None,
            };

            let mut out_header = header.clone();

            let key_len = cencryption.key_len();
            let key = match encrypter.compute_content_encryption_key(
                cencryption,
                &header,
                &mut out_header,
            )? {
                Some(val) => val,
                None => Cow::Owned(util::random_bytes(key_len)),
            };

            let encrypted_key = encrypter.encrypt(&key, &header, &mut out_header)?;
            if let None = header.claim("kid") {
                if let Some(key_id) = encrypter.key_id() {
                    out_header.set_key_id(key_id);
                }
            }

            out_header.set_algorithm(encrypter.algorithm().name());

            let header_bytes = serde_json::to_vec(out_header.claims_set())?;
            let header_b64 = base64::encode_config(header_bytes, base64::URL_SAFE_NO_PAD);

            let compressed;
            let content = if let Some(compression) = compression {
                compressed = compression.compress(payload)?;
                &compressed
            } else {
                payload
            };

            let iv_vec;
            let iv = if cencryption.iv_len() > 0 {
                iv_vec = util::random_bytes(cencryption.iv_len());
                Some(iv_vec.as_slice())
            } else {
                None
            };

            let (ciphertext, tag) =
                cencryption.encrypt(&key, iv, content, header_b64.as_bytes())?;

            let mut capacity = 4;
            capacity += header_b64.len();
            if let Some(val) = &encrypted_key {
                capacity += util::ceiling(val.len() * 4, 3);
            }
            if let Some(val) = iv {
                capacity += util::ceiling(val.len() * 4, 3);
            }
            capacity += util::ceiling(ciphertext.len() * 4, 3);
            if let Some(val) = &tag {
                capacity += util::ceiling(val.len() * 4, 3);
            }

            let mut message = String::with_capacity(capacity);
            message.push_str(&header_b64);
            message.push_str(".");
            if let Some(val) = &encrypted_key {
                base64::encode_config_buf(val, base64::URL_SAFE_NO_PAD, &mut message);
            }
            message.push_str(".");
            if let Some(val) = iv {
                base64::encode_config_buf(val, base64::URL_SAFE_NO_PAD, &mut message);
            }
            message.push_str(".");
            base64::encode_config_buf(ciphertext, base64::URL_SAFE_NO_PAD, &mut message);
            message.push_str(".");
            if let Some(val) = &tag {
                base64::encode_config_buf(val, base64::URL_SAFE_NO_PAD, &mut message);
            }

            Ok(message)
        })()
        .map_err(|err| match err.downcast::<JoseError>() {
            Ok(err) => err,
            Err(err) => JoseError::InvalidJweFormat(err),
        })
    }
/// Return a representation of the data that is formatted by compact serialization.
///
/// # Arguments
///
/// * `payload` - The payload data.
/// * `header` - The JWS heaser claims.
/// * `selector` - a function for selecting the signing algorithm.
Return a representation of the data that is formatted by compact serialization.
Return a representation of the data that is formatted by compact serialization .
pub fn serialize_compact_with_selector<'a, F>(
        &self,
        payload: &[u8],
        header: &JweHeader,
        selector: F,
    ) -> Result<String, JoseError>
    where
        F: Fn(&JweHeader) -> Option<&'a dyn JweEncrypter>,
    {
        (|| -> anyhow::Result<String> {
            let encrypter = match selector(header) {
                Some(val) => val,
                None => bail!("A encrypter is not found."),
            };
            let cencryption = match header.content_encryption() {
                Some(enc) => match self.get_content_encryption(enc) {
                    Some(val) => val,
                    None => bail!("A content encryption is not registered: {}", enc),
                },
                None => bail!("A enc header claim is required."),
            };
            let compression = match header.compression() {
                Some(zip) => match self.get_compression(zip) {
                    Some(val) => Some(val),
                    None => bail!("A compression algorithm is not registered: {}", zip),
                },
                None => None,
            };
            let mut out_header = header.clone();
            let key_len = cencryption.key_len();
            let key = match encrypter.compute_content_encryption_key(
                cencryption,
                &header,
                &mut out_header,
            )? {
                Some(val) => val,
                None => Cow::Owned(util::random_bytes(key_len)),
            };
            let encrypted_key = encrypter.encrypt(&key, &header, &mut out_header)?;
            if let None = header.claim("kid") {
                if let Some(key_id) = encrypter.key_id() {
                    out_header.set_key_id(key_id);
                }
            }
            out_header.set_algorithm(encrypter.algorithm().name());
            let header_bytes = serde_json::to_vec(out_header.claims_set())?;
            let header_b64 = base64::encode_config(header_bytes, base64::URL_SAFE_NO_PAD);
            let compressed;
            let content = if let Some(compression) = compression {
                compressed = compression.compress(payload)?;
                &compressed
            } else {
                payload
            };
            let iv_vec;
            let iv = if cencryption.iv_len() > 0 {
                iv_vec = util::random_bytes(cencryption.iv_len());
                Some(iv_vec.as_slice())
            } else {
                None
            };
            let (ciphertext, tag) =
                cencryption.encrypt(&key, iv, content, header_b64.as_bytes())?;
            let mut capacity = 4;
            capacity += header_b64.len();
            if let Some(val) = &encrypted_key {
                capacity += util::ceiling(val.len() * 4, 3);
            }
            if let Some(val) = iv {
                capacity += util::ceiling(val.len() * 4, 3);
            }
            capacity += util::ceiling(ciphertext.len() * 4, 3);
            if let Some(val) = &tag {
                capacity += util::ceiling(val.len() * 4, 3);
            }
            let mut message = String::with_capacity(capacity);
            message.push_str(&header_b64);
            message.push_str(".");
            if let Some(val) = &encrypted_key {
                base64::encode_config_buf(val, base64::URL_SAFE_NO_PAD, &mut message);
            }
            message.push_str(".");
            if let Some(val) = iv {
                base64::encode_config_buf(val, base64::URL_SAFE_NO_PAD, &mut message);
            }
            message.push_str(".");
            base64::encode_config_buf(ciphertext, base64::URL_SAFE_NO_PAD, &mut message);
            message.push_str(".");
            if let Some(val) = &tag {
                base64::encode_config_buf(val, base64::URL_SAFE_NO_PAD, &mut message);
            }
            Ok(message)
        })()
        .map_err(|err| match err.downcast::<JoseError>() {
            Ok(err) => err,
            Err(err) => JoseError::InvalidJweFormat(err),
        })
    }
pub fn serialize_compact_with_selector < ' a , F > ( & self , payload : & [ u8 ] , header : & JweHeader , selector : F , ) -> Result < String , JoseError > where F : Fn ( & JweHeader ) -> Option < & ' a dyn JweEncrypter > , { ( | | -> anyhow :: Result < String > { let encrypter = match selector ( header ) { Some ( val ) => val , None => bail ! ( " " ) , } ; let cencryption = match header . content_encryption ( ) { Some ( enc ) => match self . get_content_encryption ( enc ) { Some ( val ) => val , None => bail ! ( " " enc ) , } , None => bail ! ( " " ) , } ; let compression = match header . compression ( ) { Some ( zip ) => match self . get_compression ( zip ) { Some ( val ) => Some ( val ) , None => bail ! ( " " zip ) , } , None => None , } ; let mut out_header = header . clone ( ) ; let key_len = cencryption . key_len ( ) ; let key = match encrypter . compute_content_encryption_key ( cencryption , & header , & mut out_header , ) ? { Some ( val ) => val , None => Cow :: Owned ( util :: random_bytes ( key_len ) ) , } ; let encrypted_key = encrypter . encrypt ( & key , & header , & mut out_header ) ? ; if let None = header . claim ( " " ) { if let Some ( key_id ) = encrypter . key_id ( ) { out_header . set_key_id ( key_id ) ; } } out_header . set_algorithm ( encrypter . algorithm ( ) . name ( ) ) ; let header_bytes = serde_json :: to_vec ( out_header . claims_set ( ) ) ? ; let header_b64 = base64 :: encode_config ( header_bytes , base64 :: URL_SAFE_NO_PAD ) ; let compressed ; let content = if let Some ( compression ) = compression { compressed = compression . compress ( payload ) ? ; & compressed } else { payload } ; let iv_vec ; let iv = if cencryption . iv_len ( ) > 0 { iv_vec = util :: random_bytes ( cencryption . iv_len ( ) ) ; Some ( iv_vec . as_slice ( ) ) } else { None } ; let ( ciphertext , tag ) = cencryption . encrypt ( & key , iv , content , header_b64 . as_bytes ( ) ) ? ; let mut capacity = 4 ; capacity += header_b64 . len ( ) ; if let Some ( val ) = & encrypted_key { capacity += util :: ceiling ( val . len ( ) * 4 , 3 ) ; } if let Some ( val ) = iv { capacity += util :: ceiling ( val . len ( ) * 4 , 3 ) ; } capacity += util :: ceiling ( ciphertext . len ( ) * 4 , 3 ) ; if let Some ( val ) = & tag { capacity += util :: ceiling ( val . len ( ) * 4 , 3 ) ; } let mut message = String :: with_capacity ( capacity ) ; message . push_str ( & header_b64 ) ; message . push_str ( " " ) ; if let Some ( val ) = & encrypted_key { base64 :: encode_config_buf ( val , base64 :: URL_SAFE_NO_PAD , & mut message ) ; } message . push_str ( " " ) ; if let Some ( val ) = iv { base64 :: encode_config_buf ( val , base64 :: URL_SAFE_NO_PAD , & mut message ) ; } message . push_str ( " " ) ; base64 :: encode_config_buf ( ciphertext , base64 :: URL_SAFE_NO_PAD , & mut message ) ; message . push_str ( " " ) ; if let Some ( val ) = & tag { base64 :: encode_config_buf ( val , base64 :: URL_SAFE_NO_PAD , & mut message ) ; } Ok ( message ) } ) ( ) . map_err ( | err | match err . downcast :: < JoseError > ( ) { Ok ( err ) => err , Err ( err ) => JoseError :: InvalidJweFormat ( err ) , } ) }
Return a representation of the data that is formatted by compact serialization.
Return a representation of the data that is formatted by compact serialization .
&self
payload
&[u8]
header
&JweHeader
selector
F
&self
payload
&[u8]
header
&JweHeader
selector
F