cryptopals/set1/chal1/src/main.rs

34 lines
1.0 KiB
Rust

////
// Cryptopal set 1 chal 1 : https://cryptopals.com/sets/1/challenges/1
// Run with :
// cargo run -- 49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d
////
use std::env;
fn hex_to_char(s: &str) -> Result<char, std::num::ParseIntError> {
u8::from_str_radix(s, 16).map(|n| n as char)
}
fn main() {
let args: Vec<String> = env::args().collect();
let input = &args[1];
let char_vec: Vec<char> = input.chars().collect();
let split = &char_vec.chunks(2)
.map(|chunk| chunk.iter().collect::<String>())
.collect::<Vec<_>>();
// Printing the string in Ascii format - for decoding example
for (i, s) in split.iter().enumerate() {
match hex_to_char(s) {
Ok(s) => print!("{}", s),
Err(e) => println!("\nError decoding char '{}' at index {}", e, i),
}
}
println!();
// Now we can unroll the base64 algorithm
// We need to : http://www.herongyang.com/Encoding/Base64-Encoding-Algorithm.html
}