use std::num; use rand::Rng; use num_bigint::BigUint; use num_traits::One; /// Convert bytes array to HEX format pub fn to_hex(buf: &[u8]) -> String { let mut result = String::new(); for x in buf.iter() { result.push_str(&format!("{:01$X}", x, 2)); } result } pub fn from_hex(string: &str) -> Result, num::ParseIntError> { split_n(&string.trim()[..], 2) .iter() .map(|b| u8::from_str_radix(b, 16)) .collect() } fn split_n(s: &str, n: usize) -> Vec<&str> { (0..=(s.len() - n + 1) / 2) .map(|i| &s[2 * i..2 * i + n]) .collect() } /// There is no default PartialEq implementation for arrays > 32 in size pub fn same_hash(left: &[u8], right: &[u8]) -> bool { for (x, y) in left.iter().zip(right) { if x != y { return false; } } true } pub fn hash_is_good(hash: &[u8], difficulty: usize) -> bool { let target = BigUint::one() << ((hash.len() << 3) - difficulty); let hash_int = BigUint::from_bytes_be(&hash); return hash_int < target; } /// Generates random string of given length pub fn random_string(length: usize) -> String { let chars: Vec = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!?".chars().collect(); let mut rng = rand::thread_rng(); let mut result = String::with_capacity(length); for _ in 0..length { let position: usize = rng.gen::() % chars.len(); let c: char = *chars.get(position).unwrap(); result.push(c); } result }