Skip to content

Commit ad38c67

Browse files
committed
feat: add base64 encode and decode
1 parent 3ad28fb commit ad38c67

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

Base64-Encode-Decode/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Base64 Encode And Decode
2+
## Usage
3+
``` bash
4+
usage: Base64 [-h] [-d | --decode | --no-decode] text
5+
6+
Base64 encode adn decode string
7+
8+
positional arguments:
9+
text The text to decode or encode
10+
11+
options:
12+
-h, --help show this help message and exit
13+
-d, --decode, --no-decode
14+
Decode text (default: False)
15+
```
16+
17+
## Example
18+
### Encode
19+
```
20+
python3 base64_encode_decode.py "abcxyz 123"
21+
```
22+
Result:
23+
```
24+
YWJjeHl6IDEyMw==
25+
```
26+
27+
### Decode:
28+
```
29+
python3 base64_encode_decode.py -d YWJjeHl6IDEyMw==
30+
```
31+
Result:
32+
```
33+
abcxyz 123
34+
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import base64
2+
from argparse import ArgumentParser, BooleanOptionalAction
3+
4+
5+
def decode(encoded: str) -> str:
6+
return base64.b64decode(encoded).decode()
7+
8+
9+
def encode(text: str) -> str:
10+
return base64.b64encode(text.encode()).decode()
11+
12+
13+
if __name__ == "__main__":
14+
parser = ArgumentParser(
15+
prog="Base64",
16+
description="Base64 encode adn decode string",
17+
)
18+
parser.add_argument("-d", "--decode", action=BooleanOptionalAction, default=False, type=bool, help="Decode text")
19+
parser.add_argument("text", type=str, help="The text to decode or encode")
20+
args = parser.parse_args()
21+
if args.decode:
22+
print(decode(args.text))
23+
else:
24+
print(encode(args.text))

0 commit comments

Comments
 (0)