bpay/
utils.rs

1//! Utilities functions commonly used in the project.
2use rand;
3use rand::Rng;
4use std::time::{SystemTime, UNIX_EPOCH};
5
6const CHARS: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
7
8/// Creates the random string of the given length.
9pub fn create_nonce(length: usize) -> String {
10    let mut rng = rand::thread_rng();
11    let nonce = (0..length)
12        .map(|_| CHARS[rng.gen_range(0..CHARS.len())] as char)
13        .collect();
14    nonce
15}
16
17/// Generates the current timestamp in milliseconds.
18pub fn get_current_timestamp() -> u128 {
19    let start = SystemTime::now();
20    let since_the_epoch = start
21        .duration_since(UNIX_EPOCH)
22        .expect("Time went backwards");
23    since_the_epoch.as_millis()
24}