Skip to content

Instantly share code, notes, and snippets.

@shadeglare
Created April 2, 2023 08:44
Show Gist options
  • Save shadeglare/1193965eeeb961866dca7e705cc779a3 to your computer and use it in GitHub Desktop.
Save shadeglare/1193965eeeb961866dca7e705cc779a3 to your computer and use it in GitHub Desktop.
Simple caesar encoding in Rust
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