Gitea
2 years ago
9 changed files with 187 additions and 21 deletions
@ -0,0 +1,46 @@
|
||||
/* |
||||
ddtu - digital data transferring utility |
||||
Copyright (C) 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte) |
||||
|
||||
This program is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU Affero General Public License as published by |
||||
the Free Software Foundation, either version 3 of the License, or |
||||
(at your option) any later version. |
||||
|
||||
This program is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU Affero General Public License for more details. |
||||
*/ |
||||
|
||||
use crate::crypt::rand; |
||||
use std::time::{SystemTime, UNIX_EPOCH}; |
||||
|
||||
pub struct Key8bit { |
||||
pub k: u8, |
||||
} |
||||
|
||||
impl Key8bit { |
||||
pub fn new(key: u8) -> Key8bit { |
||||
return Key8bit{ |
||||
k: key, |
||||
} |
||||
} |
||||
|
||||
pub fn new_random() -> Key8bit { |
||||
return Key8bit{ |
||||
k: rand::rand_u16(SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() as u16) as u8, |
||||
} |
||||
} |
||||
} |
||||
|
||||
#[cfg(test)] |
||||
mod tests { |
||||
use crate::crypt::keys::Key8bit; |
||||
|
||||
#[test] |
||||
fn test_key8bit_generation() { |
||||
assert_eq!(Key8bit::new(255).k, 255); |
||||
Key8bit::new_random(); |
||||
} |
||||
} |
@ -0,0 +1,50 @@
|
||||
/* |
||||
ddtu - digital data transferring utility |
||||
Copyright (C) 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte) |
||||
|
||||
This program is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU Affero General Public License as published by |
||||
the Free Software Foundation, either version 3 of the License, or |
||||
(at your option) any later version. |
||||
|
||||
This program is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU Affero General Public License for more details. |
||||
*/ |
||||
|
||||
use crate::crypt::keys::Key8bit; |
||||
|
||||
pub fn encrypt(plain: &mut [u8], key: &Key8bit) { |
||||
for i in 0..plain.len() { |
||||
plain[i] = plain[i] ^ key.k; |
||||
} |
||||
} |
||||
|
||||
pub fn decrypt(encrypted: &mut[u8], key: &Key8bit) { |
||||
for i in 0..encrypted.len() { |
||||
encrypted[i] = encrypted[i] ^ key.k; |
||||
} |
||||
} |
||||
|
||||
#[cfg(test)] |
||||
mod tests { |
||||
use crate::crypt::keys::Key8bit; |
||||
use crate::crypt::xor; |
||||
|
||||
#[test] |
||||
fn test_xor() { |
||||
let plaintext = "plain text 123"; |
||||
let mut plaintext_bytes: Vec<u8> = Vec::with_capacity(plaintext.bytes().len()); |
||||
for b in plaintext.bytes() { |
||||
plaintext_bytes.push(b); |
||||
}
|
||||
|
||||
let key = Key8bit::new(45); |
||||
|
||||
xor::encrypt(&mut plaintext_bytes, &key);
|
||||
xor::decrypt(&mut plaintext_bytes, &key); |
||||
|
||||
assert_eq!(String::from_utf8_lossy(&plaintext_bytes), plaintext); |
||||
} |
||||
} |
@ -0,0 +1,9 @@
|
||||
pub struct Node { |
||||
|
||||
} |
||||
|
||||
impl Node { |
||||
pub fn new() -> Node { |
||||
return Node{}; |
||||
} |
||||
} |
@ -1,3 +1,2 @@
|
||||
pub mod packets; |
||||
pub mod constants; |
||||
pub mod specs; |
Loading…
Reference in new issue