pub fn serialize_nts_packet<T: Aead>(packet: NtsPacket, encryptor: &mut T) -> Vec<u8> {
    let mut buff = Cursor::new(Vec::new());
    buff.write_all(&serialize_header(packet.header))
        .expect("Nts header could not be written, failed to serialize NtsPacket");
    buff.write_all(&serialize_extensions(packet.auth_exts))
        .expect("Nts extensions could not be written, failed to serialize NtsPacket");
    let plaintext = serialize_extensions(packet.auth_enc_exts);
    let mut nonce = [0; NONCE_LEN];
    rand::thread_rng().fill(&mut nonce);
    let ciphertext = encryptor.seal(&nonce, &buff.get_ref(), &plaintext);

    let mut authent_buffer = Cursor::new(Vec::new());
    authent_buffer.write_u16::<BigEndian>(NONCE_LEN as u16)
        .expect("Nonce length could not be written, failed to serialize NtsPacket"); // length of the nonce
    authent_buffer.write_u16::<BigEndian>(ciphertext.len() as u16)
        .expect("Ciphertext length could not be written, failed to serialize NtsPacket");
    authent_buffer.write_all(&nonce)
        .expect("Nonce could not be written, failed to serialize NtsPacket"); // 16 bytes so no padding
    authent_buffer.write_all(&ciphertext)
        .expect("Ciphertext could not be written, failed to serialize NtsPacket");
    let padlen = (4 - (ciphertext.len() % 4)) % 4;
    for _i in 0..padlen {
        // pad with zeros: probably cleaner way exists
        authent_buffer.write_u8(0)
            .expect("Padding could not be written, failed to serialize NtsPacket");
    }
    let last_ext = NtpExtension {
        ext_type: NTSAuthenticator,
        contents: authent_buffer.into_inner(),
    };
    let res = serialize_extensions(vec![last_ext]);
    buff.write_all(&res)
        .expect("Extensions could not be written, failed to serialize NtsPacket");
    buff.into_inner()
}
/// serialize_nts_packet serializes the packet and does all the encryption
serialize_nts_packet serializes the packet and does all the encryption
serialize_nts_packet serializes the packet and does all the encryption
pub fn serialize_nts_packet<T: Aead>(packet: NtsPacket, encryptor: &mut T) -> Vec<u8> {
    let mut buff = Cursor::new(Vec::new());
    buff.write_all(&serialize_header(packet.header))
        .expect("Nts header could not be written, failed to serialize NtsPacket");
    buff.write_all(&serialize_extensions(packet.auth_exts))
        .expect("Nts extensions could not be written, failed to serialize NtsPacket");
    let plaintext = serialize_extensions(packet.auth_enc_exts);
    let mut nonce = [0; NONCE_LEN];
    rand::thread_rng().fill(&mut nonce);
    let ciphertext = encryptor.seal(&nonce, &buff.get_ref(), &plaintext);
    let mut authent_buffer = Cursor::new(Vec::new());
    authent_buffer.write_u16::<BigEndian>(NONCE_LEN as u16)
        .expect("Nonce length could not be written, failed to serialize NtsPacket"); // length of the nonce
    authent_buffer.write_u16::<BigEndian>(ciphertext.len() as u16)
        .expect("Ciphertext length could not be written, failed to serialize NtsPacket");
    authent_buffer.write_all(&nonce)
        .expect("Nonce could not be written, failed to serialize NtsPacket"); // 16 bytes so no padding
    authent_buffer.write_all(&ciphertext)
        .expect("Ciphertext could not be written, failed to serialize NtsPacket");
    let padlen = (4 - (ciphertext.len() % 4)) % 4;
    for _i in 0..padlen {
        // pad with zeros: probably cleaner way exists
        authent_buffer.write_u8(0)
            .expect("Padding could not be written, failed to serialize NtsPacket");
    }
    let last_ext = NtpExtension {
        ext_type: NTSAuthenticator,
        contents: authent_buffer.into_inner(),
    };
    let res = serialize_extensions(vec![last_ext]);
    buff.write_all(&res)
        .expect("Extensions could not be written, failed to serialize NtsPacket");
    buff.into_inner()
}
pub fn serialize_nts_packet < T : Aead > ( packet : NtsPacket , encryptor : & mut T ) -> Vec < u8 > { let mut buff = Cursor :: new ( Vec :: new ( ) ) ; buff . write_all ( & serialize_header ( packet . header ) ) . expect ( " " ) ; buff . write_all ( & serialize_extensions ( packet . auth_exts ) ) . expect ( " " ) ; let plaintext = serialize_extensions ( packet . auth_enc_exts ) ; let mut nonce = [ 0 ; NONCE_LEN ] ; rand :: thread_rng ( ) . fill ( & mut nonce ) ; let ciphertext = encryptor . seal ( & nonce , & buff . get_ref ( ) , & plaintext ) ; let mut authent_buffer = Cursor :: new ( Vec :: new ( ) ) ; authent_buffer . write_u16 :: < BigEndian > ( NONCE_LEN as u16 ) . expect ( " " ) ; // length of the nonce authent_buffer . write_u16 :: < BigEndian > ( ciphertext . len ( ) as u16 ) . expect ( " " ) ; authent_buffer . write_all ( & nonce ) . expect ( " " ) ; // 16 bytes so no padding authent_buffer . write_all ( & ciphertext ) . expect ( " " ) ; let padlen = ( 4 - ( ciphertext . len ( ) % 4 ) ) % 4 ; for _i in 0 .. padlen { // pad with zeros: probably cleaner way exists authent_buffer . write_u8 ( 0 ) . expect ( " " ) ; } let last_ext = NtpExtension { ext_type : NTSAuthenticator , contents : authent_buffer . into_inner ( ) , } ; let res = serialize_extensions ( vec ! [ last_ext ] ) ; buff . write_all ( & res ) . expect ( " " ) ; buff . into_inner ( ) }
serialize_nts_packet serializes the packet and does all the encryption
serialize_nts_packet serializes the packet and does all the encryption
packet
NtsPacket
encryptor
&mut T
packet
NtsPacket
encryptor
&mut T