Skip to content

Commit

Permalink
Add ROT13 implementation (#27)
Browse files Browse the repository at this point in the history
Co-authored-by: CuriousCorrelation <[email protected]>
Co-authored-by: Alexander González <[email protected]>
  • Loading branch information
3 people authored Sep 3, 2021
1 parent d35e092 commit 600c5e6
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ cargo test

- [x] Transposition
- [x] Caesar cipher
- [x] ROT13

### Bit Manipulation

Expand Down
17 changes: 17 additions & 0 deletions src/ciphers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,21 @@ top to bottom and left to right.

###### Source: [Wikipedia](https://en.wikipedia.org/wiki/Transposition_cipher)

### [ROT13](./rot13.rs)
![alt text][rot13]

**ROT13** or **"rotate by 13 places"**, sometimes hyphenated **ROT-13** is a simple letter substitution cipher that replaces a letter with the 13th letter after it in the alphabet.<br>
Because there are 26 letters (2×13) in the basic Latin alphabet, ROT13 is its own inverse; that is, to undo ROT13, the same algorithm is applied, so the same action can be used for encoding and decoding.<br>
The algorithm provides virtually no cryptographic security, and is often cited as a canonical example of weak encryption.

The transformation can be done using a lookup table, such as the following:

```
| Input | ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz |
| Output | NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm |
```

###### Source: [Wikipedia](https://en.wikipedia.org/wiki/ROT13)

[caesar]: https://upload.wikimedia.org/wikipedia/commons/4/4a/Caesar_cipher_left_shift_of_3.svg
[rot13]: https://upload.wikimedia.org/wikipedia/commons/3/33/ROT13_table_with_example.svg
2 changes: 2 additions & 0 deletions src/ciphers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod caesar;
mod rot13;
mod transposition;

pub use self::caesar::caesar;
pub use self::rot13::rot13;
pub use self::transposition::transposition;
68 changes: 68 additions & 0 deletions src/ciphers/rot13.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//! Rot13 or "rotate by 13 places"
//!
//! # Algorithm
/// Encrypts a given [`&str`] using ROT13 cipher.
///
/// See [ROT13](https://en.wikipedia.org/wiki/ROT13) for more information.
///
/// Replaces each character with the 13th letter after it in the alphabet.
/// Rot13 is a special case of [Caesar cipher](https://en.wikipedia.org/wiki/Caesar_cipher)
///
/// The most basic example is ROT 13, which rotates 'a' to 'n'.
/// This implementation does not rotate unicode characters.
///
/// # Arguments
///
/// `text` - String to transform.
///
/// # Returns
///
/// An owned [`String`]
///
/// # Panic
///
/// This function won't panic.
///
/// # Examples
/// ```
/// # use rust_algorithms::ciphers::rot13;
/// let encoded = rot13("hello world");
/// assert_eq!(encoded, "URYYB JBEYQ");
/// ```
pub fn rot13(text: &str) -> String {
let to_enc = text.to_uppercase();
to_enc
.chars()
.map(|c| match c {
'A'..='M' => ((c as u8) + 13) as char,
'N'..='Z' => ((c as u8) - 13) as char,
_ => c,
})
.collect()
}

#[cfg(test)]
mod test {
use super::rot13;

#[test]
fn test_single_letter() {
assert_eq!("N", rot13("A"));
}

#[test]
fn test_bunch_of_letters() {
assert_eq!("NOP", rot13("ABC"));
}

#[test]
fn test_non_ascii() {
assert_eq!("😀NO", rot13("😀AB"));
}

#[test]
fn test_twice() {
assert_eq!("ABCD", rot13(&rot13("ABCD")));
}
}

0 comments on commit 600c5e6

Please sign in to comment.