Browse Source

cleanup & try to create message obj in client

master^2
Niklas Eichenseer 1 year ago
parent
commit
f4996f366f
2 changed files with 10 additions and 23 deletions
  1. 7
    17
      src/bin/client.rs
  2. 3
    6
      src/bin/message.rs

+ 7
- 17
src/bin/client.rs View File

}; };
use std::io::Write; use std::io::Write;
use std::net::TcpStream; use std::net::TcpStream;
use crate::message::*;

mod message;


fn main() { fn main() {
let port: u32 = 7878; // Port Server
//let stream = TcpStream::connect("172.30.16.1:8080");
//let stream = TcpStream::connect("27.0.0.1:8080");
// Server Port
let port: u32 = 7878;
match TcpStream::connect(format!("localhost:{}", port)) { match TcpStream::connect(format!("localhost:{}", port)) {
Ok(mut stream) => { Ok(mut stream) => {
println!("Successfully connected to server"); println!("Successfully connected to server");


// Encrypt the message using the box // Encrypt the message using the box
let ciphertext = salsa_box.encrypt(&nonce, &plaintext[..]).expect("Fehler"); let ciphertext = salsa_box.encrypt(&nonce, &plaintext[..]).expect("Fehler");
let message = Message::new(0,0,4, PayloadType::Msg);


println!("Sending {0} as {1:?}", buffer.trim(), plaintext); println!("Sending {0} as {1:?}", buffer.trim(), plaintext);
stream.write(buffer.as_bytes()).unwrap(); stream.write(buffer.as_bytes()).unwrap();
} }
println!("Terminated."); println!("Terminated.");
} }

fn generate_box(partner_public_key: PublicKey) -> (SalsaBox, PublicKey) {
// Generate a random secret key.
// NOTE: The secret key bytes can be accessed by calling `secret_key.as_bytes()`
let own_secret_key = SecretKey::generate(&mut OsRng);

// Get the public key for the secret key we just generated
let own_public_key = own_secret_key.public_key().clone();

// Create a `SalsaBox` by performing Diffie-Hellman key agreement between
// the two keys.
let salsa_box = SalsaBox::new(&partner_public_key, &own_secret_key);
(salsa_box, own_public_key)
}

+ 3
- 6
src/bin/message.rs View File

<<<<<<< HEAD
use std::{ use std::{
io::{BufReader, Read}, io::{BufReader, Read},
net::TcpStream, net::TcpStream,
}; };
=======
>>>>>>> f36772d9e5c0507015de02b8c71ff25d84789b98


const KEYLENGTH: u8 = 32; const KEYLENGTH: u8 = 32;


//160-255 reserved //160-255 reserved
} }


struct Message {
pub struct Message {
// Eigenschaften der Klasse // Eigenschaften der Klasse
src_id: u32, src_id: u32,
dest_id: u32, dest_id: u32,
} }


impl Message { impl Message {
fn new(src_id: u32, dest_id: u32, size: u16, msg_type: PayloadType) -> Message {
pub fn new(src_id: u32, dest_id: u32, size: u16, msg_type: PayloadType) -> Message {
let mut msg = Message { let mut msg = Message {
src_id: src_id, src_id: src_id,
dest_id: dest_id, dest_id: dest_id,
vec vec
} }


fn recive(stream: TcpStream) -> Message {
fn receive(stream: TcpStream) -> Message {
let buffreader = BufReader::new(stream); let buffreader = BufReader::new(stream);
let mut byte_iterator = buffreader.bytes(); let mut byte_iterator = buffreader.bytes();
let byte1 = byte_iterator.next().unwrap().unwrap(); let byte1 = byte_iterator.next().unwrap().unwrap();

Loading…
Cancel
Save