Created
June 9, 2024 08:51
-
-
Save ravener/7b1fd28325f92fce0900c8257e67a35f to your computer and use it in GitHub Desktop.
ROT13 in Python and Rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # -*- coding: utf8 -*- | |
| import sys | |
| def generate_table() -> dict[str, str]: | |
| """ | |
| Generates a look up table for mapping characters to their ROT13 character. | |
| """ | |
| input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" | |
| output = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm" | |
| return dict(zip(input, output)) | |
| def main() -> None: | |
| if len(sys.argv) < 2: | |
| print("Usage: rot13.py <string>") | |
| sys.exit(1) | |
| string = " ".join(sys.argv[1:]) | |
| lookup_table = generate_table() | |
| output = "".join(map(lambda c: lookup_table.get(c, c), string)) | |
| print(output) | |
| if __name__ == "__main__": | |
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //! ROT13 (Rotate13, "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 Latin alphabet. | |
| //! ROT13 is a special case of the Caesar cipher which was developed in ancient Rome. | |
| //! | |
| //! 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. | |
| //! The algorithm provides virtually no cryptographic security, | |
| //! and is often cited as a canonical example of weak encryption. | |
| //! | |
| //! ROT13 has been used in online forums as a means of hiding spoilers, | |
| //! punchlines, puzzle solutions, and offensive materials from the casual glance. | |
| //! ROT13 has inspired a variety of letter and word games online, and is frequently mentioned in newsgroup conversations. | |
| //! | |
| //! https://en.wikipedia.org/wiki/ROT13 | |
| use std::collections::HashMap; | |
| use std::env; | |
| use std::process; | |
| fn generate_table() -> HashMap<char, char> { | |
| let input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; | |
| let output = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"; | |
| input.chars().zip(output.chars()).collect() | |
| } | |
| fn main() { | |
| let args: Vec<String> = env::args().collect(); | |
| if args.len() < 2 { | |
| eprintln!("Usage: rot13 <string>"); | |
| process::exit(1); | |
| } | |
| let input = args[1..].join(" "); | |
| let lookup_table = generate_table(); | |
| let output: String = input | |
| .chars() | |
| .map(|c| lookup_table.get(&c).copied().unwrap_or(c)) | |
| .collect(); | |
| println!("{}", output); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment