Created
October 20, 2021 08:40
-
-
Save shkumagai/2212068848ebe1890a68e21edc14f84b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import base64 | |
import secrets | |
def get_random_string(length: int, allowed_chars: str = ( | |
"abcdefghijklmnopqrstuvwxyz" | |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
"0123456789" | |
)) -> str: | |
length = 12 if not length else length | |
return "".join(secrets.choice(allowed_chars) for i in range(length)) | |
def get_random_string_key() -> str: | |
chars = ( | |
"abcdefghijklmnopqrstuvwxyz" | |
"0123456789!@#$%^&*(-_=+)" | |
) | |
return get_random_string(64, chars) | |
def main(prefix, limit) -> None: | |
print(key_string := f"{prefix}:{get_random_string_key()}"[:limit]) | |
print(base64.urlsafe_b64encode(key_string.encode("ascii")).decode("ascii")) | |
if __name__ == "__main__": | |
from argparse import ArgumentParser | |
p = ArgumentParser() | |
p.add_argument("prefix", help="specify the prefix word") | |
p.add_argument( | |
"limit", | |
type=int, | |
default=64, | |
help="limit length for generated string" | |
) | |
args = p.parse_args() | |
main(args.env, args.limit) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment