Created
April 2, 2023 08:44
-
-
Save shadeglare/1193965eeeb961866dca7e705cc779a3 to your computer and use it in GitHub Desktop.
Simple caesar encoding in Rust
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
pub fn encode<'a, I>(data: I, key: u8) -> Vec<u8> | |
where | |
I: IntoIterator<Item = &'a u8>, | |
{ | |
data.into_iter() | |
.map(|&b| b.wrapping_add(key)) | |
.collect::<Vec<_>>() | |
} | |
pub fn decode<'a, I>(data: I, key: u8) -> Vec<u8> | |
where | |
I: IntoIterator<Item = &'a u8>, | |
{ | |
data.into_iter() | |
.map(|&b| b.wrapping_sub(key)) | |
.collect::<Vec<_>>() | |
} | |
pub fn encode_str(value: &str, key: u8) -> Vec<u8> { | |
encode(value.as_bytes(), key) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment