forked from TheAlgorithms/Rust
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sutthinart Khunvadhana <gai.su@everapp.io>
- Loading branch information
Showing
4 changed files
with
26 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
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
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)); | ||
} | ||
} |