-
Notifications
You must be signed in to change notification settings - Fork 1
/
altpass.py
executable file
·77 lines (65 loc) · 2.53 KB
/
altpass.py
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
#!/usr/bin/env python
"""Generate passwords that are fun to type"""
from __future__ import print_function
from os import urandom
def choice(keys):
"""Uniformly choose element from list keys, note len(keys) must be
less than 256"""
while(True):
i = ord(urandom(1))
if i < 256 - (256 % len(keys)):
return keys[i % len(keys)]
# QWERTY layout:
left_keys = [["`", "1", "2", "3", "4", "5", "6", ],
["q", "w", "e", "r", "t", ],
["a", "s", "d", "f", "g", ],
["z", "x", "c", "v", ], ]
left_shift = [["~", "!", "@", "#", "$", "%", "^", ],
["Q", "W", "E", "R", "T", ],
["A", "S", "D", "F", "G", ],
["Z", "X", "C", "V", ], ]
right_keys = [["7", "8", "9", "0", "-", "="],
["y", "u", "i", "o", "p", "[", "]", "\\", ],
["h", "j", "k", "l", ";", "'", ],
["b", "n", "m", ",", ".", "/", ], ]
right_shift = [["&", "*", "(", ")", "_", "+", ],
["Y", "U", "I", "O", "P", "{", "}", "|", ],
["H", "J", "K", "L", ":", "\"", ],
["B", "N", "M", "<", ">", "?", ], ]
def generate_password(length):
"""Generate a password"""
# Alternating hands is fun
password = ""
# choose randomly which hand to lead
hand = choice(["left", "right"])
# choose randomly whether to start with shift
# note that the state of `shift` is a Markov chain with stationary
# distribution (0.5, 0.5), so draw from the stationary distribution so
# that the probability of shift being True or False at any given point
# in the algorithm is 0.5
shift = choice([True, False])
for i in range(length):
if hand == "left":
# Choose whether shift key
if shift:
password += choice([k for l in right_shift for k in l])
shift = False
hand = "left"
else:
password += choice([k for l in left_keys for k in l])
shift = choice([True, False])
hand = "right"
elif hand == "right":
# Choose whether shift key
if shift:
password += choice([k for l in left_shift for k in l])
shift = False
hand = "right"
else:
password += choice([k for l in right_keys for k in l])
shift = choice([True, False])
hand = "left"
return password
if __name__ == '__main__':
password = generate_password(14)
print(password)