-
Notifications
You must be signed in to change notification settings - Fork 0
/
b64tool.py
54 lines (46 loc) · 1.44 KB
/
b64tool.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
#!/usr/bin/env python3
##################################################
# CLI tool for base64 en-/decoding of strings
# using argparse is prob overkill, but anyways...
##################################################
import argparse
import base64
def main() -> None:
mode = {
"dec": "decode",
"enc": "encode"
}
# set up parser and populate arguments...
parser = argparse.ArgumentParser(description="Encode or decode base64.")
parser.add_argument(
"mode",
type=str,
choices=mode.keys(),
help="Mode of operation."
)
parser.add_argument(
"--urlsafe",
action="store_true",
dest="safe",
help="Use filename/urlsafe mode."
)
parser.add_argument(
"inputstring",
type=str,
help="Your string to encode/decode."
)
# parse arguments
parsed_args = parser.parse_args()
if parsed_args.mode == "enc":
if parsed_args.safe:
result = base64.urlsafe_b64encode(bytes(parsed_args.inputstring, "utf-8"))
else:
result = base64.b64encode(bytes(parsed_args.inputstring, 'utf-8'))
else:
if parsed_args.safe:
result = base64.urlsafe_b64decode(bytes(parsed_args.inputstring, "utf-8"))
else:
result = base64.b64decode(bytes(parsed_args.inputstring, "utf-8"))
print(f"Here's what I got: {result.decode()}")
if __name__ == '__main__':
main()