I've started learning Rust and decided to implement something on my own from scratch. I've implemented a PRNG and I use it to generate random passwords.
Project tree:
| .gitignore
| Cargo.lock
| Cargo.toml
|
+---src
| main.rs
| password.rs
| xorshift.rs
|
main.rs :
use std::io;
mod xorshift;
mod password;
fn main() {
let length : u32;
loop{
let mut pass_leng_str = String::new();
println!("Enter the length of wannable password: ");
io::stdin()
.read_line(&mut pass_leng_str)
.expect("Faild to read the line");
let pass_leng_str = pass_leng_str.trim();
match pass_leng_str.parse::<u32>(){
Ok(i) => {
length = i;
break;
},
Err(..) => {
println!("Not a valid integer!");
},
}
}
println!("Enter the characters you want to exclude: ");
let mut exclude = String::new();
io::stdin()
.read_line(&mut exclude)
.expect("Faild to read the line");
let excluded : Vec<char> = exclude.chars().collect();
println!("{}", password::rand_pass(length, excluded));
}
password.rs :
use super::xorshift;
pub static ASCII : [char; 69] = [
'a', 'b', 'c', 'd', 'e',
'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y',
'z',
'A', 'B', 'C', 'D', 'E',
'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y',
'Z',
'1','2','3','4',
'5','6','7','8',
'9','0',
'+','-','_','?','!','$','/',
];
pub fn rand_pass(pass_len : u32, excluded : Vec<char>)-> String{
let mut password = String::new();
for _i in 0..pass_len{
let c : char = loop{
let c = ASCII[xorshift::get_rand(69) as usize];
if !excluded.contains(&c){
break c
}
};
password.push(c);
}
password
}
xorshift.rs :
use std::time::UNIX_EPOCH;
use std::time::SystemTime;
pub fn get_rand(len : u128) -> u128{
let now = SystemTime::now();
let from_unix = now.duration_since(UNIX_EPOCH).expect("Congrats on time travel!");
let seed = from_unix.as_nanos();
let x = seed;
let x = x ^ seed << 13;
let x = x ^ x >>17;
let x = x ^ x <<5;
let x = x % len;
x as u128
}