Guido van Rossum | aa925a5 | 1997-04-02 05:47:39 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 3 | """RFC 3548: Base16, Base32, Base64 Data Encodings""" |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 4 | |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 5 | # Modified 04-Oct-1995 by Jack Jansen to use binascii module |
| 6 | # Modified 30-Dec-2003 by Barry Warsaw to add full RFC 3548 support |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 7 | # Modified 22-May-2007 by Guido van Rossum to use bytes everywhere |
Jack Jansen | 951213e | 1995-10-04 16:39:20 +0000 | [diff] [blame] | 8 | |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 9 | import re |
| 10 | import struct |
Jack Jansen | 951213e | 1995-10-04 16:39:20 +0000 | [diff] [blame] | 11 | import binascii |
| 12 | |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 13 | |
| 14 | __all__ = [ |
| 15 | # Legacy interface exports traditional RFC 1521 Base64 encodings |
Georg Brandl | b54d801 | 2009-06-04 09:11:51 +0000 | [diff] [blame] | 16 | 'encode', 'decode', 'encodebytes', 'decodebytes', |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 17 | # Generalized interface for other encodings |
| 18 | 'b64encode', 'b64decode', 'b32encode', 'b32decode', |
| 19 | 'b16encode', 'b16decode', |
| 20 | # Standard Base64 encoding |
| 21 | 'standard_b64encode', 'standard_b64decode', |
| 22 | # Some common Base64 alternatives. As referenced by RFC 3458, see thread |
| 23 | # starting at: |
| 24 | # |
| 25 | # http://zgp.org/pipermail/p2p-hackers/2001-September/000316.html |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 26 | 'urlsafe_b64encode', 'urlsafe_b64decode', |
| 27 | ] |
| 28 | |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 30 | bytes_types = (bytes, bytearray) # Types acceptable as binary data |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 31 | |
| 32 | |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 33 | def _translate(s, altchars): |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 34 | if not isinstance(s, bytes_types): |
Guido van Rossum | 98b349f | 2007-08-27 21:47:52 +0000 | [diff] [blame] | 35 | raise TypeError("expected bytes, not %s" % s.__class__.__name__) |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 36 | translation = bytearray(range(256)) |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 37 | for k, v in altchars.items(): |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 38 | translation[ord(k)] = v[0] |
| 39 | return s.translate(translation) |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 40 | |
| 41 | |
Antoine Pitrou | fd03645 | 2008-08-19 17:56:33 +0000 | [diff] [blame] | 42 | |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 43 | # Base64 encoding/decoding uses binascii |
| 44 | |
| 45 | def b64encode(s, altchars=None): |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 46 | """Encode a byte string using Base64. |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 48 | s is the byte string to encode. Optional altchars must be a byte |
| 49 | string of length 2 which specifies an alternative alphabet for the |
| 50 | '+' and '/' characters. This allows an application to |
| 51 | e.g. generate url or filesystem safe Base64 strings. |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 53 | The encoded byte string is returned. |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 54 | """ |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 55 | if not isinstance(s, bytes_types): |
Alexandre Vassalotti | 5209857f | 2008-05-03 04:39:38 +0000 | [diff] [blame] | 56 | raise TypeError("expected bytes, not %s" % s.__class__.__name__) |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 57 | # Strip off the trailing newline |
| 58 | encoded = binascii.b2a_base64(s)[:-1] |
| 59 | if altchars is not None: |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 60 | if not isinstance(altchars, bytes_types): |
Georg Brandl | 7d1e880 | 2009-08-13 08:47:18 +0000 | [diff] [blame] | 61 | raise TypeError("expected bytes, not %s" |
| 62 | % altchars.__class__.__name__) |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 63 | assert len(altchars) == 2, repr(altchars) |
| 64 | return _translate(encoded, {'+': altchars[0:1], '/': altchars[1:2]}) |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 65 | return encoded |
| 66 | |
| 67 | |
| 68 | def b64decode(s, altchars=None): |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 69 | """Decode a Base64 encoded byte string. |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 70 | |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 71 | s is the byte string to decode. Optional altchars must be a |
| 72 | string of length 2 which specifies the alternative alphabet used |
| 73 | instead of the '+' and '/' characters. |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 75 | The decoded byte string is returned. binascii.Error is raised if |
| 76 | s were incorrectly padded or if there are non-alphabet characters |
| 77 | present in the string. |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 78 | """ |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 79 | if not isinstance(s, bytes_types): |
Alexandre Vassalotti | 5209857f | 2008-05-03 04:39:38 +0000 | [diff] [blame] | 80 | raise TypeError("expected bytes, not %s" % s.__class__.__name__) |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 81 | if altchars is not None: |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 82 | if not isinstance(altchars, bytes_types): |
Alexandre Vassalotti | 5209857f | 2008-05-03 04:39:38 +0000 | [diff] [blame] | 83 | raise TypeError("expected bytes, not %s" |
| 84 | % altchars.__class__.__name__) |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 85 | assert len(altchars) == 2, repr(altchars) |
| 86 | s = _translate(s, {chr(altchars[0]): b'+', chr(altchars[1]): b'/'}) |
| 87 | return binascii.a2b_base64(s) |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 88 | |
| 89 | |
| 90 | def standard_b64encode(s): |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 91 | """Encode a byte string using the standard Base64 alphabet. |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 92 | |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 93 | s is the byte string to encode. The encoded byte string is returned. |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 94 | """ |
| 95 | return b64encode(s) |
| 96 | |
| 97 | def standard_b64decode(s): |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 98 | """Decode a byte string encoded with the standard Base64 alphabet. |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 99 | |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 100 | s is the byte string to decode. The decoded byte string is |
| 101 | returned. binascii.Error is raised if the input is incorrectly |
| 102 | padded or if there are non-alphabet characters present in the |
| 103 | input. |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 104 | """ |
| 105 | return b64decode(s) |
| 106 | |
| 107 | def urlsafe_b64encode(s): |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 108 | """Encode a byte string using a url-safe Base64 alphabet. |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 109 | |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 110 | s is the byte string to encode. The encoded byte string is |
| 111 | returned. The alphabet uses '-' instead of '+' and '_' instead of |
| 112 | '/'. |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 113 | """ |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 114 | return b64encode(s, b'-_') |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 115 | |
| 116 | def urlsafe_b64decode(s): |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 117 | """Decode a byte string encoded with the standard Base64 alphabet. |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 118 | |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 119 | s is the byte string to decode. The decoded byte string is |
| 120 | returned. binascii.Error is raised if the input is incorrectly |
| 121 | padded or if there are non-alphabet characters present in the |
| 122 | input. |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 123 | |
| 124 | The alphabet uses '-' instead of '+' and '_' instead of '/'. |
| 125 | """ |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 126 | return b64decode(s, b'-_') |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 127 | |
| 128 | |
Antoine Pitrou | fd03645 | 2008-08-19 17:56:33 +0000 | [diff] [blame] | 129 | |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 130 | # Base32 encoding/decoding must be done in Python |
| 131 | _b32alphabet = { |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 132 | 0: b'A', 9: b'J', 18: b'S', 27: b'3', |
| 133 | 1: b'B', 10: b'K', 19: b'T', 28: b'4', |
| 134 | 2: b'C', 11: b'L', 20: b'U', 29: b'5', |
| 135 | 3: b'D', 12: b'M', 21: b'V', 30: b'6', |
| 136 | 4: b'E', 13: b'N', 22: b'W', 31: b'7', |
| 137 | 5: b'F', 14: b'O', 23: b'X', |
| 138 | 6: b'G', 15: b'P', 24: b'Y', |
| 139 | 7: b'H', 16: b'Q', 25: b'Z', |
| 140 | 8: b'I', 17: b'R', 26: b'2', |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 143 | _b32tab = [v[0] for k, v in sorted(_b32alphabet.items())] |
| 144 | _b32rev = dict([(v[0], k) for k, v in _b32alphabet.items()]) |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 145 | |
| 146 | |
| 147 | def b32encode(s): |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 148 | """Encode a byte string using Base32. |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 149 | |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 150 | s is the byte string to encode. The encoded byte string is returned. |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 151 | """ |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 152 | if not isinstance(s, bytes_types): |
Alexandre Vassalotti | 5209857f | 2008-05-03 04:39:38 +0000 | [diff] [blame] | 153 | raise TypeError("expected bytes, not %s" % s.__class__.__name__) |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 154 | quanta, leftover = divmod(len(s), 5) |
| 155 | # Pad the last quantum with zero bits if necessary |
| 156 | if leftover: |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 157 | s = s + bytes(5 - leftover) # Don't use += ! |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 158 | quanta += 1 |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 159 | encoded = bytes() |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 160 | for i in range(quanta): |
| 161 | # c1 and c2 are 16 bits wide, c3 is 8 bits wide. The intent of this |
| 162 | # code is to process the 40 bits in units of 5 bits. So we take the 1 |
| 163 | # leftover bit of c1 and tack it onto c2. Then we take the 2 leftover |
| 164 | # bits of c2 and tack them onto c3. The shifts and masks are intended |
| 165 | # to give us values of exactly 5 bits in width. |
| 166 | c1, c2, c3 = struct.unpack('!HHB', s[i*5:(i+1)*5]) |
| 167 | c2 += (c1 & 1) << 16 # 17 bits wide |
| 168 | c3 += (c2 & 3) << 8 # 10 bits wide |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 169 | encoded += bytes([_b32tab[c1 >> 11], # bits 1 - 5 |
| 170 | _b32tab[(c1 >> 6) & 0x1f], # bits 6 - 10 |
| 171 | _b32tab[(c1 >> 1) & 0x1f], # bits 11 - 15 |
| 172 | _b32tab[c2 >> 12], # bits 16 - 20 (1 - 5) |
| 173 | _b32tab[(c2 >> 7) & 0x1f], # bits 21 - 25 (6 - 10) |
| 174 | _b32tab[(c2 >> 2) & 0x1f], # bits 26 - 30 (11 - 15) |
| 175 | _b32tab[c3 >> 5], # bits 31 - 35 (1 - 5) |
| 176 | _b32tab[c3 & 0x1f], # bits 36 - 40 (1 - 5) |
| 177 | ]) |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 178 | # Adjust for any leftover partial quanta |
| 179 | if leftover == 1: |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 180 | return encoded[:-6] + b'======' |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 181 | elif leftover == 2: |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 182 | return encoded[:-4] + b'====' |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 183 | elif leftover == 3: |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 184 | return encoded[:-3] + b'===' |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 185 | elif leftover == 4: |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 186 | return encoded[:-1] + b'=' |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 187 | return encoded |
| 188 | |
| 189 | |
| 190 | def b32decode(s, casefold=False, map01=None): |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 191 | """Decode a Base32 encoded byte string. |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 192 | |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 193 | s is the byte string to decode. Optional casefold is a flag |
| 194 | specifying whether a lowercase alphabet is acceptable as input. |
| 195 | For security purposes, the default is False. |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 196 | |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 197 | RFC 3548 allows for optional mapping of the digit 0 (zero) to the |
| 198 | letter O (oh), and for optional mapping of the digit 1 (one) to |
| 199 | either the letter I (eye) or letter L (el). The optional argument |
| 200 | map01 when not None, specifies which letter the digit 1 should be |
| 201 | mapped to (when map01 is not None, the digit 0 is always mapped to |
| 202 | the letter O). For security purposes the default is None, so that |
| 203 | 0 and 1 are not allowed in the input. |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 204 | |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 205 | The decoded byte string is returned. binascii.Error is raised if |
| 206 | the input is incorrectly padded or if there are non-alphabet |
| 207 | characters present in the input. |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 208 | """ |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 209 | if not isinstance(s, bytes_types): |
Alexandre Vassalotti | 5209857f | 2008-05-03 04:39:38 +0000 | [diff] [blame] | 210 | raise TypeError("expected bytes, not %s" % s.__class__.__name__) |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 211 | quanta, leftover = divmod(len(s), 8) |
| 212 | if leftover: |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 213 | raise binascii.Error('Incorrect padding') |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 214 | # Handle section 2.4 zero and one mapping. The flag map01 will be either |
| 215 | # False, or the character to map the digit 1 (one) to. It should be |
| 216 | # either L (el) or I (eye). |
Alexandre Vassalotti | 5209857f | 2008-05-03 04:39:38 +0000 | [diff] [blame] | 217 | if map01 is not None: |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 218 | if not isinstance(map01, bytes_types): |
Alexandre Vassalotti | 5209857f | 2008-05-03 04:39:38 +0000 | [diff] [blame] | 219 | raise TypeError("expected bytes, not %s" % map01.__class__.__name__) |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 220 | assert len(map01) == 1, repr(map01) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 221 | s = _translate(s, {b'0': b'O', b'1': map01}) |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 222 | if casefold: |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 223 | s = s.upper() |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 224 | # Strip off pad characters from the right. We need to count the pad |
| 225 | # characters because this will tell us how many null bytes to remove from |
| 226 | # the end of the decoded string. |
| 227 | padchars = 0 |
Antoine Pitrou | fd03645 | 2008-08-19 17:56:33 +0000 | [diff] [blame] | 228 | mo = re.search(b'(?P<pad>[=]*)$', s) |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 229 | if mo: |
| 230 | padchars = len(mo.group('pad')) |
| 231 | if padchars > 0: |
| 232 | s = s[:-padchars] |
| 233 | # Now decode the full quanta |
| 234 | parts = [] |
| 235 | acc = 0 |
| 236 | shift = 35 |
| 237 | for c in s: |
| 238 | val = _b32rev.get(c) |
| 239 | if val is None: |
| 240 | raise TypeError('Non-base32 digit found') |
| 241 | acc += _b32rev[c] << shift |
| 242 | shift -= 5 |
| 243 | if shift < 0: |
Andrew M. Kuchling | 6e57c2a | 2005-06-08 22:51:38 +0000 | [diff] [blame] | 244 | parts.append(binascii.unhexlify('%010x' % acc)) |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 245 | acc = 0 |
| 246 | shift = 35 |
| 247 | # Process the last, partial quanta |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 248 | last = binascii.unhexlify(bytes('%010x' % acc, "ascii")) |
Andrew M. Kuchling | 6e57c2a | 2005-06-08 22:51:38 +0000 | [diff] [blame] | 249 | if padchars == 0: |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 250 | last = b'' # No characters |
Andrew M. Kuchling | 6e57c2a | 2005-06-08 22:51:38 +0000 | [diff] [blame] | 251 | elif padchars == 1: |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 252 | last = last[:-1] |
| 253 | elif padchars == 3: |
| 254 | last = last[:-2] |
| 255 | elif padchars == 4: |
| 256 | last = last[:-3] |
| 257 | elif padchars == 6: |
| 258 | last = last[:-4] |
Andrew M. Kuchling | 6e57c2a | 2005-06-08 22:51:38 +0000 | [diff] [blame] | 259 | else: |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 260 | raise binascii.Error('Incorrect padding') |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 261 | parts.append(last) |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 262 | return b''.join(parts) |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 263 | |
| 264 | |
Antoine Pitrou | fd03645 | 2008-08-19 17:56:33 +0000 | [diff] [blame] | 265 | |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 266 | # RFC 3548, Base 16 Alphabet specifies uppercase, but hexlify() returns |
| 267 | # lowercase. The RFC also recommends against accepting input case |
| 268 | # insensitively. |
| 269 | def b16encode(s): |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 270 | """Encode a byte string using Base16. |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 271 | |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 272 | s is the byte string to encode. The encoded byte string is returned. |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 273 | """ |
Alexandre Vassalotti | 5209857f | 2008-05-03 04:39:38 +0000 | [diff] [blame] | 274 | if not isinstance(s, bytes_types): |
| 275 | raise TypeError("expected bytes, not %s" % s.__class__.__name__) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 276 | return binascii.hexlify(s).upper() |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 277 | |
| 278 | |
| 279 | def b16decode(s, casefold=False): |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 280 | """Decode a Base16 encoded byte string. |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 281 | |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 282 | s is the byte string to decode. Optional casefold is a flag |
| 283 | specifying whether a lowercase alphabet is acceptable as input. |
| 284 | For security purposes, the default is False. |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 285 | |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 286 | The decoded byte string is returned. binascii.Error is raised if |
| 287 | s were incorrectly padded or if there are non-alphabet characters |
| 288 | present in the string. |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 289 | """ |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 290 | if not isinstance(s, bytes_types): |
Alexandre Vassalotti | 5209857f | 2008-05-03 04:39:38 +0000 | [diff] [blame] | 291 | raise TypeError("expected bytes, not %s" % s.__class__.__name__) |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 292 | if casefold: |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 293 | s = s.upper() |
Antoine Pitrou | fd03645 | 2008-08-19 17:56:33 +0000 | [diff] [blame] | 294 | if re.search(b'[^0-9A-F]', s): |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 295 | raise binascii.Error('Non-base16 digit found') |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 296 | return binascii.unhexlify(s) |
| 297 | |
| 298 | |
Antoine Pitrou | fd03645 | 2008-08-19 17:56:33 +0000 | [diff] [blame] | 299 | |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 300 | # Legacy interface. This code could be cleaned up since I don't believe |
| 301 | # binascii has any line length limitations. It just doesn't seem worth it |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 302 | # though. The files should be opened in binary mode. |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 303 | |
Guido van Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 304 | MAXLINESIZE = 76 # Excluding the CRLF |
Guido van Rossum | 54e54c6 | 2001-09-04 19:14:14 +0000 | [diff] [blame] | 305 | MAXBINSIZE = (MAXLINESIZE//4)*3 |
Guido van Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 306 | |
Guido van Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 307 | def encode(input, output): |
Guido van Rossum | 54a40cb | 2007-08-27 22:27:41 +0000 | [diff] [blame] | 308 | """Encode a file; input and output are binary files.""" |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 309 | while True: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 310 | s = input.read(MAXBINSIZE) |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 311 | if not s: |
| 312 | break |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 313 | while len(s) < MAXBINSIZE: |
| 314 | ns = input.read(MAXBINSIZE-len(s)) |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 315 | if not ns: |
| 316 | break |
| 317 | s += ns |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 318 | line = binascii.b2a_base64(s) |
| 319 | output.write(line) |
Guido van Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 320 | |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 321 | |
Guido van Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 322 | def decode(input, output): |
Guido van Rossum | 54a40cb | 2007-08-27 22:27:41 +0000 | [diff] [blame] | 323 | """Decode a file; input and output are binary files.""" |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 324 | while True: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 325 | line = input.readline() |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 326 | if not line: |
| 327 | break |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 328 | s = binascii.a2b_base64(line) |
| 329 | output.write(s) |
Guido van Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 330 | |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 331 | |
Georg Brandl | b54d801 | 2009-06-04 09:11:51 +0000 | [diff] [blame] | 332 | def encodebytes(s): |
| 333 | """Encode a bytestring into a bytestring containing multiple lines |
| 334 | of base-64 data.""" |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 335 | if not isinstance(s, bytes_types): |
Guido van Rossum | 98b349f | 2007-08-27 21:47:52 +0000 | [diff] [blame] | 336 | raise TypeError("expected bytes, not %s" % s.__class__.__name__) |
Peter Schneider-Kamp | fbb2b4c | 2001-06-07 18:56:13 +0000 | [diff] [blame] | 337 | pieces = [] |
| 338 | for i in range(0, len(s), MAXBINSIZE): |
| 339 | chunk = s[i : i + MAXBINSIZE] |
| 340 | pieces.append(binascii.b2a_base64(chunk)) |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 341 | return b"".join(pieces) |
Guido van Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 342 | |
Georg Brandl | b54d801 | 2009-06-04 09:11:51 +0000 | [diff] [blame] | 343 | def encodestring(s): |
| 344 | """Legacy alias of encodebytes().""" |
| 345 | import warnings |
| 346 | warnings.warn("encodestring() is a deprecated alias, use encodebytes()", |
| 347 | DeprecationWarning, 2) |
| 348 | return encodebytes(s) |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 349 | |
Guido van Rossum | 54a40cb | 2007-08-27 22:27:41 +0000 | [diff] [blame] | 350 | |
Georg Brandl | b54d801 | 2009-06-04 09:11:51 +0000 | [diff] [blame] | 351 | def decodebytes(s): |
| 352 | """Decode a bytestring of base-64 data into a bytestring.""" |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 353 | if not isinstance(s, bytes_types): |
Guido van Rossum | 98b349f | 2007-08-27 21:47:52 +0000 | [diff] [blame] | 354 | raise TypeError("expected bytes, not %s" % s.__class__.__name__) |
Peter Schneider-Kamp | fbb2b4c | 2001-06-07 18:56:13 +0000 | [diff] [blame] | 355 | return binascii.a2b_base64(s) |
Guido van Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 356 | |
Georg Brandl | b54d801 | 2009-06-04 09:11:51 +0000 | [diff] [blame] | 357 | def decodestring(s): |
| 358 | """Legacy alias of decodebytes().""" |
| 359 | import warnings |
| 360 | warnings.warn("decodestring() is a deprecated alias, use decodebytes()", |
| 361 | DeprecationWarning, 2) |
| 362 | return decodebytes(s) |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 363 | |
Antoine Pitrou | fd03645 | 2008-08-19 17:56:33 +0000 | [diff] [blame] | 364 | |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 365 | # Usable as a script... |
| 366 | def main(): |
| 367 | """Small main program""" |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 368 | import sys, getopt |
| 369 | try: |
| 370 | opts, args = getopt.getopt(sys.argv[1:], 'deut') |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 371 | except getopt.error as msg: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 372 | sys.stdout = sys.stderr |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 373 | print(msg) |
| 374 | print("""usage: %s [-d|-e|-u|-t] [file|-] |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 375 | -d, -u: decode |
| 376 | -e: encode (default) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 377 | -t: encode and decode string 'Aladdin:open sesame'"""%sys.argv[0]) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 378 | sys.exit(2) |
| 379 | func = encode |
| 380 | for o, a in opts: |
| 381 | if o == '-e': func = encode |
| 382 | if o == '-d': func = decode |
| 383 | if o == '-u': func = decode |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 384 | if o == '-t': test(); return |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 385 | if args and args[0] != '-': |
| 386 | func(open(args[0], 'rb'), sys.stdout) |
| 387 | else: |
| 388 | func(sys.stdin, sys.stdout) |
Guido van Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 389 | |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 390 | |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 391 | def test(): |
| 392 | s0 = b"Aladdin:open sesame" |
| 393 | print(repr(s0)) |
Georg Brandl | 706824f | 2009-06-04 09:42:55 +0000 | [diff] [blame] | 394 | s1 = encodebytes(s0) |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 395 | print(repr(s1)) |
Georg Brandl | 706824f | 2009-06-04 09:42:55 +0000 | [diff] [blame] | 396 | s2 = decodebytes(s1) |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 397 | print(repr(s2)) |
| 398 | assert s0 == s2 |
Guido van Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 399 | |
Barry Warsaw | 4c904d1 | 2004-01-04 01:12:26 +0000 | [diff] [blame] | 400 | |
Guido van Rossum | f194546 | 1995-06-14 23:43:44 +0000 | [diff] [blame] | 401 | if __name__ == '__main__': |
Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 402 | main() |