Passwords
This crate provides useful tools to generate multiple readable passwords, as well as analyze and score them.
Generator
PasswordGenerator
can be used for generating passwords which consist optional numbers, lowercase letters, uppercase letters, symbols and spaces.
use PasswordGenerator;
let pg = PasswordGenerator ;
println!;
println!;
It also has a fluent interface.
use PasswordGenerator;
let pg = new.length.numbers.lowercase_letters.uppercase_letters.symbols.spaces.exclude_similar_characters.strict;
println!;
println!;
The generate
method has been optimized for multiple generation. Don't reuse the generate_one
method to generate multiple passwords. If the count of passwords can't be determined, use the try_iter
method to create a PasswordGeneratorIter
instance which implements the Iterator
trait and can re-generate passwords more efficiently.
use PasswordGenerator;
let pgi = new.try_iter.unwrap;
println!;
println!;
use PasswordGenerator;
let mut pgi = new.try_iter.unwrap;
println!;
println!;
Hasher
To enable hashing functions, you need to enable the crypto feature.
[]
= "*"
= ["crypto"]
Then, bcrypt
, identify_bcrypt
, bcrypt_format
, identify_bcrypt_format
, get_password_with_null_terminated_byte
and gen_salt
functions in the hasher
module are available.
use hasher;
let salt = gen_salt;
let hashed = bcrypt.unwrap;
assert!;
let mcf = bcrypt_format.unwrap;
assert!;
Analyzer
The analyze
function in the analyzer
module can be used to create a AnalyzedPassword
instance which contains some information about the input password.
Typically, we don't want our readable password to contain control characters like BS, LF, CR, etc. Before the analyzer analyzes a password, it filters the password in order to remove its control characters. And after analyzing, the analyzer will return the filtered password. Therefore, you can use this analyzer as a password guard before you store the input password (or generally hash it first and then store) to your database.
use analyzer;
let password = "ZYX[$BCkQB中文}%A_3456] H(\rg";
let analyzed = analyze;
assert_eq!; // "\r" was filtered
assert_eq!; // Characters' length, instead of that of UTF-8 bytes
assert_eq!; // Two spaces between "]" and "H"
assert_eq!; // Numbers are "3456"
assert_eq!; // Lowercase letters are "k" and "g"
assert_eq!; // Uppercase letters are "ZYX", "BC", "QB", "A" and "H"
assert_eq!; // Symbols are "[$", "}%", "_", "]" and "("
assert_eq!; // Other characters are "中文". These characters are usually not included on the rainbow table.
assert_eq!; // Consecutive repeated characters are " " (two spaces)
assert_eq!; // Non-consecutive repeated characters are "B" (appears twice)
assert_eq!; // Progressive characters are "ZYX" and "3456". "BC" is not counted, because its length is only 2, not three or more.
You can also check whether a password is too simple and dangerous to use, by looking up a common passwords table. If you want to do that, you need to enable the common-password feature.
[]
= "*"
= ["common-password"]
Then, the is_common_password
function in analyzer
module and the is_common
method of a AnalyzedPassword
instance are available.
You should notice that after you enable the common-password feature, the time for compiling increases dramatically, because the common passwords table will be compiled into the executable binary file as a hardcode array.
Scorer
After analyzing a password, you can use the score
function in the scorer
module to score it.
use analyzer;
use scorer;
assert_eq!;
assert_eq!;
if cfg! else
A password whose score is,
- 0 ~ 20 is very dangerous (may be cracked within few seconds)
- 20 ~ 40 is dangerous
- 40 ~ 60 is very weak
- 60 ~ 80 is weak
- 80 ~ 90 is good
- 90 ~ 95 is strong
- 95 ~ 99 is very strong
- 99 ~ 100 is invulnerable
Crates.io
https://crates.io/crates/passwords