-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase32.py
executable file
·65 lines (51 loc) · 1.95 KB
/
base32.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
#!/usr/bin/env python3
'''
base32.py
CLI base32 encoder/decoder.
Too slow to be usable on big (>1MB) files, apparently.
Created: 7 feb 2013.
@author: Anton Eliasson <[email protected]>
'''
import base64
def encode(file):
'''
Reads file and returns it base32 encoded as a bytes object.
'''
data = file.read()
return base64.b32encode(data)
def decode(file):
'''
Reads file, strips all whitespace and base32 decodes it. The result is returned
as a bytes object.
'''
data = file.read().decode('ascii')
data = data.strip().replace(' ', '').replace('\n', '').replace('\t', '')
return base64.b32decode(data, casefold=True)
def wrap_text(text, width):
'''
Inserts a newline into the string text at every width character (but not at
the end of the string). The result is returned as a new string.
'''
import textwrap
# break_on_hyphens makes textwrap really slow with unbreakable lines, disable it
return '\n'.join(textwrap.wrap(text, width=width, break_on_hyphens = False))
def main():
import sys, argparse
# make stdin binary
sys.stdin = sys.stdin.detach()
parser = argparse.ArgumentParser(description='Base32 encode or decode FILE, '
'or standard input, to standard output.',
epilog='With no FILE, or when FILE is -, read standard input.')
parser.add_argument('-d', '--decode', action='store_true', help='decode data')
parser.add_argument('FILE', nargs='?', type=argparse.FileType('rb'), default=sys.stdin)
parser.add_argument('-w', '--wrap', default=76, help='wrap encoded lines after COLS character (default 76).')
args = parser.parse_args()
file = args.FILE
textwidth = int(args.wrap)
if args.decode:
print(decode(file).decode('ascii'), end='')
else:
text = encode(file).decode('ascii')
print(wrap_text(text, textwidth))
if __name__ == '__main__':
main()