Returns a decoded representation of provided Base64 value >>> decodeBase64("MTIz") == b"123" True >>> decodeBase64("MTIz", binary=False) '123' >>> decodeBase64("A-B_CDE") == decodeBase64("A+B/CDE") True >>> decodeBase64(b"MTIzNA") == b"1234" True >>> decodeB
(value, binary=True, encoding=None)
| 197 | return retVal |
| 198 | |
| 199 | def decodeBase64(value, binary=True, encoding=None): |
| 200 | """ |
| 201 | Returns a decoded representation of provided Base64 value |
| 202 | |
| 203 | >>> decodeBase64("MTIz") == b"123" |
| 204 | True |
| 205 | >>> decodeBase64("MTIz", binary=False) |
| 206 | '123' |
| 207 | >>> decodeBase64("A-B_CDE") == decodeBase64("A+B/CDE") |
| 208 | True |
| 209 | >>> decodeBase64(b"MTIzNA") == b"1234" |
| 210 | True |
| 211 | >>> decodeBase64("MTIzNA") == b"1234" |
| 212 | True |
| 213 | >>> decodeBase64("MTIzNA==") == b"1234" |
| 214 | True |
| 215 | """ |
| 216 | |
| 217 | if value is None: |
| 218 | return None |
| 219 | |
| 220 | padding = b'=' if isinstance(value, bytes) else '=' |
| 221 | |
| 222 | # Reference: https://stackoverflow.com/a/49459036 |
| 223 | if not value.endswith(padding): |
| 224 | value += 3 * padding |
| 225 | |
| 226 | # Reference: https://en.wikipedia.org/wiki/Base64#URL_applications |
| 227 | # Reference: https://perldoc.perl.org/MIME/Base64.html |
| 228 | if isinstance(value, bytes): |
| 229 | value = value.replace(b'-', b'+').replace(b'_', b'/') |
| 230 | else: |
| 231 | value = value.replace('-', '+').replace('_', '/') |
| 232 | |
| 233 | retVal = base64.b64decode(value) |
| 234 | |
| 235 | if not binary: |
| 236 | retVal = getText(retVal, encoding) |
| 237 | |
| 238 | return retVal |
| 239 | |
| 240 | def encodeBase64(value, binary=True, encoding=None, padding=True, safe=False): |
| 241 | """ |
no test coverage detected
searching dependent graphs…