2017-04-14 16:13:26 +01:00
|
|
|
#!/usr/bin/env python
|
2021-12-11 18:22:49 +00:00
|
|
|
"""
|
|
|
|
Module to generate a random string
|
2015-03-26 18:25:58 +00:00
|
|
|
|
2017-04-14 16:13:26 +01:00
|
|
|
Attributes:
|
|
|
|
LENGTH (int): Defaults to a length of 16
|
|
|
|
"""
|
2015-03-26 18:25:58 +00:00
|
|
|
|
2021-12-11 18:22:49 +00:00
|
|
|
import argparse
|
2017-04-14 16:13:26 +01:00
|
|
|
import base64
|
2021-12-11 18:22:49 +00:00
|
|
|
import os
|
|
|
|
import sys
|
2015-03-26 18:25:58 +00:00
|
|
|
|
2017-04-14 16:13:26 +01:00
|
|
|
LENGTH = 16
|
2015-03-26 18:25:58 +00:00
|
|
|
|
|
|
|
|
2021-12-11 18:22:49 +00:00
|
|
|
def generate(length: int) -> str:
|
2017-04-14 16:13:26 +01: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]
|
2017-04-14 16:13:26 +01:00
|
|
|
|
|
|
|
|
2021-12-11 18:22:49 +00:00
|
|
|
if __name__ == '__main__':
|
2017-04-14 16:13:26 +01:00
|
|
|
|
2021-12-11 18:22:49 +00:00
|
|
|
def main() -> int:
|
|
|
|
"""Main function to call"""
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('-n', dest='length', default=LENGTH, type=int)
|
|
|
|
args = parser.parse_args()
|
2017-04-14 16:13:26 +01:00
|
|
|
|
2021-12-11 18:22:49 +00:00
|
|
|
sys.stdout.write(generate(args.length))
|
2017-04-14 16:13:26 +01:00
|
|
|
|
2021-12-11 18:22:49 +00:00
|
|
|
return 0
|
|
|
|
|
|
|
|
sys.exit(main())
|