dotfiles/bin/genpw

47 lines
809 B
Plaintext
Raw Normal View History

#!/usr/bin/env python
2021-12-11 18:22:49 +00:00
"""
Module to generate a random string
Attributes:
LENGTH (int): Defaults to a length of 16
"""
2021-12-11 18:22:49 +00:00
import argparse
import base64
2021-12-11 18:22:49 +00:00
import os
import sys
LENGTH = 16
2021-12-11 18:22:49 +00:00
def generate(length: int) -> str:
2021-12-11 18:35:10 +00:00
"""
Simple function to call OpenSSL
Args:
length (int): length of string to return
"""
2021-12-11 18:22:49 +00:00
return base64.encodebytes(os.urandom(256)).decode()[:length]
2021-12-11 18:22:49 +00:00
if __name__ == '__main__':
2021-12-11 18:22:49 +00:00
def main() -> int:
2021-12-11 18:35:10 +00:00
"""
Main function to call
"""
2021-12-11 18:22:49 +00:00
parser = argparse.ArgumentParser()
2021-12-11 18:35:10 +00:00
parser.add_argument(
'-n',
dest='length',
default=LENGTH,
type=int,
)
2021-12-11 18:22:49 +00:00
args = parser.parse_args()
2021-12-11 18:22:49 +00:00
sys.stdout.write(generate(args.length))
2021-12-11 18:22:49 +00:00
return 0
sys.exit(main())