passgenlib/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
mod gen_engine;
/// Main [Passgen] structure.
///
/// # Examples
///
/// You can create a strong token including all leterals, numbers and symbols:
///
/// ```
/// use passgenlib::Passgen;
/// let result = Passgen::default().generate(30);
/// ```
///
/// You can create a strong and usability password:
///
/// ```
/// use passgenlib::Passgen;
/// let result = Passgen::default_strong_and_usab().generate(8);
/// ```
/// You can create a set from your custom charset:
///
/// ```
/// use passgenlib::Passgen;
/// let result = Passgen::new().set_custom_charset("bla@.321").generate(8);
/// ```
pub struct Passgen {
/// Presence of letters.
pub enab_letters: bool,
/// Presence of a capital letters.
pub enab_u_letters: bool,
/// Presence of numeric characters.
pub enab_num: bool,
/// Presence of special characters.
pub enab_spec_symbs: bool,
/// Including all characters, but
/// the first position in the password is a capital or small letter,
/// the last position is the symbol. Excluded ambiguous characters `"0oOiIlL1"`.
///
/// ⚠️ If this rule is enabled, the other consistency rules of the generating are not taken,
/// except for a rule `custom_charset`.
pub enab_strong_usab: bool,
/// User defined character set.
///
/// ⚠️ This set of characters will exclude all other rules.
pub custom_charset: &'static str,
}
impl Passgen {
/// Get an instance of `Passgen` without any rules.
pub fn new() -> Passgen {
Passgen {
enab_letters: false,
enab_u_letters: false,
enab_num: false,
enab_spec_symbs: false,
enab_strong_usab: false,
custom_charset: "",
}
}
/// Set default ruleset of `Passgen` to *"all simple rules are enabled"*.
pub fn default() -> Passgen {
Passgen {
enab_letters: true,
enab_u_letters: true,
enab_num: true,
enab_spec_symbs: true,
enab_strong_usab: false,
custom_charset: "",
}
}
/// Set default ruleset of `Passgen` to *"Strong & usability"*.
///
/// Including all characters, but
/// the first position in the password is a capital or small letter,
/// the last position is the symbol. Excluded ambiguous characters `"0oOiIlL1"`.
///
/// ⚠️ If this rule is enabled, the other consistency rules of the generating are not taken,
/// except for a rule `custom_charset`.
pub fn default_strong_and_usab() -> Passgen {
Passgen {
enab_letters: false,
enab_u_letters: false,
enab_num: false,
enab_spec_symbs: false,
custom_charset: "",
enab_strong_usab: true,
}
}
/// Set value of the field `enab_letters` for `Passgen`.
pub fn set_enabled_letters(&mut self, value: bool) -> &mut Passgen {
self.enab_letters = value;
self
}
/// Set value of the field `enab_u_letters` for `Passgen`.
pub fn set_enabled_uppercase_letters(&mut self, value: bool) -> &mut Passgen {
self.enab_u_letters = value;
self
}
/// Set value of the field `enab_num` for `Passgen`.
pub fn set_enabled_numbers(&mut self, value: bool) -> &mut Passgen {
self.enab_num = value;
self
}
/// Set value of the field `enab_spec_symbs` for `Passgen`.
pub fn set_enabled_spec_symbols(&mut self, value: bool) -> &mut Passgen {
self.enab_spec_symbs = value;
self
}
/// Set value of the field `enab_strong_usab` for `Passgen`.
///
/// Including all characters, but
/// the first position in the password is a capital or small letter,
/// the last position is the symbol. Excluded ambiguous characters `"0oOiIlL1"`.
///
/// ⚠️ If this rule is enabled, the other consistency rules of the generating are not taken,
/// except for a rule `custom_charset`.
pub fn set_enabled_strong_usab(&mut self, value: bool) -> &mut Passgen {
self.enab_strong_usab = value;
self
}
/// Set user defined character set.
///
/// ⚠️ This set of characters will exclude all other rules.
pub fn set_custom_charset(&mut self, value: &'static str) -> &mut Passgen {
self.custom_charset = value;
self
}
/// Generate result. Argument "length" will not be less than 4
pub fn generate(&mut self, length: u32) -> String {
if !self.is_ruleset_clean() {
let res_len = if length < 4 { 4 } else { length };
let mut pwd = self.generate_pass(res_len);
if self.custom_charset.len() == 0 {
while !self.is_valid_pwd_by_consist(pwd.clone()) {
pwd = self.generate_pass(res_len);
}
}
pwd
} else {
"".to_string()
}
}
fn is_ruleset_clean(&self) -> bool {
!self.enab_letters
&& !self.enab_u_letters
&& !self.enab_num
&& !self.enab_spec_symbs
&& !self.enab_strong_usab
&& self.custom_charset.len() == 0
}
}
#[cfg(test)]
mod tests {
use crate::Passgen;
#[test]
fn it_works() {
assert_eq!(Passgen::new().generate(4).len(), 0);
assert_ne!(
Passgen::new().set_enabled_letters(true).generate(4).len(),
0
);
assert_ne!(
Passgen::default()
.set_custom_charset("bla@321.")
.generate(4)
.len(),
0
);
assert_ne!(Passgen::default_strong_and_usab().generate(4).len(), 0);
}
}