def _parse_password(self, s): if len(s) < 22: raise DecodeError("wifi oob password less than 22 byte") password = dict() password['public-key-hash'] = s[0:20] password['password-id'] = struct.unpack('>H', s[20:22])[0] password['password'] = s[22:] return password
def read_byte(self) -> int: if len(self.data) == 0: raise DecodeError('Reached end of packet') b = self.data[0] self.data = self.data[1:] return b
def read_bool(self) -> bool: b = self.read_byte() if b == 1: return True elif b == 0: return False else: raise DecodeError('Invalid value for bool: ' + str(b))
def decode(self, data): """ Decoder implementation of the Bencode algorithm @param data: The encoded data @type data: str @note: This is a convenience wrapper for the recursive decoding algorithm, C{_decodeRecursive} @return: The decoded data, as a native Python type @rtype: int, list, dict or str """ if len(data) == 0: raise DecodeError('Cannot decode empty string') try: return self._decodeRecursive(data)[0] except ValueError as e: raise DecodeError(e.message)
def data(self, string): log.debug("parse '{0}' record".format(self.type)) if len(string) > 0: attributes = parse_attributes(string) log.debug("wifi attributes: " + repr(attributes)) for k, v in attributes: if k in (VERSION1, VERSION2): self._version = v elif k == OOB_PASSWORD: self._passwords.append(self._parse_password(v)) else: self._other.append((k, v)) if len(self._passwords) == 0: raise DecodeError("missing password attribute")
def data(self, string): log.debug("parse '{0}' record".format(self.type)) if len(string) > 0: attributes = parse_attributes(string) log.debug("wifi attributes: " + repr(attributes)) for k, v in attributes: if k in (VERSION1, VERSION2): self._version = v elif k == CREDENTIAL: self._credentials.append(self._parse_credential(v)) else: self._other.append((k, v)) if len(self._credentials) == 0: raise DecodeError("missing credential attribute")
def parse_element(f): k, l = struct.unpack(">cB", f.read(2)); v = f.read(l) if len(v) != l: raise DecodeError("wfa subelement length error") return k, v
def parse_attribute(f): k, l = struct.unpack('>2sH', f.read(4)) v = f.read(l) if len(v) != l: raise DecodeError("wsc attribute length error") return k, v