/// Enciphers the given plaintext (a str reference) using the Columnar
/// Transposition cipher and returns the ciphertext as a `CipherResult`.
///
/// # Example
/// ```
/// use ciphers::{Cipher, ColumnarTransposition};
///
/// let ct = ColumnarTransposition::new("GERMAN");
///
/// let ctext = ct.encipher("DEFENDTHEEASTWALLOFTHECASTLE");
/// assert_eq!(ctext.unwrap(), "NALCEHWTTDTTFSEELEEDSOAFEAHL")
/// ```
Enciphers the given plaintext (a str reference) using the Columnar
Transposition cipher and returns the ciphertext as a `CipherResult`.
Enciphers the given plaintext ( a str reference ) using the Columnar Transposition cipher and returns the ciphertext as a ` CipherResult ` .
fn encipher(&self, ptext: &str) -> CipherResult {
        input::is_ascii(ptext)?;
        let mut key: Vec<u8> = self.key.bytes().collect();
        let ptext = ptext.as_bytes();
        let mut matrix: HashMap<u8, Vec<u8>> = HashMap::with_capacity(key.len());
        // populate matrix
        for i in 0..ptext.len() {
            matrix.entry(key[i % key.len()]).or_insert(vec![]);
            matrix.get_mut(&key[i % key.len()]).unwrap().push(ptext[i]);
        }
        key.sort();
        let mut ctext = vec![];
        for k in key.iter() {
            for byte in matrix.get(&k).unwrap() {
                ctext.push(*byte);
            }
        }
        Ok(String::from_utf8(ctext).unwrap())
    }
fn encipher ( & self , ptext : & str ) -> CipherResult { input :: is_ascii ( ptext ) ? ; let mut key : Vec < u8 > = self . key . bytes ( ) . collect ( ) ; let ptext = ptext . as_bytes ( ) ; let mut matrix : HashMap < u8 , Vec < u8 > > = HashMap :: with_capacity ( key . len ( ) ) ; // populate matrix for i in 0 .. ptext . len ( ) { matrix . entry ( key [ i % key . len ( ) ] ) . or_insert ( vec ! [ ] ) ; matrix . get_mut ( & key [ i % key . len ( ) ] ) . unwrap ( ) . push ( ptext [ i ] ) ; } key . sort ( ) ; let mut ctext = vec ! [ ] ; for k in key . iter ( ) { for byte in matrix . get ( & k ) . unwrap ( ) { ctext . push ( * byte ) ; } } Ok ( String :: from_utf8 ( ctext ) . unwrap ( ) ) }
Enciphers the given plaintext (a str reference) using the Columnar
Transposition cipher and returns the ciphertext as a `CipherResult`.
Enciphers the given plaintext ( a str reference ) using the Columnar Transposition cipher and returns the ciphertext as a ` CipherResult ` .
&self
ptext
&str
example
```
use ciphers::{Cipher, ColumnarTransposition}.

let ct = ColumnarTransposition::new("GERMAN").

let ctext = ct.encipher("DEFENDTHEEASTWALLOFTHECASTLE");
assert_eq!(ctext.unwrap(), "NALCEHWTTDTTFSEELEEDSOAFEAHL")
```
` ` ` use ciphers :: { Cipher ColumnarTransposition } . let ct = ColumnarTransposition :: new ( " GERMAN " ) . let ctext = ct . encipher ( " DEFENDTHEEASTWALLOFTHECASTLE " ) ; assert_eq! ( ctext . unwrap () " NALCEHWTTDTTFSEELEEDSOAFEAHL " ) ` ` `