dotfiles/bin/genpw
Scott Wallace 717d0c1fa1 Convert genpw from Bash to Python.
Add a method to install Python modules via Pip to `software-install`.
2017-04-14 16:13:26 +01:00

46 lines
819 B
Python
Executable file

#!/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)