-
Notifications
You must be signed in to change notification settings - Fork 0
/
passwordgen
executable file
·111 lines (105 loc) · 4.21 KB
/
passwordgen
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python3
# passwordgen: Simple CLI password generator
#
# Copyright (C) 2021-2022 mini_bomba
#
# 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 <https://www.gnu.org/licenses/>.
import base64
import secrets
import string
import sys
import os
import shutil
import subprocess
import math
mode_env = os.environ['MBPASS_MODE'].lower() if 'MBPASS_MODE' in os.environ else None
mode_map = { # (clipboard, printpass)
None: (False, True),
'print': (False, True),
'terminal': (False, True),
'copy': (True, False),
'clipboard': (True, False),
'both': (True, True),
}
if mode_env not in mode_map:
print('Invalid "MBPASS_MODE" environment variable value.', file=sys.stderr)
print('See https://github.com/mini-bomba/mini_bomba-utils/blob/main/docs/passwordgen.md#using-environment-variables-to-alter-programs-behaviour for details.', file=sys.stderr)
exit(1)
else:
clipboard, printpass = mode_map[mode_env]
clipboard_env = os.environ['MBPASS_CLIPBOARD'].lower() if 'MBPASS_CLIPBOARD' in os.environ else None
clipboard_map = {
'tk': 'tk',
'tkinter': 'tk',
'kde': 'kde',
'klipper': 'kde',
'termux': 'termux',
'android': 'termux',
}
if not clipboard:
pass
elif clipboard_env is None:
if os.environ.get('XDG_SESSION_DESKTOP') == 'KDE':
clipboard_driver = 'kde'
elif shutil.which('termux-clipboard-set') is not None:
clipboard_driver = 'termux'
else:
clipboard_driver='tk'
elif clipboard_env not in clipboard_map:
print('Invalid "MBPASS_CLIPBOARD" environment variable value.', file=sys.stderr)
print('See https://github.com/mini-bomba/mini_bomba-utils/blob/main/docs/passwordgen.md#using-environment-variables-to-alter-programs-behaviour for details.', file=sys.stderr)
exit(1)
else:
clipboard_driver = clipboard_map[clipboard_env]
length = int(sys.argv[1]) if len(sys.argv) > 1 else 64
type_env = os.environ['MBPASS_TYPE'].lower() if 'MBPASS_TYPE' in os.environ else None
if type_env is None or type_env == 'urlsafe':
passw = secrets.token_urlsafe(length)[:length]
elif type_env == 'alphanum':
chars = string.ascii_letters + string.digits
passw = "".join(secrets.choice(chars) for _ in range(length))
elif type_env == 'ascii':
chars = string.ascii_letters + string.digits + string.punctuation
passw = "".join(secrets.choice(chars) for _ in range(length))
elif type_env in ('base64', 'b64'):
passw = base64.b64encode(secrets.token_bytes(length)).decode()[:length]
elif type_env == 'hex':
passw = secrets.token_hex(math.ceil(length/2))[:length]
else:
print('Invalid "MBPASS_TYPE" environment variable value.', file=sys.stderr)
print('See https://github.com/mini-bomba/mini_bomba-utils/blob/main/docs/passwordgen.md#using-environment-variables-to-alter-programs-behaviour for details.', file=sys.stderr)
exit(1)
if printpass:
print(passw)
if clipboard:
if clipboard_driver == 'tk':
# Clipboard code based on https://stackoverflow.com/a/4203897
import tkinter
tk = tkinter.Tk()
tk.withdraw()
tk.clipboard_clear()
tk.clipboard_append(passw)
tk.update()
tk.destroy()
elif clipboard_driver == 'kde':
# Code based on https://gist.github.com/aeris/3308963
import dbus
bus = dbus.SessionBus()
klipper = dbus.Interface(bus.get_object('org.kde.klipper', '/klipper'),
dbus_interface='org.kde.klipper.klipper')
klipper.setClipboardContents(passw)
elif clipboard_driver == 'termux':
subprocess.run(['termux-clipboard-set'], input=passw, text=True)
else:
assert False, 'Invalid clipboard_driver value'