Skip to content

Commit

Permalink
generate random passwords using minclass
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoge1001 committed Oct 12, 2024
1 parent a8f6940 commit d36e3da
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cloudinit/config/cc_set_passwords.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,4 @@ def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None:


def rand_user_password(pwlen=20):
return util.rand_str(pwlen, select_from=PW_SET)
return util.rand_str_minclass(pwlen, select_from=PW_SET, minclass=3)
30 changes: 30 additions & 0 deletions cloudinit/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,36 @@ def rand_str(strlen=32, select_from=None):
return "".join([r.choice(select_from) for _x in range(strlen)])


def rand_str_minclass(strlen=32, select_from=None, minclass=3):
res_rand_list = [
random.choice(string.ascii_digits),
random.choice(string.ascii_lowercase),
random.choice(string.ascii_uppercase),
]
if minclass > 3:
res_rand_list.extend(random.choice(string.punctuation))
if select_from:
select_from += string.punctuation

res_rand_list = random.sample(res_rand_list, minclass)
res_rand_list.extend(list(rand_str(strlen-minclass, select_from)))
random.shuffle(res_rand_list)
return "".join(res_rand_list)


def get_str_class_num(str):
str_class_num = 0
if any(c.islower() for c in str):
str_class_num += 1
if any(c.isupper() for c in str):
str_class_num += 1
if any(c.isdigit() for c in str):
str_class_num += 1
if any(c in string.punctuation for c in str):
str_class_num += 1
return str_class_num


def rand_dict_key(dictionary, postfix=None):
if not postfix:
postfix = ""
Expand Down

0 comments on commit d36e3da

Please sign in to comment.