File: __main__.py

package info (click to toggle)
python-zxcvbn 4.4.28-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 55,856 kB
  • sloc: python: 2,179; makefile: 3
file content (44 lines) | stat: -rw-r--r-- 1,132 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import argparse
import json
import select
import sys
import getpass

from . import zxcvbn

parser = argparse.ArgumentParser(
    description="Python implementation of Dropbox's realistic password "
                'strength estimator'
)
parser.add_argument(
    '--user-input',
    action='append',
    help='user data to be added to the dictionaries that are tested against '
         '(name, birthdate, etc)',
)

class JSONEncoder(json.JSONEncoder):
    def default(self, o):
        try:
            return super(JSONEncoder, self).default(o)
        except TypeError:
            return str(o)

def cli():
    args = parser.parse_args()

    # check if stdin is ready for reading
    rlist, _, _ = select.select([sys.stdin], [], [], 0.0)
    if rlist:
        password = rlist[0].read()
        if password[-1] == '\n':  # strip off the trailing newline
            password = password[:-1]
    else:
        password = getpass.getpass()

    res = zxcvbn(password, user_inputs=args.user_input)
    json.dump(res, sys.stdout, indent=2, cls=JSONEncoder)
    sys.stdout.write('\n')

if __name__ == '__main__':
    sys.exit(cli())