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:
|
2021-12-11 18:35:10 +00:00
|
|
|
"""
|
|
|
|
Simple function to call OpenSSL
|
2017-04-14 16:13:26 +01:00
|
|
|
|
|
|
|
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:
|
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()
|
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())
|