pub fn new(context: SharedContext, stream: S, svr_cfg: &ServerConfig) -> CryptoStream<S> {
        let method = svr_cfg.method();
        let category = method.category();
        let key = svr_cfg.clone_key();

        if category == CipherCategory::None {
            return CryptoStream::<S>::new_none(stream);
        }

        let prev_len = match category {
            CipherCategory::Stream => method.iv_len(),
            CipherCategory::Aead => method.salt_len(),
            CipherCategory::None => 0,
        };

        let iv = match category {
            CipherCategory::Stream => {
                let local_iv = loop {
                    let mut iv = vec![0u8; prev_len];
                    if prev_len > 0 {
                        random_iv_or_salt(&mut iv);
                    }

                    if context.check_nonce_and_set(&iv) {
                        // IV exist, generate another one
                        continue;
                    }
                    break iv;
                };
                trace!("generated Stream cipher IV {:?}", ByteStr::new(&local_iv));
                local_iv
            }
            CipherCategory::Aead => {
                let local_salt = loop {
                    let mut salt = vec![0u8; prev_len];
                    if prev_len > 0 {
                        random_iv_or_salt(&mut salt);
                    }

                    if context.check_nonce_and_set(&salt) {
                        // Salt exist, generate another one
                        continue;
                    }
                    break salt;
                };
                trace!("generated AEAD cipher salt {:?}", ByteStr::new(&local_salt));
                local_salt
            }
            CipherCategory::None => Vec::new(),
        };

        let enc = match category {
            CipherCategory::Stream => EncryptedWriter::Stream(StreamEncryptedWriter::new(method, &key, &iv)),
            CipherCategory::Aead => EncryptedWriter::Aead(AeadEncryptedWriter::new(method, &key, &iv)),
            CipherCategory::None => EncryptedWriter::None,
        };

        CryptoStream {
            stream,
            dec: None,
            enc,
            read_status: ReadStatus::WaitIv(context, BytesMut::with_capacity(prev_len).limit(prev_len), method, key),
        }
    }
/// Create a new CryptoStream with the underlying stream connection
Create a new CryptoStream with the underlying stream connection
Create a new CryptoStream with the underlying stream connection
pub fn new(context: SharedContext, stream: S, svr_cfg: &ServerConfig) -> CryptoStream<S> {
        let method = svr_cfg.method();
        let category = method.category();
        let key = svr_cfg.clone_key();
        if category == CipherCategory::None {
            return CryptoStream::<S>::new_none(stream);
        }
        let prev_len = match category {
            CipherCategory::Stream => method.iv_len(),
            CipherCategory::Aead => method.salt_len(),
            CipherCategory::None => 0,
        };
        let iv = match category {
            CipherCategory::Stream => {
                let local_iv = loop {
                    let mut iv = vec![0u8; prev_len];
                    if prev_len > 0 {
                        random_iv_or_salt(&mut iv);
                    }
                    if context.check_nonce_and_set(&iv) {
                        // IV exist, generate another one
                        continue;
                    }
                    break iv;
                };
                trace!("generated Stream cipher IV {:?}", ByteStr::new(&local_iv));
                local_iv
            }
            CipherCategory::Aead => {
                let local_salt = loop {
                    let mut salt = vec![0u8; prev_len];
                    if prev_len > 0 {
                        random_iv_or_salt(&mut salt);
                    }
                    if context.check_nonce_and_set(&salt) {
                        // Salt exist, generate another one
                        continue;
                    }
                    break salt;
                };
                trace!("generated AEAD cipher salt {:?}", ByteStr::new(&local_salt));
                local_salt
            }
            CipherCategory::None => Vec::new(),
        };
        let enc = match category {
            CipherCategory::Stream => EncryptedWriter::Stream(StreamEncryptedWriter::new(method, &key, &iv)),
            CipherCategory::Aead => EncryptedWriter::Aead(AeadEncryptedWriter::new(method, &key, &iv)),
            CipherCategory::None => EncryptedWriter::None,
        };
        CryptoStream {
            stream,
            dec: None,
            enc,
            read_status: ReadStatus::WaitIv(context, BytesMut::with_capacity(prev_len).limit(prev_len), method, key),
        }
    }
pub fn new ( context : SharedContext , stream : S , svr_cfg : & ServerConfig ) -> CryptoStream < S > { let method = svr_cfg . method ( ) ; let category = method . category ( ) ; let key = svr_cfg . clone_key ( ) ; if category == CipherCategory :: None { return CryptoStream :: < S > :: new_none ( stream ) ; } let prev_len = match category { CipherCategory :: Stream => method . iv_len ( ) , CipherCategory :: Aead => method . salt_len ( ) , CipherCategory :: None => 0 , } ; let iv = match category { CipherCategory :: Stream => { let local_iv = loop { let mut iv = vec ! [ 0u8 prev_len ] ; if prev_len > 0 { random_iv_or_salt ( & mut iv ) ; } if context . check_nonce_and_set ( & iv ) { // IV exist, generate another one continue ; } break iv ; } ; trace ! ( " " ByteStr new ( local_iv ) ) ; local_iv } CipherCategory :: Aead => { let local_salt = loop { let mut salt = vec ! [ 0u8 prev_len ] ; if prev_len > 0 { random_iv_or_salt ( & mut salt ) ; } if context . check_nonce_and_set ( & salt ) { // Salt exist, generate another one continue ; } break salt ; } ; trace ! ( " " ByteStr new ( local_salt ) ) ; local_salt } CipherCategory :: None => Vec :: new ( ) , } ; let enc = match category { CipherCategory :: Stream => EncryptedWriter :: Stream ( StreamEncryptedWriter :: new ( method , & key , & iv ) ) , CipherCategory :: Aead => EncryptedWriter :: Aead ( AeadEncryptedWriter :: new ( method , & key , & iv ) ) , CipherCategory :: None => EncryptedWriter :: None , } ; CryptoStream { stream , dec : None , enc , read_status : ReadStatus :: WaitIv ( context , BytesMut :: with_capacity ( prev_len ) . limit ( prev_len ) , method , key ) , } }
Create a new CryptoStream with the underlying stream connection
Create a new CryptoStream with the underlying stream connection
context
SharedContext
stream
S
svr_cfg
&ServerConfig