Skip to content

Commit

Permalink
feat(docs): document transposition cipher (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
scriptandcompile authored Jun 11, 2024
1 parent 07180ab commit e4d1b76
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions src/ciphers/transposition.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,37 @@
//! Transposition Cipher
//!
//! # Algorithm

use std::collections::BTreeMap;

/// Encrypts a given String with the Transposition cipher
/// Encrypts a given input text with the Transposition cipher
///
/// For each character of the keyword string a new column inside a table is created.
/// Each column receives the corresponding character of the keyword string.
/// Every character of the input string will then be put in the fields from left to right.
/// For each character of the keyword text a new column inside a table is created.
/// Each column receives the corresponding character of the keyword text.
/// Every character of the input text will then be put in the fields from left to right.
/// Empty fields will be filled with the character 'X'.
/// The keyword string and its corresponding column is then sorted by its alphanumeric values.
/// To get the encrypted String every character inside the table will be added from
/// The key text and its corresponding column is then sorted by its alphanumeric values.
/// To get the encrypted text every character inside the table will be added from
/// top to bottom and left to right.
///
/// See [Transposition Cipher](https://en.wikipedia.org/wiki/Transposition_cipher) for the theoretical background.
/// # References
///
/// * [Transposition Cipher](https://en.wikipedia.org/wiki/Transposition_cipher) for the theoretical background.
///
/// # Arguments
///
/// * `key` - string that functions as encryption key
/// * `input` - string that is encrypted
/// * `key` - Text that functions as encryption key
/// * `input` - Text to encrypt.
///
/// # Returns
///
/// * `enc` - encrypted string
///
/// # Panic
///
/// This function won't panic
/// The encrypted text.
///
/// # Examples
///
/// use std::collections::BTreeMap;
/// ```rust
/// use rust_algorithms::ciphers::transposition;
///
/// let encrypted = transposition("lorem", "ipsum");
///
/// assert_eq!("UIMPS", encrypted);
/// ```
pub fn transposition(key: &str, input: &str) -> String {
let mut to_enc = input.to_uppercase();
let keyword = key.to_uppercase();
Expand Down Expand Up @@ -83,13 +80,13 @@ mod test {
use super::*;

#[test]
fn test_word() {
assert_eq!("OMLERX", transposition("key", "lorem"));
fn test_sentence_with_punctuation_marks() {
assert_eq!("OMIUXLE S!R,PMX", transposition("key", "Lorem, ipsum!"));
}

#[test]
fn test_sentence_with_punctuation_marks() {
assert_eq!("OMIUXLE S!R,PMX", transposition("key", "Lorem, ipsum!"));
fn test_word() {
assert_eq!("OMLERX", transposition("key", "lorem"));
}

#[test]
Expand Down

0 comments on commit e4d1b76

Please sign in to comment.