From af39ee8c3f4cfdf4c80938187f5b2b96a39d401b Mon Sep 17 00:00:00 2001 From: scriptandcompile Date: Tue, 11 Jun 2024 11:43:34 -0700 Subject: [PATCH] feat(docs): document the vigenere cipher (#84) --- src/ciphers/vigenere.rs | 51 +++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/src/ciphers/vigenere.rs b/src/ciphers/vigenere.rs index 5f98a56..f6d97bc 100644 --- a/src/ciphers/vigenere.rs +++ b/src/ciphers/vigenere.rs @@ -1,12 +1,37 @@ -//! Vigenère Cipher -//! -//! # Algorithm -//! -//! Rotate each ascii character by the offset of the corresponding key character. -//! When we reach the last key character, we start over from the first one. -//! This implementation does not rotate unicode characters. - -/// Vigenère cipher to rotate plain_text text by key and return an owned String. +/// A Vigenère cipher is a method of encrypting alphabetic text by using a simple form of polyalphabetic substitution. +/// Each letter in the plain_text text is shifted by the corresponding letter in the key. +/// +/// # Algorithm +/// +/// Rotate each ascii character by the offset of the corresponding key character. +/// When we reach the last key character, we start over from the first one. +/// This implementation does not rotate unicode characters. +/// +/// # Reference +/// +/// [Vigenère Cipher](https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher). +/// +/// # Arguments +/// +/// * `plain_text` - A string slice that holds the text to be encrypted. +/// * `key` - A string slice that holds the key to be used for encryption. +/// +/// # Returns +/// +/// An owned String that holds the encrypted text. +/// +/// # Example +/// +/// ```rust +/// use rust_algorithms::ciphers::vigenere; +/// +/// let plain_text = "LoremIpsumDolorSitAmet"; +/// let key = "base"; +/// +/// let encrypted = vigenere(plain_text, key); +/// +/// assert_eq!(encrypted, "MojinIhwvmVsmojWjtSqft"); +/// ``` pub fn vigenere(plain_text: &str, key: &str) -> String { // Remove all unicode and non-ascii characters from key. let key: String = key.chars().filter(|&c| c.is_ascii_alphabetic()).collect(); @@ -44,14 +69,6 @@ mod tests { assert_eq!(vigenere("", "test"), ""); } - #[test] - fn vigenere_base() { - assert_eq!( - vigenere("LoremIpsumDolorSitAmet", "base"), - "MojinIhwvmVsmojWjtSqft" - ); - } - #[test] fn vigenere_with_spaces() { assert_eq!(