dotfiles/bin/genpw

46 lines
819 B
Plaintext
Raw Normal View History

#!/usr/bin/env python
"""Module to generate a random string
Attributes:
LENGTH (int): Defaults to a length of 16
"""
import sys
import logging
import optparse
try:
import OpenSSL
except ImportError:
logging.error('Missing PyOpenSSL.')
logging.debug('Try `pip install pyopenssl`')
sys.exit(1)
import base64
LENGTH = 16
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)