user generation added

randomness is generated via /dev/urandom thru getrandom crate
This commit is contained in:
shockrah 2020-03-08 19:13:24 -07:00
parent ec88a08391
commit e9a880f6a9

42
server/src/users.rs Normal file
View File

@ -0,0 +1,42 @@
use chrono:Utc;
use payload::NewUserInfo;
use getrandom::getrandom;
use bcrypt::{hash, DEFAULT_COST};
fn rand_string() -> Option<String> {
// Returns a random string which we later hash with bcrypt
let mut raw_slice = [0u8; 32];
if let Some(_ignored) = getrandom(raw_slice) {
let mut buf = String::new();
for i in raw_slice {
let mut cv: u8 = *i;
if cv > 126 { cv %= 126; }
if cv < 33 { cv += 34; }
buf.push(cv as char);
}
Some(buf)
}
else {
None
}
}
fn rand_uid() -> u64 {
let mut buf = [0u8;8];
let _r = getrandom(&mut buf); // this basically never fails hehexd
u64::from_ne_bytes(buf)
}
// Returns a struct of payload::NewUserInfo
pub fn createNewUser() -> NewUserInfo {
let uid = rand_id();
let uname = format!("User: {}", uid);
let hashkey = bcrypt(rand_string(), DEFAULT_COST);
// Fuck it
NewUserInfo {
userid: uid,
username: uname,
key_hash: hashkey,
valid: true
}
}