Skip to content

Instantly share code, notes, and snippets.

@davidhcefx
Last active April 20, 2022 22:07
Show Gist options
  • Save davidhcefx/ae5eb67ed78e6980c1051684f8d5a48a to your computer and use it in GitHub Desktop.
Save davidhcefx/ae5eb67ed78e6980c1051684f8d5a48a to your computer and use it in GitHub Desktop.
[REAL] Offline Random Password Generator in Python3

Offline Random Password Generator

There are plenty of ONLINE websites that claimed to be generating your password OFFLINE. This sounds quite absurd to me, as there are no way to tell if they are collecting your data or not, unless you look into their Javascript code or something. At the end of the day, generating one's password using someone else's website just doesn't make sense. Therefore, this code is meant to be run locally and is easy to understand, which is essential to provide confidence that it is safe.

Usage

python3 random_password_generator.py

Examples

  • Y?otqi3h8$DA9M-&=TWb
  • ixjF~_!$Vu*qE&49io#*
  • #B*dzjcDv$ezzD!mcR?U
  • bvV9NseA9naxA7YodQBF
  • kJDCoAU39Ueuebf5WMQw
  • xeWeq8o4sHd63qjTogx9

Code

from os import urandom
from random import shuffle

# Written by davidhcefx, 2020.3.16.

print('===============================================================================')
print('||                     Offline Random Password Generator                     ||')
print('===============================================================================')
print('')
ln = input('> Password Length: (strong: >= 16) ').strip()
length = 16 if ln == '' else int(ln)
print('length = %d\n' % length)

print('Choose Character Set: [default = 12345]')
print('[1] Lowercase + Uppercase')
print('[2] Digits')
print('[3] Symbols')
print('[4] EXCLUDE Similar Characters: I l 1 O 0')
print('[5] EXCLUDE Ambiguous Characters: {}[]()<>/\\\'\"`;:,.')
choice = input('> ').strip()
if choice == '':
    choice = '12345'

charset = []
exclude_alnum = ''
exclude_symbol = ''

# Exclude similar
if '4' in choice:
    exclude_alnum = 'Il1O0'

# Exclude ambiguous
if '5' in choice:
    exclude_symbol = '{}[]()<>/\\\'\"`;:,.'

# Lower + Upper
if '1' in choice:
    alpha = [chr(i) for i in range(ord('a'), ord('z') + 1)] \
        + [chr(i) for i in range(ord('A'), ord('Z') + 1)]
    for a in alpha:
        if a not in exclude_alnum:
            charset.append(a)

# Digits
if '2' in choice:
    for i in range(ord('0'), ord('9') + 1):
        if chr(i) not in exclude_alnum:
            charset.append(chr(i))

# Symbols
if '3' in choice:
    all_symbols = [chr(i) for i in range(0x21, 0x2f + 1)] \
        + [chr(i) for i in range(0x3a, 0x40 + 1)] \
        + [chr(i) for i in range(0x5b, 0x60 + 1)] \
        + [chr(i) for i in range(0x7b, 0x7e + 1)]
    for s in all_symbols:
        if s not in exclude_symbol:
            charset.append(s)

print('charset:', ''.join(charset))
shuffle(charset)  # mitigate the probability bias in [0,255] % len(charset)

passwd = []
for i in urandom(length):
    passwd.append(charset[i % len(charset)])

print('\n> Here is your generated password: ', ''.join(passwd))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment