runtime/
utils.rs

1// Copyright 2020-2021 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use random::{distributions::Alphanumeric, thread_rng, Rng, RngCore};
5use zeroize::Zeroizing;
6
7pub fn xor(data: &mut [u8], payload: &[u8], noise: &[u8], size: usize) {
8    for i in 0..size {
9        data[i] = noise[i] ^ payload[i];
10    }
11}
12
13pub fn xor_mut(payload: &mut [u8], noise: &[u8], size: usize) {
14    for i in 0..size {
15        payload[i] ^= noise[i];
16    }
17}
18
19pub fn random_vec(size: usize) -> Zeroizing<Vec<u8>> {
20    let mut rng = thread_rng();
21    let mut v = Zeroizing::new(vec![0u8; size]);
22    rng.fill_bytes(&mut v);
23
24    v
25}
26
27// Creates random file name and join it to the storing directory
28pub fn random_fname(size: usize) -> String {
29    let fname: String = thread_rng()
30        .sample_iter(&Alphanumeric)
31        .take(size)
32        .map(char::from)
33        .collect();
34    fname
35}