Skip to content

Instantly share code, notes, and snippets.

@jimbojetset
Last active September 26, 2015 20:45
Show Gist options
  • Save jimbojetset/5ad0bbcc4c7b4e692385 to your computer and use it in GitHub Desktop.
Save jimbojetset/5ad0bbcc4c7b4e692385 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Copyright (c) 2015 James Booth
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Random password generator.
usage: passGen.py -s <chars> -l <length> -c <count> -e <entropy> -o <outputFile>
"""
import sys, getopt, random, time, re
from math import log,pow
CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.:,;<>?"#$%&/()!@{}~=+'
ENTROPY = [
(10, re.compile('\d')),
(26, re.compile('[a-z]')),
(26, re.compile('[A-Z]')),
(24, re.compile('[-_.:,;<>?"#$%&/()!@{}~=+]')),
]
def main(argv):
length = 8
count = 1
entropy = 42
outputFile = ''
try:
opts, args = getopt.getopt(argv,'hs:l:x:e:o:')
except getopt.GetoptError:
printHelp()
for opt, arg in opts:
if opt == '-h':
printHelp()
elif opt in ('-s'):
CHARS = arg
elif opt in ('-l'):
length = validate_int('-l', arg)
elif opt in ('-x'):
count = validate_int('-c',arg)
elif opt in ('-e'):
entropy = validate_int('-e',arg)
elif opt in ('-o'):
outputFile = arg
generate(count,length,entropy,outputFile)
def validate_int(flag, arg):
try:
return int(arg)
except ValueError:
print '%s %r not an integer' % (flag, arg)
sys.exit()
def calc_entropy(password):
total = 0
for weight, reg in ENTROPY:
if reg.search(password):
total += weight
return log(pow(total,len(password)),2)
def generate(count,length,entropy,outputFile):
try:
f = open(outputFile, 'w')
except IOError:
f = sys.stdout
timeout = time.time() + (count/10)
catch = False
while (count > 0):
password = ''.join([random.choice(CHARS) for n in xrange(length)])
if calc_entropy(password) >= entropy:
count -= 1
f.write(password+'\n')
if time.time() > timeout:
catch = True
count -=1
if catch:
print 'One or more timeouts occured.'
print 'Try adjusting the chars, length and/or entropy values'
def printHelp():
print '\nusage: passGen.py -s <chars> -l <length> -c <count> -e <entropy> -o <outputFile>\n'
print 'defaults: (if no argument specified)'
print '[-s] abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.:,;<>?"#$%&/()!@{}~'
print '[-l] 8'
print '[-c] 1'
print '[-e] 42'
print '[-o] {output to console}\n'
sys.exit()
if __name__ =='__main__':main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment