2017-04-14 16:13:26 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""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
|
|
|
|
2017-04-14 16:13:26 +01:00
|
|
|
import sys
|
|
|
|
import logging
|
|
|
|
import optparse
|
2015-03-26 18:25:58 +00:00
|
|
|
|
2017-04-14 16:13:26 +01:00
|
|
|
try:
|
|
|
|
import OpenSSL
|
|
|
|
except ImportError:
|
|
|
|
logging.error('Missing PyOpenSSL.')
|
|
|
|
logging.debug('Try `pip install pyopenssl`')
|
|
|
|
sys.exit(1)
|
2015-03-26 18:25:58 +00:00
|
|
|
|
2017-04-14 16:13:26 +01:00
|
|
|
import base64
|
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
|
|
|
|
|
|
|
|
2017-04-14 16:13:26 +01:00
|
|
|
def generate(length):
|
|
|
|
"""Simple function to call OpenSSL
|
|
|
|
|
|
|
|
Args:
|
|
|
|
length (int): length of string to return
|
|
|
|
"""
|
|
|
|
return base64.encodestring(OpenSSL.rand.bytes(256))[:length]
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
"""Main function to call
|
|
|
|
"""
|
|
|
|
parser = optparse.OptionParser()
|
|
|
|
parser.add_option('-n', dest='length', default=LENGTH, type=int)
|
|
|
|
opts, _ = parser.parse_args()
|
|
|
|
|
|
|
|
print generate(opts.length)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
|
|
|
sys.exit(0)
|