MCPcopy Create free account
hub / github.com/Keeper-Security/Commander / KeeperPasswordGenerator

Class KeeperPasswordGenerator

keepercommander/generator.py:104–195  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

102
103
104class KeeperPasswordGenerator(PasswordGenerator):
105 def __init__(self, length: int = DEFAULT_PASSWORD_LENGTH,
106 symbols: Optional[int] = None,
107 digits: Optional[int] = None,
108 caps: Optional[int] = None,
109 lower: Optional[int] = None,
110 special_characters: str = PW_SPECIAL_CHARACTERS):
111
112 sum_categories = sum((abs(i) if isinstance(i, int) else 0) for i in (symbols, digits, caps, lower))
113 extra_count = length - sum_categories if length > sum_categories else 0
114 extra_chars = ''
115 if symbols is None or isinstance(symbols, int) and symbols > 0:
116 extra_chars += special_characters
117 if digits is None or isinstance(digits, int) and digits > 0:
118 extra_chars += string.digits
119 if caps is None or isinstance(caps, int) and caps > 0:
120 extra_chars += string.ascii_uppercase
121 if lower is None or isinstance(lower, int) and lower > 0:
122 extra_chars += string.ascii_lowercase
123 if extra_count > 0 and not extra_chars:
124 if isinstance(symbols, int) and symbols < 0:
125 extra_chars += special_characters
126 if isinstance(digits, int) and digits < 0:
127 extra_chars += string.digits
128 if isinstance(caps, int) and caps < 0:
129 extra_chars += string.ascii_uppercase
130 if isinstance(lower, int) and lower < 0:
131 extra_chars += string.ascii_lowercase
132
133 if extra_count > 0 and not extra_chars:
134 raise Exception('Password character set is empty')
135 self.category_map = [
136 (abs(symbols) if isinstance(symbols, int) else 0, special_characters),
137 (abs(digits) if isinstance(digits, int) else 0, string.digits),
138 (abs(caps) if isinstance(caps, int) else 0, string.ascii_uppercase),
139 (abs(lower) if isinstance(lower, int) else 0, string.ascii_lowercase),
140 (extra_count, extra_chars)
141 ]
142
143 def generate(self) -> str:
144 password_list = []
145 for count, chars in self.category_map:
146 password_list.extend(choice(chars) for i in range(count))
147 shuffle(password_list)
148 return ''.join(password_list)
149
150 @classmethod
151 def create_from_rules(cls, rule_string: str, length: Optional[int] = None,
152 special_characters: str = PW_SPECIAL_CHARACTERS):
153 """Create instance of class from rules string
154
155 rule_string: comma separated integer character counts of [length,] uppercase, lowercase, numbers, symbols
156 length: length of password
157 special_characters: set of characters used to generate password symbols
158 """
159 try:
160 rule_list = [int(s.strip()) for s in rule_string.split(',')]
161 if len(rule_list) == 5:

Callers 4

generateFunction · 0.85
executeMethod · 0.85
_generate_passwordFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected