diff --git a/server/src/payload.rs b/server/src/payload.rs index 6f71938..4cb967d 100644 --- a/server/src/payload.rs +++ b/server/src/payload.rs @@ -8,6 +8,7 @@ #[derive(Serialize)] pub struct NewUserInfo { pub userid: u64, // userid which later will be discoverable via the - pub key_hash: Option, + pub username: String, + pub key: String, pub valid: bool } \ No newline at end of file diff --git a/server/src/rand_utils.rs b/server/src/rand_utils.rs index 2cd49e4..3ccc04b 100644 --- a/server/src/rand_utils.rs +++ b/server/src/rand_utils.rs @@ -1,4 +1,5 @@ // This modules mainly deals with creating various types of random data +use getrandom; pub fn newUserID() -> u64 { let mut buf = [0u8; 8]; @@ -6,22 +7,21 @@ pub fn newUserID() -> u64 { Ok(_val) => {u64::from_ne_bytes(buf)} Err(_e) => {0} // if this really happens we're fucked anyway } - } -pub fn newKey() -> String { - let mut buf = [0u8; 32]; - getrandom::getrandom(&mut buf)?; - let mut string = String::new(); - for i in buf.iter() { - // if the value isn't in the proper range then we place it in the proper range - // [33-126] - if i < 33 { - i += 33; - } - else if i > 126 { - i %= 126; +pub fn newKey() -> Option { // 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 } - string } \ No newline at end of file diff --git a/server/src/users.rs b/server/src/users.rs index 2be8975..77837a2 100644 --- a/server/src/users.rs +++ b/server/src/users.rs @@ -1,42 +1,17 @@ +use crate::rand_utils::{newUserID, newKey}; use chrono:Utc; use payload::NewUserInfo; use getrandom::getrandom; -use bcrypt::{hash, DEFAULT_COST}; - -fn rand_string() -> Option { - // 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 + let uid = newUserID(); + let uname = format!("User:#{}", uid); + let rstring = newKey(); NewUserInfo { userid: uid, username: uname, - key_hash: hashkey, - valid: true + key: rstring, + valid: true, } } \ No newline at end of file