Created
January 26, 2020 03:46
-
-
Save nic-hartley/eb69ff75481a8a20e9c33df0c780c02a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TODO: Replace with real crypto | |
#[derive(Debug, Clone)] | |
pub struct PublicKey(u8, String); | |
impl PublicKey { | |
pub fn of(name: &str) -> PublicKey { | |
let sum = name.as_bytes().iter().fold(0u8, |a, i| a.wrapping_add(*i)); | |
PublicKey(sum, name.to_owned()) | |
} | |
pub fn encrypt(&self, data: &[u8]) -> Vec<u8> { | |
data.iter().map(|b| b.wrapping_add(self.0)).collect() | |
} | |
} | |
#[derive(Debug, Clone)] | |
pub struct SecretKey(u8, String); | |
impl SecretKey { | |
pub fn of(name: &str) -> SecretKey { | |
let sum = name.as_bytes().iter().fold(0u8, |a, i| a.wrapping_add(*i)); | |
SecretKey(sum, name.to_owned()) | |
} | |
pub fn decrypt(&self, ciphertext: &[u8]) -> Result<Vec<u8>, ()> { | |
Ok(ciphertext.iter().map(|b| b.wrapping_sub(self.0)).collect()) | |
} | |
pub fn pkey(&self) -> PublicKey { | |
PublicKey(self.0, self.1.clone()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment