pub fn rot_letter(mode: Mode, letter: u8, rotation: u8) -> u8
Expand description
Transform single letter to ROT
The letter after rotated/shifted by n from its initial value
use nrot::{rot_letter, Mode};
let rotation = 13;
let input = b'a';
let result = b'n';
let encrypted = rot_letter(Mode::Encrypt, input, rotation);
assert_eq!(result, encrypted);
let input = b'n';
let result = b'a';
let encrypted = rot_letter(Mode::Decrypt, input, rotation);
assert_eq!(result, encrypted);
Examples found in repository?
examples/nrot.rs (line 10)
3fn encrypt(input: String) {
4 let rotation = 13;
5
6 let input_length = input.len();
7 let input_bytes = input.as_bytes();
8
9 if input_length == 1 {
10 let byte_result = rot_letter(Mode::Encrypt, input_bytes[0], rotation);
11 println!("{}", String::from_utf8_lossy(&[byte_result]))
12 } else {
13 let bytes_result = rot(Mode::Encrypt, input_bytes, rotation);
14 println!("{}", String::from_utf8_lossy(&bytes_result))
15 };
16}
17
18fn decrypt(input: String) {
19 let rotation = 13;
20
21 let input_length = input.len();
22 let input_bytes = input.as_bytes();
23
24 if input_length == 1 {
25 let byte_result = rot_letter(Mode::Decrypt, input_bytes[0], rotation);
26 println!("{}", String::from_utf8_lossy(&[byte_result]))
27 } else {
28 let bytes_result = rot(Mode::Decrypt, input_bytes, rotation);
29 println!("{}", String::from_utf8_lossy(&bytes_result))
30 };
31}