Skip to content

Commit

Permalink
Add Xor cipher (TheAlgorithms#227)
Browse files Browse the repository at this point in the history
Co-authored-by: Sutthinart Khunvadhana <gai.su@everapp.io>
  • Loading branch information
iakgoog and Sutthinart Khunvadhana authored Oct 3, 2021
1 parent d6a1311 commit f614286
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* [Rot13](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/rot13.rs)
* [Sha256](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/sha256.rs)
* [Vigenere](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/vigenere.rs)
* [Xor](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/xor.rs)
* Data Structures
* [Avl Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/avl_tree.rs)
* [B Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/b_tree.rs)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ RESTART BUILD
- [x] [Caesar](./src/ciphers/caesar.rs)
- [x] [Vigenère](./src/ciphers/vigenere.rs)
- [x] [Morse Code](./src/ciphers/morse_code.rs)
- [x] [XOR](./src/ciphers/xor.rs)
- Rot13
- [x] [Rot13](./src/ciphers/rot13.rs)
- [x] [Another Rot13](./src/ciphers/another_rot13.rs)
Expand Down
2 changes: 2 additions & 0 deletions src/ciphers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ mod morse_code;
mod rot13;
mod sha256;
mod vigenere;
mod xor;

pub use self::another_rot13::another_rot13;
pub use self::caesar::caesar;
pub use self::morse_code::{decode, encode};
pub use self::rot13::rot13;
pub use self::sha256::sha256;
pub use self::vigenere::vigenere;
pub use self::xor::xor;
22 changes: 22 additions & 0 deletions src/ciphers/xor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pub fn xor(text: &str, key: u8) -> String {
text.chars().map(|c| ((c as u8) ^ key) as char).collect()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_simple() {
let test_string = "test string";
let ciphered_text = xor(test_string, 32);
assert_eq!(test_string, xor(&ciphered_text, 32));
}

#[test]
fn test_every_alphabet_with_space() {
let test_string = "The quick brown fox jumps over the lazy dog";
let ciphered_text = xor(test_string, 64);
assert_eq!(test_string, xor(&ciphered_text, 64));
}
}

0 comments on commit f614286

Please sign in to comment.