[PYTHON] I'll make a password!

gen-pass.py


# Python3 fileencoding: utf-8

from argparse import ArgumentParser
from os import urandom
from string import printable, ascii_letters, digits


def option_parse():
    parser = ArgumentParser()
    parser.add_argument("length", type=int, nargs="?", default=12)
    parser.add_argument(
        "-c", "--charset", choices="na",
        help="a = alphabets, n = a + nums, default is printables."
    )
    return parser.parse_args()


def generate(charset, passlen):
    chars = printable[:-5]
    if charset == "a":
        chars = ascii_letters
    elif charset == "n":
        chars = digits + ascii_letters
    password = ""
    while len(password) < passlen:
        s = urandom(1)[0]
        # if Python2.x
        # s = ord(urandom(1)[0])
        if s < len(chars):
            password += chars[s]
    return password


if __name__ == "__main__":
    options = option_parse()
    charset = options.charset
    length = options.length
    print(generate(charset, length))

You can get help with -h. Specify the length with -l and specify the set of characters to be used with -c.

$ python3 gen-pass.py -h
usage: gen-pass.py [-h] [-c {n,a}] [length]

positional arguments:
  length

optional arguments:
  -h, --help            show this help message and exit
  -c {n,a}, --charset {n,a}
                        a = alphabets, n = a + nums, default is printables.
$ python3 gen-pass.py
B1h(=,qW;ClW
$ python3 gen-pass.py 24 -c n
wup4sGyAb1GHp7ajE9PL5Vk0
$

Recommended Posts

I'll make a password!
Make a squash game
Make a function decorator
Make a distance matrix
Make a Nyan button
Make a Tetris-style game!
Make a Base64 decoder
Let's make a Discord Bot.
Make a Blueqat backend ~ Part 2
[Django] Make a pull-down menu
Make a LINE BOT (chat)
Make a bookmarklet in Python
Make a fortune with Python
Make Responder a daemon (service)
Let's make a rock-paper-scissors game
Make a fire with kdeplot
Make a math drill print
Let's make a remote rumba [Hardware]
How to make a Japanese-English translation
Make a Santa classifier from a Santa image
Let's make a remote rumba [Software]
Make a Tweet box for Pepper
Let's make a GUI with python.
Make a sound with Jupyter notebook
Let's make a spot sale service 2
Make a face recognizer using TensorFlow
How to make a slack bot
Let's make a breakout with wxPython
How to make a recursive function
python / Make a dict from a list.
Make a recommender system with python
[Blender] How to make a Blender plugin
[Python] Generate a password with Slackbot
Make Flask a Cloud Native application
Make a filter with a django template
Let's make a graph with python! !!
Let's make a supercomputer with xCAT
How to make a crawler --Basic
Make a model iterator with PySide
Make a nice graph with plotly
Make a curtain generator in Blender
Let's make a spot sale service 3
Let's make a shiritori game with Python
Make a video player with PySimpleGUI + OpenCV
[Python] How to make a class iterable
Try to make a kernel of Jupyter
Make a relation diagram of Python module
Make a rare gacha simulator with Flask
Make Jupyter Notebook a service on CentOS
Make Unity Accelerator a service on Linux
Make a partially zoomed figure with matplotlib
Make a drawing quiz with kivy + PyTorch
Let's make a voice slowly with Python
Make a cascade classifier with google colaboratory
Let's make a simple language with PLY 1
Make Qt for Python app a desktop app
Do you make something like a rocket?
Make a logic circuit with a perceptron (multilayer perceptron)
Make a Yes No Popup with Kivy
Let's make a multilingual site using flask-babel
Make a wash-drying timer with a Raspberry Pi