xor

  1. pub fn xor(text: &str, key: u8) -> String {
  2. text.chars().map(|c| ((c as u8) ^ key) as char).collect()
  3. }
  4. #[cfg(test)]
  5. mod tests {
  6. use super::*;
  7. #[test]
  8. fn test_simple() {
  9. let test_string = "test string";
  10. let ciphered_text = xor(test_string, 32);
  11. assert_eq!(test_string, xor(&ciphered_text, 32));
  12. }
  13. #[test]
  14. fn test_every_alphabet_with_space() {
  15. let test_string = "The quick brown fox jumps over the lazy dog";
  16. let ciphered_text = xor(test_string, 64);
  17. assert_eq!(test_string, xor(&ciphered_text, 64));
  18. }
  19. }