Returns a Base64 encoded representation of the provided value >>> encodeBase64(b"123") == b"MTIz" True >>> encodeBase64(u"1234", binary=False) 'MTIzNA==' >>> encodeBase64(u"1234", binary=False, padding=False) 'MTIzNA' >>> encodeBase64(decodeBase64("A-B_CDE"), binary
(value, binary=True, encoding=None, padding=True, safe=False)
| 238 | return retVal |
| 239 | |
| 240 | def encodeBase64(value, binary=True, encoding=None, padding=True, safe=False): |
| 241 | """ |
| 242 | Returns a Base64 encoded representation of the provided value |
| 243 | |
| 244 | >>> encodeBase64(b"123") == b"MTIz" |
| 245 | True |
| 246 | >>> encodeBase64(u"1234", binary=False) |
| 247 | 'MTIzNA==' |
| 248 | >>> encodeBase64(u"1234", binary=False, padding=False) |
| 249 | 'MTIzNA' |
| 250 | >>> encodeBase64(decodeBase64("A-B_CDE"), binary=False, safe=True) |
| 251 | 'A-B_CDE' |
| 252 | """ |
| 253 | |
| 254 | if value is None: |
| 255 | return None |
| 256 | |
| 257 | if isinstance(value, six.text_type): |
| 258 | value = value.encode(encoding or UNICODE_ENCODING) |
| 259 | |
| 260 | retVal = base64.b64encode(value) |
| 261 | |
| 262 | if not binary: |
| 263 | retVal = getText(retVal, encoding) |
| 264 | |
| 265 | if safe: |
| 266 | padding = False |
| 267 | |
| 268 | # Reference: https://en.wikipedia.org/wiki/Base64#URL_applications |
| 269 | # Reference: https://perldoc.perl.org/MIME/Base64.html |
| 270 | if isinstance(retVal, bytes): |
| 271 | retVal = retVal.replace(b'+', b'-').replace(b'/', b'_') |
| 272 | else: |
| 273 | retVal = retVal.replace('+', '-').replace('/', '_') |
| 274 | |
| 275 | if not padding: |
| 276 | retVal = retVal.rstrip(b'=' if isinstance(retVal, bytes) else '=') |
| 277 | |
| 278 | return retVal |
| 279 | |
| 280 | def getBytes(value, encoding=None, errors="strict", unsafe=True): |
| 281 | """ |
searching dependent graphs…