3Provides some helper methods for base 64 encoding.
11 """Decode a Base64 encoded string.
13 :param text: Base64 encoded string to decode
14 :return: Decoded string
16 missing_padding = len(text) % 4
18 text +=
"=" * (4 - missing_padding)
20 decoded = base64.urlsafe_b64decode(text)
21 return decoded.decode(
"utf-8")
24def encode_base_64(text: str, add_padding: bool =
False) -> str:
25 """Encode a string to Base64.
27 :param text: String to encode
28 :param add_padding: Whether to add padding to the encoded string
29 :return: Base64 encoded string
31 encoded_bytes = base64.urlsafe_b64encode(text.encode(
"utf-8"))
34 encoded_bytes = encoded_bytes.rstrip(b
"=")
36 return encoded_bytes.decode(
"utf-8")
40 """Check if a string is Base64 encoded.
42 :param value: String to check
43 :return: True if the string is Base64 encoded, False otherwise
47 except (binascii.Error, ValueError):
50 return encode_base_64(decoded) == value
str decode_base_64(str text)
Decode a Base64 encoded string.
bool is_base_64_encoded(str value)
Check if a string is Base64 encoded.