utils.rs (740B)
1 use rand::Rng; 2 use std::time::{SystemTime, UNIX_EPOCH}; 3 4 /// Get actual timestamp in seconds 5 /// # Example 6 /// ```rust 7 /// use nostr_rust::utils::get_timestamp; 8 /// 9 /// let timestamp = get_timestamp(); 10 /// assert!(timestamp > 0); 11 /// ``` 12 pub fn get_timestamp() -> u64 { 13 let now = SystemTime::now(); 14 let since_the_epoch = now.duration_since(UNIX_EPOCH).expect("Time went backwards"); 15 since_the_epoch.as_secs() 16 } 17 18 /// Random sha256 hash 19 /// # Example 20 /// ```rust 21 /// use nostr_rust::utils::random_hash; 22 /// let hash = random_hash(); 23 /// assert_eq!(hash.len(), 64); 24 /// ``` 25 pub fn random_hash() -> String { 26 let mut rng = rand::thread_rng(); 27 let mut bytes = [0u8; 32]; 28 rng.fill(&mut bytes); 29 sha256::digest(&bytes) 30 }