mega-security-rs 0.1.0

An implementation of Mega's security whitepaper
Documentation
use crate::{errors::ServerError, utils::_salt, Client};

use std::collections::HashMap;

use sha2::{Digest, Sha256};

#[derive(Debug, Default)]
pub struct Server {
    clients_registered: HashMap<String, Client>,
}

impl Server {
    pub fn new() -> Self {
        Self {
            ..Default::default()
        }
    }

    // TODO: this needs to return a confirmation token
    pub fn register_client(&mut self, client: &Client) -> Result<(), ServerError> {
        if let Some(_) = self.clients_registered.get(&client.id) {
            return Err(ServerError::ClientAlreadyRegistred(client.id.to_owned()))
        }

        self.clients_registered.insert(client.id.to_owned(), client.clone());

        Ok(())
    }

    pub fn get_salt_from_id(&self, id: &str) -> Result<Vec<u8>, ServerError> {
        let client = if let Some(c) = self.clients_registered.get(id) { c } 
            else { return Err(ServerError::ClientNotFound(id.to_owned()))};
        
        Ok(_salt(id, client.random_number()))
    }

    pub fn auth_client(&self, id: &str, autentication_key: &[u8]) -> Result<(), ServerError>{
        let mut auth_key_hasher = Sha256::new();
        auth_key_hasher.update(autentication_key);
        let hashed_auth_key = &auth_key_hasher.finalize()[..16];

        let client = if let Some(c) = self.clients_registered.get(id) { c } 
            else { return Err(ServerError::ClientNotFound(id.to_owned()))};

        if client.hashed_auth_key() == hashed_auth_key { Ok(()) } 
            else { return Err(ServerError::AutenticationFailed(id.to_owned())) }
    } 
}