freechat/server/src/rand_utils.rs
shockrah e4f03b951e invites:
handling result from diesel
generally:
fixed pub/private issue in modles module
renamed rand_utils functions to be snake case
2020-03-08 20:07:26 -07:00

27 lines
827 B
Rust

// This modules mainly deals with creating various types of random data
use getrandom::getrandom;
pub fn new_user_id() -> u64 {
let mut buf = [0u8; 8];
match getrandom::getrandom(&mut buf) { // honestly if this fails idk wtf you want
Ok(_val) => {u64::from_ne_bytes(buf)}
Err(_e) => {0} // if this really happens we're fucked anyway
}
}
pub fn new_key() -> String { // Returns a random string which we later hash with bcrypt
let mut raw_slice = [0u8; 32];
if let Ok(_ignored) = getrandom(&mut raw_slice) {
let mut buf = String::new();
for i in raw_slice.iter() {
let mut cv: u8 = *i;
if cv > 126 { cv %= 126; }
if cv < 33 { cv += 34; }
buf.push(cv as char);
}
buf
}
else {
"".into()
}
}