passt

passt is a CLI tool and library for generating random strings to be used as password.

The goal with this small project was to build a usable and useful CLI tool and library using Rust. It was my first time having the code for a CLI and library in one project.

The goal with this was to have as little dependencies as possible. I managed to only use functions from the std library, though there’s a hard dependency on /dev/urandom (Unix) or file:/dev/urandom (Windows) for random number generation.

In the process of building the tool I learned how to use configs to run code depending on the OS it is executed on, as shown in the following example.

#[cfg(target_os = "windows")]
let mut f = File::open("file:/dev/urandom").unwrap();
#[cfg(not(target_os = "windows"))]
let mut f = File::open("/dev/urandom").unwrap();

let mut buf = [0u8; 32];
f.read_exact(&mut buf).unwrap();

Library

To use passt in your project, add it to the dependencies in Cargo.toml like so

[dependencies]
passt = "0.3.0"

Then add it to your code

use passt::Passt;

// create random password with 16 characters, do not include special characters
fn my_random_string() -> String {
  Passt::random_password(16, Some(false));
}

CLI

The CLI can be installed with cargo. Run cargo install passt, then run passt -h to see all options:

$ passt -h

USAGE: passt -l <int> [-s] [-chars "<str>"] [-n <int>]

-l      length of the generated password
-n      number of passwords to create (default: 1)
-s      use special characters
-chars  possible characters as a string, e.g. "abc012"
Sourcehttps://github.com/KevinGimbel/passt
lib.rshttps://lib.rs/crates/passt
Docshttps://docs.rs/passt/latest/passt/