Created
November 3, 2015 22:04
-
-
Save anonymous/ab70820490f3f3861f34 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
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
// Caesar's cipher implemented in Rust | |
// Made by Xinayder with the help of folks from #rust at irc.mozilla.org | |
// | |
#![feature(convert)] | |
fn encrypt(msg: &str, shift: i32) -> String { | |
let alphabet_upper: Vec<(usize, char)> = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".char_indices().collect(); | |
let chars: Vec<char> = msg.chars().collect(); | |
let mut result_raw: Vec<char> = Vec::new(); | |
for c in chars { | |
if c.is_whitespace() { | |
result_raw.push(c); | |
} | |
let mut new_index: usize; | |
let pos: Option<&(usize, char)> = alphabet_upper.iter().find(|&&(_, b)| c == b); | |
match pos { | |
Some(x) => { | |
let index = x.0; | |
new_index = shift as usize + index; | |
if (new_index as i32) < 0i32 { | |
new_index += 26usize; | |
} else if (new_index as i32) >= 26i32 { | |
new_index -= 26usize; | |
} | |
let pos: Option<&(usize, char)> = alphabet_upper.iter().find(|&&(a, _)| new_index == a); | |
match pos { | |
Some(x) => { | |
let (_, e) = *x; | |
result_raw.push(e); | |
println!("New char is: {}", e); | |
}, | |
None => { }, | |
}; | |
}, | |
None => { }, | |
}; | |
} | |
println!("A: {:?}", result_raw); | |
let result = result_raw.into_iter().collect::<String>(); | |
return result; | |
} | |
// DON'T USE THIS FUNCTION | |
// OVERFLOWS ARITHMETIC OPERATIONS | |
fn decrypt(msg: &str, shift: i32) -> String { | |
let alphabet_upper: Vec<(usize, char)> = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".char_indices().collect(); | |
let chars: Vec<char> = msg.chars().collect(); | |
let mut result_raw: Vec<char> = Vec::new(); | |
for c in chars { | |
if c.is_whitespace() { | |
result_raw.push(c); | |
} | |
let mut new_index: usize; | |
let pos: Option<&(usize, char)> = alphabet_upper.iter().find(|&&(_, b)| c == b); | |
match pos { | |
Some(x) => { | |
let index = x.0; | |
new_index = shift as usize - index; | |
if (new_index as i32) < 0i32 { | |
new_index += 26usize; | |
} else if (new_index as i32) >= 26i32 { | |
new_index -= 26usize; | |
} | |
let pos: Option<&(usize, char)> = alphabet_upper.iter().find(|&&(a, _)| new_index == a); | |
match pos { | |
Some(x) => { | |
let (_, e) = *x; | |
result_raw.push(e); | |
println!("New char is: {}", e); | |
}, | |
None => { }, | |
}; | |
}, | |
None => { }, | |
}; | |
} | |
println!("A: {:?}", result_raw); | |
let result = result_raw.into_iter().collect::<String>(); | |
return result; | |
} | |
fn main() { | |
let msg: &str = "FOX"; | |
let shift = 5; | |
let encrypted: String = encrypt(msg, shift); | |
println!("{} in a shift of {} is: {}", msg, shift, encrypted); | |
//println!("Decrypted is: {}", decrypt(encrypted.as_str(), shift)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment