-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgenotp.py
60 lines (45 loc) · 1.18 KB
/
genotp.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
#!/usr/bin/env python
# file: genotp.py
# vim:fileencoding=utf-8:ft=python
#
# Copyright © 2014-2018 R.F. Smith <[email protected]>.
# SPDX-License-Identifier: MIT
# Created: 2014-03-08T14:04:00+01:00
# Last modified: 2018-08-02T00:51:46+0200
"""
Generate an old-fashioned one-time pad.
The format of the one-time pad is:
65 lines of 12 groups of 5 random capital letters.
"""
from secrets import choice
_CAPS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
def main():
"""Entry point."""
r = rndcaps(10)
print(f"+++++ {r} +++++")
print(otp())
def otp(n=65):
"""
Return a one-time pad.
Arguments:
n: number of lines in the key
Returns:
n lines of 12 groups of 5 random capital letters.
"""
lines = []
for num in range(1, n + 1):
i = [f"{num:02d} "]
i += [rndcaps(5) for j in range(0, 12)]
lines.append(" ".join(i))
return "\n".join(lines)
def rndcaps(n):
"""
Generates a string of random capital letters.
Arguments:
n: Length of the output string.
Returns:
A string of n random capital letters.
"""
return "".join([choice(_CAPS) for c in range(n)])
if __name__ == "__main__":
main()