Created
June 14, 2012 10:57
-
-
Save andrelaszlo/2929616 to your computer and use it in GitHub Desktop.
Generate a truly random password, based on atmospheric noise (random.org!)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
############################################################################### | |
# | |
# Script for generating a truly random password, using atmospheric noise. The | |
# generator uses the web service at random.org | |
# | |
# Usage: | |
# password.py [password length] [alphabet] | |
# For example: password.py 5 abcdefgh | |
# | |
############################################################################### | |
############################################################################### | |
# Default config goes here | |
############################################################################### | |
# define alphabet | |
alphabet = 'abcdefghijklmnopqrstuvwxyz' | |
alphabet += alphabet.upper() | |
alphabet += '0123456789' | |
alphabet += '@$!#%&()-/\\"\'{}[]._,;:' | |
# and length | |
password_length = 15 | |
############################################################################### | |
# End config | |
############################################################################### | |
import sys | |
if (sys.version_info.major < 3): | |
raise Exception('You must run Python 3 for this script to work') | |
from random import shuffle | |
from http.client import HTTPConnection | |
import re | |
# Try to get command line args | |
if 2 == len(sys.argv): | |
try: | |
password_length = int(sys.argv[1]) | |
except Exception as ex: | |
alphabet = sys.argv[1] | |
elif 3 == len(sys.argv): | |
try: | |
password_length = int(sys.argv[1]) | |
alphabet = sys.argv[2] | |
except Exception as ex: | |
print("Usage: password.py [password length] [alphabet]\nFor example: password.py 5 abcdefgh") | |
sys.exit() | |
alphabet = list(set([c for c in alphabet])) # unique chars in alphabet | |
if len(alphabet) < 2: | |
print("The alphabet must contain at least two unique characters.") | |
sys.exit() | |
c = HTTPConnection('www.random.org') | |
try: | |
c.request('GET', '/integers/?num=%s&min=0&max=%s&col=1&base=10&format=html&rnd=new' % (password_length, len(alphabet)-1)) | |
except Exception as e: | |
print("Connection error:\n\t%s" % (str(e))) | |
sys.exit() | |
data = c.getresponse().read().decode('utf-8') | |
m = re.search(r'<pre class="data">(.*?)</pre>', data, flags=re.S) | |
if m is None: | |
print("Could not read data from random.org") | |
sys.exit() | |
rnd = [int(s) for s in m.group(1).strip().splitlines()] | |
password = [] | |
for i in rnd[:password_length]: | |
shuffle(alphabet) # combine two layers of randomness, if one is corrupted somehow | |
password.append(alphabet[i]) | |
password = ''.join(password) | |
print(password) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Checkout my password generator, it`s PWAs https://randompasswordgenerator.org/