Skip to content

Easy Base64

py_simple.easy_base64

easy_base64 turns text into Base64 and back without the encoding boilerplate.

EasyBase64Error

Bases: Exception

Raised when text cannot be encoded or decoded as Base64.

Parameters:

Name Type Description Default
message str

Description of what went wrong.

required

decode_text(encoded)

Decode a Base64 string back into text.

Parameters:

Name Type Description Default
encoded str

Base64 text, such as the result of encode_text.

required

Returns:

Name Type Description
str str

The original text.

Raises:

Type Description
EasyBase64Error

If encoded is not valid Base64 text.

Example
from py_simple import decode_text

message = decode_text("aGVsbG8=")
import base64

message = base64.b64decode("aGVsbG8=").decode("utf-8")

encode_text(text)

Encode a piece of text as a Base64 string.

Parameters:

Name Type Description Default
text str

The text to encode.

required

Returns:

Name Type Description
str str

The Base64 text, safe to paste into a file or a message.

Raises:

Type Description
EasyBase64Error

If text is not a string.

Example
from py_simple import encode_text

secret = encode_text("hello")
import base64

secret = base64.b64encode("hello".encode("utf-8")).decode("ascii")

is_base64(value)

Check whether a string is valid Base64.

Parameters:

Name Type Description Default
value str

The text to check.

required

Returns:

Name Type Description
bool bool

True when value can be decoded as Base64, otherwise False.

Example
from py_simple import is_base64

is_base64("aGVsbG8=")  # True
import base64

try:
    base64.b64decode("aGVsbG8=", validate=True)
    ok = True
except ValueError:
    ok = False