我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用binascii.Error()。
def b64decode(s, altchars=None, validate=False): """Decode the Base64 encoded bytes-like object or ASCII string s. Optional altchars must be a bytes-like object or ASCII string of length 2 which specifies the alternative alphabet used instead of the '+' and '/' characters. The result is returned as a bytes object. A binascii.Error is raised if s is incorrectly padded. If validate is False (the default), characters that are neither in the normal base-64 alphabet nor the alternative alphabet are discarded prior to the padding check. If validate is True, these non-alphabet characters in the input result in a binascii.Error. """ s = _bytes_from_decode_data(s) if altchars is not None: altchars = _bytes_from_decode_data(altchars) assert len(altchars) == 2, repr(altchars) s = s.translate(bytes.maketrans(altchars, b'+/')) if validate and not re.match(b'^[A-Za-z0-9+/]*={0,2}$', s): raise binascii.Error('Non-base64 digit found') return binascii.a2b_base64(s)
def urlsafe_b64decode(s): """Decode bytes using the URL- and filesystem-safe Base64 alphabet. Argument s is a bytes-like object or ASCII string to decode. The result is returned as a bytes object. A binascii.Error is raised if the input is incorrectly padded. Characters that are not in the URL-safe base-64 alphabet, and are not a plus '+' or slash '/', are discarded prior to the padding check. The alphabet uses '-' instead of '+' and '_' instead of '/'. """ s = _bytes_from_decode_data(s) s = s.translate(_urlsafe_decode_translation) return b64decode(s) # Base32 encoding/decoding must be done in Python
def b16decode(s, casefold=False): """Decode the Base16 encoded bytes-like object or ASCII string s. Optional casefold is a flag specifying whether a lowercase alphabet is acceptable as input. For security purposes, the default is False. The result is returned as a bytes object. A binascii.Error is raised if s is incorrectly padded or if there are non-alphabet characters present in the input. """ s = _bytes_from_decode_data(s) if casefold: s = s.upper() if re.search(b'[^0-9A-F]', s): raise binascii.Error('Non-base16 digit found') return binascii.unhexlify(s) # # Ascii85 encoding/decoding #
def b64decode(s, altchars=None): """Decode a Base64 encoded string. s is the string to decode. Optional altchars must be a string of at least length 2 (additional characters are ignored) which specifies the alternative alphabet used instead of the '+' and '/' characters. The decoded string is returned. A TypeError is raised if s were incorrectly padded or if there are non-alphabet characters present in the string. """ if altchars is not None: s = _translate(s, {altchars[0]: '+', altchars[1]: '/'}) try: return binascii.a2b_base64(s) except binascii.Error, msg: # Transform this exception for consistency raise TypeError(msg)
def _handle_request_exception(self, e): if isinstance(e, Finish): # Not an error; just finish the request without logging. if not self._finished: self.finish(*e.args) return try: self.log_exception(*sys.exc_info()) except Exception: # An error here should still get a best-effort send_error() # to avoid leaking the connection. app_log.error("Error in exception logger", exc_info=True) if self._finished: # Extra errors after the request has been finished should # be logged, but there is no reason to continue to try and # send a response. return if isinstance(e, HTTPError): if e.status_code not in httputil.responses and not e.reason: gen_log.error("Bad HTTP status code: %d", e.status_code) self.send_error(500, exc_info=sys.exc_info()) else: self.send_error(e.status_code, exc_info=sys.exc_info()) else: self.send_error(500, exc_info=sys.exc_info())
def parse_netntlm_resp_msg(headers, resp_header, seq): ''' Parse the client response to the challenge ''' try: header_val3 = headers[resp_header] except KeyError: return header_val3 = header_val3.split(' ', 1) # The header value can either start with NTLM or Negotiate if header_val3[0] == 'NTLM' or header_val3[0] == 'Negotiate': try: msg3 = base64.decodestring(header_val3[1]) except binascii.Error: return return parse_ntlm_resp(msg3, seq)
def _authorize(self): # Authorization, (mostly) per the RFC try: authh = self.getHeader("Authorization") if not authh: self.user = self.password = '' return bas, upw = authh.split() if bas.lower() != "basic": raise ValueError upw = base64.decodestring(upw) self.user, self.password = upw.split(':', 1) except (binascii.Error, ValueError): self.user = self.password = "" except: log.err() self.user = self.password = ""
def _authResponse(self, auth, challenge): self._failresponse = self.esmtpAUTHDeclined try: challenge = base64.decodestring(challenge) except binascii.Error, e: # Illegal challenge, give up, then quit self.sendLine('*') self._okresponse = self.esmtpAUTHMalformedChallenge self._failresponse = self.esmtpAUTHMalformedChallenge else: resp = auth.challengeResponse(self.secret, challenge) self._expected = [235] self._okresponse = self.smtpState_from self.sendLine(encode_base64(resp, eol="")) if auth.getName() == "LOGIN" and challenge == "Username:": self._expected = [334] self._authinfo = auth self._okresponse = self.esmtpState_challenge
def test_ctor_w_key_and_format(self): """constructor -- 'key' and 'format' parameters""" # handle base32 encoding (the default) self.assertEqual(TOTP(KEY1).key, KEY1_RAW) # .. w/ lower case self.assertEqual(TOTP(KEY1.lower()).key, KEY1_RAW) # .. w/ spaces (e.g. user-entered data) self.assertEqual(TOTP(' 4aog gdbb qsyh ntuz ').key, KEY1_RAW) # .. w/ invalid char self.assertRaises(Base32DecodeError, TOTP, 'ao!ggdbbqsyhntuz') # handle hex encoding self.assertEqual(TOTP('e01c630c2184b076ce99', 'hex').key, KEY1_RAW) # .. w/ invalid char self.assertRaises(Base16DecodeError, TOTP, 'X01c630c2184b076ce99', 'hex') # handle raw bytes self.assertEqual(TOTP(KEY1_RAW, "raw").key, KEY1_RAW)
def _get_basicauth_credentials(request): authorization = AUTHORIZATION(request.environ) try: authmeth, auth = authorization.split(' ', 1) except ValueError: # not enough values to unpack return None if authmeth.lower() == 'basic': try: auth = base64.b64decode(auth.strip().encode('ascii')) except binascii.Error: # can't decode return None try: login, password = auth.decode('utf8').split(':', 1) except ValueError: # not enough values to unpack return None return {'login': login, 'password': password} return None
def decode_b64jose(data, size=None, minimum=False): """Decode JOSE Base-64 field. :param unicode data: :param int size: Required length (after decoding). :param bool minimum: If ``True``, then `size` will be treated as minimum required length, as opposed to exact equality. :rtype: bytes """ error_cls = TypeError if six.PY2 else binascii.Error try: decoded = b64.b64decode(data.encode()) except error_cls as error: raise errors.DeserializationError(error) if size is not None and ((not minimum and len(decoded) != size) or (minimum and len(decoded) < size)): raise errors.DeserializationError( "Expected at least or exactly {0} bytes".format(size)) return decoded
def decode_hex16(value, size=None, minimum=False): """Decode hexlified field. :param unicode value: :param int size: Required length (after decoding). :param bool minimum: If ``True``, then `size` will be treated as minimum required length, as opposed to exact equality. :rtype: bytes """ value = value.encode() if size is not None and ((not minimum and len(value) != size * 2) or (minimum and len(value) < size * 2)): raise errors.DeserializationError() error_cls = TypeError if six.PY2 else binascii.Error try: return binascii.unhexlify(value) except error_cls as error: raise errors.DeserializationError(error)
def deserialize_compact(jwt): """ Deserialization of a compact representation of a :class:`~jwt.JWE` :param jwt: The serialized JWT to deserialize. :rtype: :class:`~jose.JWT`. :raises: :class:`~jose.Error` if the JWT is malformed """ parts = jwt.split(six.b('.')) # http://tools.ietf.org/html/ # draft-ietf-jose-json-web-encryption-23#section-9 if len(parts) == 3: token_type = JWS elif len(parts) == 5: token_type = JWE else: raise Error('Malformed JWT') return token_type(*parts)
def b64decode(s, altchars=None): """Decode a Base64 encoded string. s is the string to decode. Optional altchars must be a string of at least length 2 (additional characters are ignored) which specifies the alternative alphabet used instead of the '+' and '/' characters. The decoded string is returned. A TypeError is raised if s were incorrectly padded or if there are non-alphabet characters present in the string. """ if altchars is not None: s = s.translate(string.maketrans(altchars[:2], '+/')) try: return binascii.a2b_base64(s) except binascii.Error, msg: # Transform this exception for consistency raise TypeError(msg)
def _get_user(self, environ): user = None http_auth = environ.get("HTTP_AUTHORIZATION") if http_auth and http_auth.startswith('Basic'): auth = http_auth.split(" ", 1) if len(auth) == 2: try: # b64decode doesn't accept unicode in Python < 3.3 # so we need to convert it to a byte string auth = base64.b64decode(auth[1].strip().encode('utf-8')) if PY3: # b64decode returns a byte string in Python 3 auth = auth.decode('utf-8') auth = auth.split(":", 1) except TypeError as exc: self.debug("Couldn't get username: %s", exc) return user except binascii.Error as exc: self.debug("Couldn't get username: %s", exc) return user if len(auth) == 2: user = auth[0] return user
def parse_as_public_pair(s): try: if s[:2] in (["02", "03", "04"]): return encoding.sec_to_public_pair(h2b(s)) except (encoding.EncodingError, binascii.Error): pass for c in ",/": if c in s: s0, s1 = s.split(c, 1) v0 = parse_as_number(s0) if v0: if s1 in ("even", "odd"): return ecdsa.public_pair_for_x(ecdsa.generator_secp256k1, v0, is_even=(s1=='even')) v1 = parse_as_number(s1) if v1: if not ecdsa.is_public_pair_valid(ecdsa.generator_secp256k1, (v0, v1)): sys.stderr.write("invalid (x, y) pair\n") sys.exit(1) return (v0, v1)
def netcode_and_type_for_text(text): # check for "public pair" try: LENGTH_LOOKUP = { 33: "public_pair", 65: "public_pair", 16: "elc_seed", 32: "elc_prv", 64: "elc_pub", } as_bin = h2b(text) l = len(as_bin) if l in LENGTH_LOOKUP: return None, LENGTH_LOOKUP[l], as_bin except (binascii.Error, TypeError): pass data = encoding.a2b_hashed_base58(text) netcode, the_type = netcode_and_type_for_data(data) length = 1 if the_type in ["wif", "address"] else 4 return netcode, the_type, data[length:]
def authenticate(self, request): """ Returns a `User` if a correct username and password have been supplied using HTTP Basic authentication. Otherwise returns `None`. """ auth = get_authorization_header(request).split() if not auth or auth[0].lower() != b'basic': return None if len(auth) == 1: msg = _('Invalid basic header. No credentials provided.') raise exceptions.AuthenticationFailed(msg) elif len(auth) > 2: msg = _('Invalid basic header. Credentials string should not contain spaces.') raise exceptions.AuthenticationFailed(msg) try: auth_parts = base64.b64decode(auth[1]).decode(HTTP_HEADER_ENCODING).partition(':') except (TypeError, UnicodeDecodeError, binascii.Error): msg = _('Invalid basic header. Credentials not correctly base64 encoded.') raise exceptions.AuthenticationFailed(msg) userid, password = auth_parts[0], auth_parts[2] return self.authenticate_credentials(userid, password)
def LoadPlugins(plugins, plugindir, verbose): if plugins == '': return if plugindir == '': scriptPath = os.path.dirname(sys.argv[0]) else: scriptPath = plugindir for plugin in sum(map(ProcessAt, plugins.split(',')), []): try: if not plugin.lower().endswith('.py'): plugin += '.py' if os.path.dirname(plugin) == '': if not os.path.exists(plugin): scriptPlugin = os.path.join(scriptPath, plugin) if os.path.exists(scriptPlugin): plugin = scriptPlugin exec open(plugin, 'r') in globals(), globals() except Exception as e: print('Error loading plugin: %s' % plugin) if verbose: raise e
def deserialize_compact(jwt): """ Deserialization of a compact representation of a :class:`~jwt.JWE` :param jwt: The serialized JWT to deserialize. :rtype: :class:`~jose.JWT`. :raises: :class:`~jose.Error` if the JWT is malformed """ parts = jwt.split(b'.') # http://tools.ietf.org/html/ # draft-ietf-jose-json-web-encryption-23#section-9 if len(parts) == 3: token_type = JWS elif len(parts) == 5: token_type = JWE else: raise Error('Malformed JWT') return token_type(*parts)
def normalize_transactiontest_fixture(fixture): normalized_fixture = {} if 'blocknumber' in fixture: normalized_fixture['blocknumber'] = to_int(fixture['blocknumber']) try: normalized_fixture['rlp'] = decode_hex(fixture['rlp']) except binascii.Error: normalized_fixture['rlpHex'] = fixture['rlp'] if "sender" in fixture: # intentionally not normalized. normalized_fixture["transaction"] = fixture['transaction'] # intentionally not normalized. normalized_fixture['sender'] = fixture['sender'] return normalized_fixture
def _try_potential_passwords(self): valid_passwords = list() for potential_password in self._potential_passwords: try: potential_password = base64.b64decode(potential_password) except binascii.Error: continue else: try: user, password = potential_password.split(':', maxsplit=1) valid_passwords.append((user, password)) except IndexError: continue return valid_passwords
def test_uu(self): MAX_UU = 45 lines = [] for i in range(0, len(self.data), MAX_UU): b = self.type2test(self.rawdata[i:i+MAX_UU]) a = binascii.b2a_uu(b) lines.append(a) res = bytes() for line in lines: a = self.type2test(line) b = binascii.a2b_uu(a) res += b self.assertEqual(res, self.rawdata) self.assertEqual(binascii.a2b_uu(b"\x7f"), b"\x00"*31) self.assertEqual(binascii.a2b_uu(b"\x80"), b"\x00"*32) self.assertEqual(binascii.a2b_uu(b"\xff"), b"\x00"*31) self.assertRaises(binascii.Error, binascii.a2b_uu, b"\xff\x00") self.assertRaises(binascii.Error, binascii.a2b_uu, b"!!!!") self.assertRaises(binascii.Error, binascii.b2a_uu, 46*b"!") # Issue #7701 (crash on a pydebug build) self.assertEqual(binascii.b2a_uu(b'x'), b'!> \n')
def b64decode(s, altchars=None, validate=False): """Decode a Base64 encoded byte string. s is the byte string to decode. Optional altchars must be a string of length 2 which specifies the alternative alphabet used instead of the '+' and '/' characters. The decoded string is returned. A binascii.Error is raised if s is incorrectly padded. If validate is False (the default), non-base64-alphabet characters are discarded prior to the padding check. If validate is True, non-base64-alphabet characters in the input result in a binascii.Error. """ if not isinstance(s, bytes_types): raise TypeError("expected bytes, not %s" % s.__class__.__name__) if altchars is not None: if not isinstance(altchars, bytes_types): raise TypeError("expected bytes, not %s" % altchars.__class__.__name__) assert len(altchars) == 2, repr(altchars) s = _translate(s, {chr(altchars[0]): b'+', chr(altchars[1]): b'/'}) if validate and not re.match(b'^[A-Za-z0-9+/]*={0,2}$', s): raise binascii.Error('Non-base64 digit found') return binascii.a2b_base64(s)
def b16decode(s, casefold=False): """Decode a Base16 encoded byte string. s is the byte string to decode. Optional casefold is a flag specifying whether a lowercase alphabet is acceptable as input. For security purposes, the default is False. The decoded byte string is returned. binascii.Error is raised if s were incorrectly padded or if there are non-alphabet characters present in the string. """ if not isinstance(s, bytes_types): raise TypeError("expected bytes, not %s" % s.__class__.__name__) if casefold: s = s.upper() if re.search(b'[^0-9A-F]', s): raise binascii.Error('Non-base16 digit found') return binascii.unhexlify(s) # Legacy interface. This code could be cleaned up since I don't believe # binascii has any line length limitations. It just doesn't seem worth it # though. The files should be opened in binary mode.
def LoadPlugins(plugins, verbose): if plugins == '': return scriptPath = os.path.dirname(sys.argv[0]) for plugin in sum(map(ProcessAt, plugins.split(',')), []): try: if not plugin.lower().endswith('.py'): plugin += '.py' if os.path.dirname(plugin) == '': if not os.path.exists(plugin): scriptPlugin = os.path.join(scriptPath, plugin) if os.path.exists(scriptPlugin): plugin = scriptPlugin exec open(plugin, 'r') in globals(), globals() except Exception as e: print('Error loading plugin: %s' % plugin) if verbose: raise e
def test_uu(self): MAX_UU = 45 lines = [] for i in range(0, len(self.data), MAX_UU): b = self.type2test(self.rawdata[i:i+MAX_UU]) a = binascii.b2a_uu(b) lines.append(a) res = "" for line in lines: a = self.type2test(line) b = binascii.a2b_uu(a) res += b self.assertEqual(res, self.rawdata) self.assertEqual(binascii.a2b_uu("\x7f"), "\x00"*31) self.assertEqual(binascii.a2b_uu("\x80"), "\x00"*32) self.assertEqual(binascii.a2b_uu("\xff"), "\x00"*31) self.assertRaises(binascii.Error, binascii.a2b_uu, "\xff\x00") self.assertRaises(binascii.Error, binascii.a2b_uu, "!!!!") self.assertRaises(binascii.Error, binascii.b2a_uu, 46*"!") # Issue #7701 (crash on a pydebug build) self.assertEqual(binascii.b2a_uu('x'), '!> \n')
def uu_decode(input, errors='strict'): assert errors == 'strict' infile = BytesIO(input) outfile = BytesIO() readline = infile.readline write = outfile.write # Find start of encoded data while 1: s = readline() if not s: raise ValueError('Missing "begin" line in input data') if s[:5] == b'begin': break # Decode while True: s = readline() if not s or s == b'end\n': break try: data = binascii.a2b_uu(s) except binascii.Error as v: # Workaround for broken uuencoders by /Fredrik Lundh nbytes = (((s[0]-32) & 63) * 4 + 5) // 3 data = binascii.a2b_uu(s[:nbytes]) #sys.stderr.write("Warning: %s\n" % str(v)) write(data) if not s: raise ValueError('Truncated input data') return (outfile.getvalue(), len(input))
def standard_b64decode(s): """Decode bytes encoded with the standard Base64 alphabet. Argument s is a bytes-like object or ASCII string to decode. The result is returned as a bytes object. A binascii.Error is raised if the input is incorrectly padded. Characters that are not in the standard alphabet are discarded prior to the padding check. """ return b64decode(s)
def validate_token(key, token, user_id, action_id="", current_time=None): """Validates that the given token authorizes the user for the action. Tokens are invalid if the time of issue is too old or if the token does not match what generateToken outputs (i.e. the token was forged). Args: key: secret key to use. token: a string of the token generated by generateToken. user_id: the user ID of the authenticated user. action_id: a string identifier of the action they requested authorization for. Returns: A boolean - True if the user is authorized for the action, False otherwise. """ if not token: return False try: decoded = base64.urlsafe_b64decode(token) token_time = int(decoded.split(DELIMITER)[-1]) except (TypeError, ValueError, binascii.Error): return False if current_time is None: current_time = time.time() # If the token is too old it's not valid. if current_time - token_time > DEFAULT_TIMEOUT_SECS: return False # The given token should match the generated one with the same time. expected_token = generate_token(key, user_id, action_id=action_id, when=token_time) if len(token) != len(expected_token): return False # Perform constant time comparison to avoid timing attacks different = 0 for x, y in zip(bytearray(token), bytearray(expected_token)): different |= x ^ y return not different