Skip to content

Commit 600c5e6

Browse files
Add ROT13 implementation (alexfertel#27)
Co-authored-by: CuriousCorrelation <[email protected]> Co-authored-by: Alexander González <[email protected]>
1 parent d35e092 commit 600c5e6

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ cargo test
8383

8484
- [x] Transposition
8585
- [x] Caesar cipher
86+
- [x] ROT13
8687

8788
### Bit Manipulation
8889

src/ciphers/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,21 @@ top to bottom and left to right.
2727

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

30+
### [ROT13](./rot13.rs)
31+
![alt text][rot13]
32+
33+
**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>
34+
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>
35+
The algorithm provides virtually no cryptographic security, and is often cited as a canonical example of weak encryption.
36+
37+
The transformation can be done using a lookup table, such as the following:
38+
39+
```
40+
| Input | ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz |
41+
| Output | NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm |
42+
```
43+
44+
###### Source: [Wikipedia](https://en.wikipedia.org/wiki/ROT13)
45+
3046
[caesar]: https://upload.wikimedia.org/wikipedia/commons/4/4a/Caesar_cipher_left_shift_of_3.svg
47+
[rot13]: https://upload.wikimedia.org/wikipedia/commons/3/33/ROT13_table_with_example.svg

src/ciphers/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
mod caesar;
2+
mod rot13;
23
mod transposition;
34

45
pub use self::caesar::caesar;
6+
pub use self::rot13::rot13;
57
pub use self::transposition::transposition;

src/ciphers/rot13.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//! Rot13 or "rotate by 13 places"
2+
//!
3+
//! # Algorithm
4+
5+
/// Encrypts a given [`&str`] using ROT13 cipher.
6+
///
7+
/// See [ROT13](https://en.wikipedia.org/wiki/ROT13) for more information.
8+
///
9+
/// Replaces each character with the 13th letter after it in the alphabet.
10+
/// Rot13 is a special case of [Caesar cipher](https://en.wikipedia.org/wiki/Caesar_cipher)
11+
///
12+
/// The most basic example is ROT 13, which rotates 'a' to 'n'.
13+
/// This implementation does not rotate unicode characters.
14+
///
15+
/// # Arguments
16+
///
17+
/// `text` - String to transform.
18+
///
19+
/// # Returns
20+
///
21+
/// An owned [`String`]
22+
///
23+
/// # Panic
24+
///
25+
/// This function won't panic.
26+
///
27+
/// # Examples
28+
/// ```
29+
/// # use rust_algorithms::ciphers::rot13;
30+
/// let encoded = rot13("hello world");
31+
/// assert_eq!(encoded, "URYYB JBEYQ");
32+
/// ```
33+
pub fn rot13(text: &str) -> String {
34+
let to_enc = text.to_uppercase();
35+
to_enc
36+
.chars()
37+
.map(|c| match c {
38+
'A'..='M' => ((c as u8) + 13) as char,
39+
'N'..='Z' => ((c as u8) - 13) as char,
40+
_ => c,
41+
})
42+
.collect()
43+
}
44+
45+
#[cfg(test)]
46+
mod test {
47+
use super::rot13;
48+
49+
#[test]
50+
fn test_single_letter() {
51+
assert_eq!("N", rot13("A"));
52+
}
53+
54+
#[test]
55+
fn test_bunch_of_letters() {
56+
assert_eq!("NOP", rot13("ABC"));
57+
}
58+
59+
#[test]
60+
fn test_non_ascii() {
61+
assert_eq!("😀NO", rot13("😀AB"));
62+
}
63+
64+
#[test]
65+
fn test_twice() {
66+
assert_eq!("ABCD", rot13(&rot13("ABCD")));
67+
}
68+
}

0 commit comments

Comments
 (0)