/ adafruit_azureiot / base64.py
base64.py
1 # The MIT License (MIT) 2 # 3 # Copyright (c) 2020 Jim Bennett 4 # 5 # Permission is hereby granted, free of charge, to any person obtaining a copy 6 # of this software and associated documentation files (the "Software"), to deal 7 # in the Software without restriction, including without limitation the rights 8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 # copies of the Software, and to permit persons to whom the Software is 10 # furnished to do so, subject to the following conditions: 11 # 12 # The above copyright notice and this permission notice shall be included in 13 # all copies or substantial portions of the Software. 14 # 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 # THE SOFTWARE. 22 """ 23 `base64` 24 ================================================================================ 25 26 RFC 3548: Base64 Data Encodings 27 28 29 * Author(s): Jim Bennett 30 31 Implementation Notes 32 -------------------- 33 34 **Software and Dependencies:** 35 36 * Adafruit CircuitPython firmware for the supported boards: 37 https://github.com/adafruit/circuitpython/releases 38 39 """ 40 41 import adafruit_binascii as binascii 42 43 __all__ = ["b64encode", "b64decode"] 44 45 46 def _bytes_from_decode_data(data: str): 47 try: 48 return data.encode("ascii") 49 except: 50 raise ValueError("string argument should contain only ASCII characters") 51 52 53 def b64encode(toencode: bytes) -> bytes: 54 """Encode a byte string using Base64. 55 56 toencode is the byte string to encode. Optional altchars must be a byte 57 string of length 2 which specifies an alternative alphabet for the 58 '+' and '/' characters. This allows an application to 59 e.g. generate url or filesystem safe Base64 strings. 60 61 The encoded byte string is returned. 62 """ 63 # Strip off the trailing newline 64 return binascii.b2a_base64(toencode)[:-1] 65 66 67 def b64decode(todecode: str) -> bytes: 68 """Decode a Base64 encoded byte string. 69 70 todecode is the byte string to decode. Optional altchars must be a 71 string of length 2 which specifies the alternative alphabet used 72 instead of the '+' and '/' characters. 73 74 The decoded string is returned. A binascii.Error is raised if todecode is 75 incorrectly padded. 76 77 If validate is False (the default), non-base64-alphabet characters are 78 discarded prior to the padding check. If validate is True, 79 non-base64-alphabet characters in the input result in a binascii.Error. 80 """ 81 return binascii.a2b_base64(_bytes_from_decode_data(todecode))