#![allow(dead_code)] use rand::{Rng, RngCore}; use std::fs::File; use std::io::prelude::*; pub fn read_as_hex_lines(filename: &str) -> Vec> { let fh = File::open(filename).unwrap(); return std::io::BufReader::new(fh) .lines() .map(|line| hex::decode(line.unwrap()).unwrap()) .collect(); } pub fn read_as_base64_lines(filename: &str) -> Vec> { let fh = File::open(filename).unwrap(); return std::io::BufReader::new(fh) .lines() .map(|line| base64::decode(&line.unwrap()).unwrap()) .collect(); } pub fn read_as_lines(filename: &str) -> Vec> { let fh = File::open(filename).unwrap(); return std::io::BufReader::new(fh) .lines() .map(|line| line.unwrap().as_bytes().to_vec()) .collect(); } pub fn read_as_base64(filename: &str) -> Vec { let fh = File::open(filename).unwrap(); return std::io::BufReader::new(fh) .lines() .map(|line| base64::decode(&line.unwrap()).unwrap()) .collect::>>() .concat(); } pub fn read(filename: &str) -> Vec { let outfh = File::open(filename).unwrap(); return outfh.bytes().map(|c| c.unwrap()).collect(); } pub fn write(filename: &str, data: &[u8]) { let mut outfh = File::create(filename).unwrap(); outfh.write(data).unwrap(); } pub fn random_aes_128_key() -> [u8; 16] { let mut key = [0; 16]; rand::thread_rng().fill_bytes(&mut key); return key; } pub fn coinflip() -> bool { rand::thread_rng().gen() } pub fn now() -> u32 { return time::now().to_timespec().sec as u32; }