moved rand_utils::new_key to utils crate

This commit is contained in:
shockrah 2020-05-10 13:06:54 -07:00
parent bd45508584
commit 0340204141
3 changed files with 14 additions and 16 deletions

View File

@ -1,15 +0,0 @@
// This modules mainly deals with creating various types of random data
use getrandom::getrandom;
pub fn new_key() -> String {
let mut raw_slice = [0u8; 64];
let _ignored = getrandom(&mut raw_slice).unwrap();
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
}

View File

@ -4,7 +4,7 @@ use rocket_contrib::json::{Json, JsonValue};
use diesel::{self, prelude::*};
use diesel::result::Error;
use crate::rand_utils::new_key;
use crate::utils::new_key;
use crate::models::{User, USER_OFFLINE};
use crate::{DBConn, schema};

View File

@ -1,5 +1,6 @@
// Generic functions used by pretty much everyone
use base64::{encode_config, decode_config, URL_SAFE};
use getrandom::getrandom;
use std::str;
@ -16,6 +17,18 @@ pub fn decode_param(enc: &str) -> Result<String, i32> {
}
}
pub fn new_key() -> String {
let mut raw_slice = [0u8; 64];
let _ignored = getrandom(&mut raw_slice).unwrap();
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
}
#[cfg(test)]
mod encoder_decoder_tests {
use super::*;