1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use crate::error::*;
use rc_crypto::rand;
use std::time::{SystemTime, UNIX_EPOCH};
pub fn now() -> u64 {
let since_epoch = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("Something is very wrong.");
since_epoch.as_secs() * 1000 + u64::from(since_epoch.subsec_nanos()) / 1_000_000
}
pub fn now_secs() -> u64 {
let since_epoch = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("Something is very wrong.");
since_epoch.as_secs()
}
pub fn random_base64_url_string(len: usize) -> Result<String> {
let mut out = vec![0u8; len];
rand::fill(&mut out)?;
Ok(base64::encode_config(&out, base64::URL_SAFE_NO_PAD))
}
pub trait Xorable {
fn xored_with(&self, other: &[u8]) -> Result<Vec<u8>>;
}
impl Xorable for [u8] {
fn xored_with(&self, other: &[u8]) -> Result<Vec<u8>> {
if self.len() != other.len() {
Err(ErrorKind::XorLengthMismatch(self.len(), other.len()).into())
} else {
Ok(self
.iter()
.zip(other.iter())
.map(|(&x, &y)| x ^ y)
.collect())
}
}
}