Last active
April 19, 2016 00:00
-
-
Save travis-bradbury/f5b2236a169193f823fcf775c99b7273 to your computer and use it in GitHub Desktop.
Cryptopals set1/challenge1 main.rs
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
| extern crate rustc_serialize; | |
| /// Converts its argument from hex to base64. | |
| /// | |
| /// # Examples | |
| /// `$ challenge1 "af"` | |
| fn main() { | |
| let args: Vec<String> = std::env::args().collect(); | |
| let input: &String = match args.get(1) { | |
| Some(x) => x, | |
| None => { | |
| println!("You must provide a hex argument."); | |
| std::process::exit(1); | |
| } | |
| }; | |
| let output: String = hex2base64(input); | |
| println!("{}", output); | |
| } | |
| /// Convert a hex string to base64. | |
| /// # Examples | |
| /// | |
| /// ``` | |
| /// assert_eq!("rw==", hextobase64("af")); | |
| /// # fn hex2base64(input String) -> String { | |
| /// # input.hex2bin().bin2base64(); | |
| /// # } | |
| /// ``` | |
| fn hex2base64(input: &String) -> String { | |
| input.hex2bin().bin2base64() | |
| } | |
| trait HexToBinary { | |
| fn hex2bin(&self) -> Vec<u8>; | |
| } | |
| trait BinaryToBase64 { | |
| fn bin2base64(&self) -> String; | |
| } | |
| impl HexToBinary for String { | |
| fn hex2bin(&self) -> Vec<u8> { | |
| use rustc_serialize::hex::{FromHex}; | |
| let bin = match self.from_hex() { | |
| Ok(value) => value, | |
| Err(_) => { | |
| println!("That aint hex."); | |
| std::process::exit(2); | |
| } | |
| }; | |
| bin | |
| } | |
| } | |
| impl BinaryToBase64 for Vec<u8> { | |
| fn bin2base64(&self) -> String { | |
| use rustc_serialize::base64::{ToBase64, MIME}; | |
| let mut config = MIME; | |
| config.line_length = None; | |
| self.to_base64(config) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment