pub struct McEliece {
pub public_key: Vec<u8>,
pub private_key: Vec<u8>,
}
impl McEliece {
pub fn new() -> Self {
McEliece {
public_key: vec![0; 128], private_key: vec![0; 128], }
}
pub fn encrypt(&self, plaintext: &[u8]) -> Vec<u8> {
self.simple_transform(plaintext, |x| x.wrapping_add(1))
}
pub fn decrypt(&self, ciphertext: &[u8]) -> Vec<u8> {
self.simple_transform(ciphertext, |x| x.wrapping_sub(1))
}
fn simple_transform<F>(&self, data: &[u8], f: F) -> Vec<u8>
where
F: Fn(u8) -> u8,
{
data.iter().map(|&x| f(x)).collect()
}
}
impl Default for McEliece {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_mceliece_encrypt_decrypt() {
let mceliece = McEliece::new();
let plaintext = vec![1, 2, 3, 4];
let ciphertext = mceliece.encrypt(&plaintext);
let decrypted_text = mceliece.decrypt(&ciphertext);
assert_eq!(decrypted_text, plaintext);
}
}