58 lines
1.6 KiB
Rust
58 lines
1.6 KiB
Rust
use crate::{bytes::Bytes, bytes_base64::BytesBase64};
|
|
use std::{
|
|
io::{BufRead, BufReader},
|
|
time::{SystemTime, UNIX_EPOCH},
|
|
};
|
|
use openssl::bn::BigNum;
|
|
|
|
pub fn unix_timestamp() -> u32 {
|
|
let start = SystemTime::now();
|
|
let since_the_epoch = start
|
|
.duration_since(UNIX_EPOCH)
|
|
.expect("Time went backwards");
|
|
since_the_epoch
|
|
.as_secs()
|
|
.try_into()
|
|
.expect("Linux time no longer fits into 32 bits.")
|
|
}
|
|
|
|
pub fn read_base64(path: &str) -> Bytes {
|
|
let s = std::fs::read_to_string(path).unwrap();
|
|
match BytesBase64::from_base64(&s) {
|
|
Ok(bytes) => bytes.to_bytes(),
|
|
Err(msg) => panic!("Failed to read_base64 {}: {}", path, msg),
|
|
}
|
|
}
|
|
|
|
pub fn read_base64_lines(path: &str) -> Vec<Bytes> {
|
|
let file = std::fs::File::open(path).unwrap();
|
|
let br = BufReader::new(file);
|
|
br.lines()
|
|
.map(|line| {
|
|
BytesBase64::from_base64(
|
|
&line.unwrap_or_else(|_| panic!("Failed to read line in {}", path)),
|
|
)
|
|
.unwrap_or_else(|_| panic!("Invalid base64 in {}", path))
|
|
.to_bytes()
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
pub fn read_hex_lines(path: &str) -> Vec<Bytes> {
|
|
let file = std::fs::File::open(path).unwrap();
|
|
let br = BufReader::new(file);
|
|
br.lines()
|
|
.map(|line| Bytes::from_hex(&line.unwrap()))
|
|
.collect()
|
|
}
|
|
|
|
pub fn xor(a: &[u8], b: &[u8]) -> Vec<u8> {
|
|
Iterator::zip(a.iter(), b.iter())
|
|
.map(|z| *(z.0) ^ *(z.1))
|
|
.collect()
|
|
}
|
|
|
|
pub fn bnclone(b: &BigNum) -> BigNum {
|
|
BigNum::from_slice(&b.to_vec()).unwrap()
|
|
}
|