mirror of
https://github.com/hastagAB/Awesome-Python-Scripts.git
synced 2025-01-30 13:13:43 +00:00
feat: add base64 encode and decode
This commit is contained in:
parent
3ad28fbf2f
commit
ad38c67b6a
34
Base64-Encode-Decode/README.md
Normal file
34
Base64-Encode-Decode/README.md
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
# Base64 Encode And Decode
|
||||||
|
## Usage
|
||||||
|
``` bash
|
||||||
|
usage: Base64 [-h] [-d | --decode | --no-decode] text
|
||||||
|
|
||||||
|
Base64 encode adn decode string
|
||||||
|
|
||||||
|
positional arguments:
|
||||||
|
text The text to decode or encode
|
||||||
|
|
||||||
|
options:
|
||||||
|
-h, --help show this help message and exit
|
||||||
|
-d, --decode, --no-decode
|
||||||
|
Decode text (default: False)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Example
|
||||||
|
### Encode
|
||||||
|
```
|
||||||
|
python3 base64_encode_decode.py "abcxyz 123"
|
||||||
|
```
|
||||||
|
Result:
|
||||||
|
```
|
||||||
|
YWJjeHl6IDEyMw==
|
||||||
|
```
|
||||||
|
|
||||||
|
### Decode:
|
||||||
|
```
|
||||||
|
python3 base64_encode_decode.py -d YWJjeHl6IDEyMw==
|
||||||
|
```
|
||||||
|
Result:
|
||||||
|
```
|
||||||
|
abcxyz 123
|
||||||
|
```
|
24
Base64-Encode-Decode/base64_encode_decode.py
Normal file
24
Base64-Encode-Decode/base64_encode_decode.py
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
import base64
|
||||||
|
from argparse import ArgumentParser, BooleanOptionalAction
|
||||||
|
|
||||||
|
|
||||||
|
def decode(encoded: str) -> str:
|
||||||
|
return base64.b64decode(encoded).decode()
|
||||||
|
|
||||||
|
|
||||||
|
def encode(text: str) -> str:
|
||||||
|
return base64.b64encode(text.encode()).decode()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
parser = ArgumentParser(
|
||||||
|
prog="Base64",
|
||||||
|
description="Base64 encode adn decode string",
|
||||||
|
)
|
||||||
|
parser.add_argument("-d", "--decode", action=BooleanOptionalAction, default=False, type=bool, help="Decode text")
|
||||||
|
parser.add_argument("text", type=str, help="The text to decode or encode")
|
||||||
|
args = parser.parse_args()
|
||||||
|
if args.decode:
|
||||||
|
print(decode(args.text))
|
||||||
|
else:
|
||||||
|
print(encode(args.text))
|
Loading…
Reference in New Issue
Block a user