Skip to content

Commit

Permalink
feat(docs): document xor cipher (#85)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexander Gonzalez <[email protected]>
  • Loading branch information
scriptandcompile and alexfertel authored Jun 11, 2024
1 parent af39ee8 commit 07180ab
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/ciphers/xor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
/// XOR cipher
///
/// # Arguments
///
/// * `text` - A string slice that holds the text to be ciphered.
/// * `key` - A u8 that holds the key to be used for ciphering.
///
/// # Returns
///
/// * A String that holds the ciphered text.
///
/// # Example
///
/// ```rust
/// use rust_algorithms::ciphers::xor;
///
/// 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));
/// ```
pub fn xor(text: &str, key: u8) -> String {
text.chars().map(|c| ((c as u8) ^ key) as char).collect()
}
Expand All @@ -12,11 +33,4 @@ mod tests {
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 07180ab

Please sign in to comment.