From e588ed13aa2d3e621e2605e20bb417d2bed8ac25 Mon Sep 17 00:00:00 2001 From: beneth Date: Mon, 13 Apr 2020 17:39:08 +0200 Subject: [PATCH] Move base64 encode to a function --- set1/chal1/src/main.rs | 107 ++++++++++++++++++++++------------------- 1 file changed, 58 insertions(+), 49 deletions(-) diff --git a/set1/chal1/src/main.rs b/set1/chal1/src/main.rs index 5c9b7cd..3b74cb7 100644 --- a/set1/chal1/src/main.rs +++ b/set1/chal1/src/main.rs @@ -5,11 +5,6 @@ //// use std::env; -use std::convert::TryInto; - -static BASE64_TABLE: &'static [char] = &['A','B','C','D','E','F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V' ,'W' ,'X' ,'Y' ,'Z', - 'a','b','c','d','e','f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v' ,'w' ,'x' ,'y' ,'z', - '0','1','2','3','4','5', '6', '7', '8', '9', '+', '/']; /// Convert Hex String to char /// @@ -23,6 +18,63 @@ fn hex_to_char(s: &str) -> Result { u8::from_str_radix(s, 16).map(|n| n as char) } +/// Encode a Vector of bytes to Base64 and return the base 64 String +fn base64_encode(input: &Vec) -> String { + static BASE64_TABLE: &'static [char] = &['A','B','C','D','E','F', 'G', 'H', + 'I', 'J', 'K', 'L', 'M', 'N', 'O', + 'P', 'Q', 'R', 'S', 'T', 'U', 'V', + 'W' ,'X' ,'Y' ,'Z', + 'a','b','c','d','e','f', 'g', 'h', + 'i', 'j', 'k', 'l', 'm', 'n', 'o', + 'p', 'q', 'r', 's', 't', 'u', 'v', + 'w' ,'x' ,'y' ,'z', + '0','1','2','3','4','5', '6', '7', + '8', '9', '+', '/']; + let mut output = String::new(); + for j in (0..input.len()).step_by(3) { + let mut pad = 0; + let mut arr = [0u8;4]; + arr[0] = 0; + + for i in 0..3 { + if i+j == input.len() { + arr[i+1] = 0; + pad = 1; + } else if i+j > input.len() { + arr[i+1] = 0; + pad = 2; + } else { + arr[i+1] = input[i+j]; + } + } + let tmp = u32::from_be_bytes(arr); + + let tmp1 = (tmp>>18) & 0x3F; + let tmp2 = (tmp>>12) & 0x3F; + + output.push(BASE64_TABLE[tmp1 as usize]); + output.push(BASE64_TABLE[tmp2 as usize]); + + if pad == 2 { + output.push_str("=="); + break + } else { + let tmp3 = (tmp>>6) & 0x3F; + output.push(BASE64_TABLE[tmp3 as usize]); + } + + if pad == 1 { + output.push_str("="); + break + } else { + let tmp4 = (tmp) & 0x3F; + output.push(BASE64_TABLE[tmp4 as usize]); + } + } + + output +} + fn main() { let mut input_str = String::new(); let args: Vec = env::args().collect(); @@ -51,50 +103,7 @@ fn main() { .map(|i| u8::from_str_radix(i, 16).unwrap()) .collect(); - // TODO ! - // TODO: Make not ugly and move to function b64_encode(Vec) -> str - // TODO ! - let mut output = String::new(); - for j in (0..hex.len()).step_by(3) { - let mut pad = 0; - let mut arr = [0u8;4]; - arr[0] = 0; - - for i in 0..3 { - if i+j == hex.len() { - arr[i+1] = 0; - pad = 1; - } else if i+j > hex.len() { - arr[i+1] = 0; - pad = 2; - } else { - arr[i+1] = hex[i+j]; - } - } - let tmp = u32::from_be_bytes(arr); - - let tmp1:usize = ((tmp>>18) & 0x3F).try_into().unwrap(); - let tmp2:usize = ((tmp>>12) & 0x3F).try_into().unwrap(); - - output.push(BASE64_TABLE[tmp1]); - output.push(BASE64_TABLE[tmp2]); - - if pad == 2 { - output.push_str("=="); - break - } else { - let tmp3:usize = ((tmp>>6) & 0x3F).try_into().unwrap(); - output.push(BASE64_TABLE[tmp3]); - } - - if pad == 1 { - output.push_str("="); - break - } else { - let tmp4:usize = ((tmp) & 0x3F).try_into().unwrap(); - output.push(BASE64_TABLE[tmp4]); - } - } + let output = base64_encode(&hex); println!("→ Input string is « {} »", input); println!("→ Hex String to ASCII « {} »", &input_str);