Python binascii.crc_hqx() Examples

The following are 30 code examples of binascii.crc_hqx(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module binascii , or try the search function .
Example #1
Source File: protocol.py    From huawei-lpv2 with MIT License 7 votes vote down vote up
def from_bytes(data: bytes) -> "Packet":

        if len(data) < 1 + 2 + 1 + 2:
            raise ValueError("packet too short")

        magic, _, payload, checksum = data[0], data[1:3], data[4:-2], data[-2:]

        if magic != ord(HUAWEI_LPV2_MAGIC):
            raise ValueError(f"magic mismatch: {magic} != {HUAWEI_LPV2_MAGIC}")

        actual_checksum = encode_int(binascii.crc_hqx(data[:-2], 0))

        if actual_checksum != checksum:
            raise ValueError(f"checksum mismatch: {actual_checksum} != {checksum}")

        return Packet(service_id=payload[0], command_id=payload[1], command=Command.from_bytes(payload[2:])) 
Example #2
Source File: test_binascii.py    From ironpython3 with Apache License 2.0 6 votes vote down vote up
def test_returned_value(self):
        # Limit to the minimum of all limits (b2a_uu)
        MAX_ALL = 45
        raw = self.rawdata[:MAX_ALL]
        for fa, fb in zip(a2b_functions, b2a_functions):
            a2b = getattr(binascii, fa)
            b2a = getattr(binascii, fb)
            try:
                a = b2a(self.type2test(raw))
                res = a2b(self.type2test(a))
            except Exception as err:
                self.fail("{}/{} conversion raises {!r}".format(fb, fa, err))
            if fb == 'b2a_hqx':
                # b2a_hqx returns a tuple
                res, _ = res
            self.assertEqual(res, raw, "{}/{} conversion: "
                             "{!r} != {!r}".format(fb, fa, res, raw))
            self.assertIsInstance(res, bytes)
            self.assertIsInstance(a, bytes)
            self.assertLess(max(a), 128)
        self.assertIsInstance(binascii.crc_hqx(raw, 0), int)
        self.assertIsInstance(binascii.crc32(raw), int) 
Example #3
Source File: test_binascii.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 6 votes vote down vote up
def test_returned_value(self):
        # Limit to the minimum of all limits (b2a_uu)
        MAX_ALL = 45
        raw = self.rawdata[:MAX_ALL]
        for fa, fb in zip(a2b_functions, b2a_functions):
            a2b = getattr(binascii, fa)
            b2a = getattr(binascii, fb)
            try:
                a = b2a(self.type2test(raw))
                res = a2b(self.type2test(a))
            except Exception as err:
                self.fail("{}/{} conversion raises {!r}".format(fb, fa, err))
            if fb == 'b2a_hqx':
                # b2a_hqx returns a tuple
                res, _ = res
            self.assertEqual(res, raw, "{}/{} conversion: "
                             "{!r} != {!r}".format(fb, fa, res, raw))
            self.assertIsInstance(res, bytes)
            self.assertIsInstance(a, bytes)
            self.assertLess(max(a), 128)
        self.assertIsInstance(binascii.crc_hqx(raw, 0), int)
        self.assertIsInstance(binascii.crc32(raw), int) 
Example #4
Source File: test_binascii.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def test_returned_value(self):
        # Limit to the minimum of all limits (b2a_uu)
        MAX_ALL = 45
        raw = self.rawdata[:MAX_ALL]
        for fa, fb in zip(a2b_functions, b2a_functions):
            a2b = getattr(binascii, fa)
            b2a = getattr(binascii, fb)
            try:
                a = b2a(self.type2test(raw))
                res = a2b(self.type2test(a))
            except Exception as err:
                self.fail("{}/{} conversion raises {!r}".format(fb, fa, err))
            if fb == 'b2a_hqx':
                # b2a_hqx returns a tuple
                res, _ = res
            self.assertEqual(res, raw, "{}/{} conversion: "
                             "{!r} != {!r}".format(fb, fa, res, raw))
            self.assertIsInstance(res, bytes)
            self.assertIsInstance(a, bytes)
            self.assertLess(max(a), 128)
        self.assertIsInstance(binascii.crc_hqx(raw, 0), int)
        self.assertIsInstance(binascii.crc32(raw), int) 
Example #5
Source File: binhex.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def _checkcrc(self):
        filecrc = struct.unpack('>h', self.ifp.read(2))[0] & 0xffff
        #self.crc = binascii.crc_hqx('\0\0', self.crc)
        # XXXX Is this needed??
        self.crc = self.crc & 0xffff
        if filecrc != self.crc:
            raise Error, 'CRC error, computed %x, read %x' \
                  %(self.crc, filecrc)
        self.crc = 0 
Example #6
Source File: binhex.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def _read(self, len):
        data = self.ifp.read(len)
        self.crc = binascii.crc_hqx(data, self.crc)
        return data 
Example #7
Source File: binhex.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def _checkcrc(self):
        filecrc = struct.unpack('>h', self.ifp.read(2))[0] & 0xffff
        #self.crc = binascii.crc_hqx('\0\0', self.crc)
        # XXXX Is this needed??
        self.crc = self.crc & 0xffff
        if filecrc != self.crc:
            raise Error('CRC error, computed %x, read %x'
                        % (self.crc, filecrc))
        self.crc = 0 
Example #8
Source File: test_binascii.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_empty_string(self):
        # A test for SF bug #1022953.  Make sure SystemError is not raised.
        empty = self.type2test(b'')
        for func in all_functions:
            if func == 'crc_hqx':
                # crc_hqx needs 2 arguments
                binascii.crc_hqx(empty, 0)
                continue
            f = getattr(binascii, func)
            try:
                f(empty)
            except Exception as err:
                self.fail("{}({!r}) raises {!r}".format(func, empty, err)) 
Example #9
Source File: binhex.py    From RevitBatchProcessor with GNU General Public License v3.0 5 votes vote down vote up
def _write(self, data):
        self.crc = binascii.crc_hqx(data, self.crc)
        self.ofp.write(data) 
Example #10
Source File: binhex.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def _write(self, data):
        self.crc = binascii.crc_hqx(data, self.crc)
        self.ofp.write(data) 
Example #11
Source File: binhex.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def _read(self, len):
        data = self.ifp.read(len)
        self.crc = binascii.crc_hqx(data, self.crc)
        return data 
Example #12
Source File: binhex.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def _write(self, data):
        self.crc = binascii.crc_hqx(data, self.crc)
        self.ofp.write(data) 
Example #13
Source File: test_binascii.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_empty_string(self):
        # A test for SF bug #1022953.  Make sure SystemError is not raised.
        empty = self.type2test('')
        for func in all_functions:
            if func == 'crc_hqx':
                # crc_hqx needs 2 arguments
                binascii.crc_hqx(empty, 0)
                continue
            f = getattr(binascii, func)
            try:
                f(empty)
            except Exception, err:
                self.fail("{}({!r}) raises {!r}".format(func, empty, err)) 
Example #14
Source File: binhex.py    From RevitBatchProcessor with GNU General Public License v3.0 5 votes vote down vote up
def _read(self, len):
        data = self.ifp.read(len)
        self.crc = binascii.crc_hqx(data, self.crc)
        return data 
Example #15
Source File: binhex.py    From PokemonGo-DesktopMap with MIT License 5 votes vote down vote up
def _write(self, data):
        self.crc = binascii.crc_hqx(data, self.crc)
        self.ofp.write(data) 
Example #16
Source File: binhex.py    From RevitBatchProcessor with GNU General Public License v3.0 5 votes vote down vote up
def _checkcrc(self):
        filecrc = struct.unpack('>h', self.ifp.read(2))[0] & 0xffff
        #self.crc = binascii.crc_hqx('\0\0', self.crc)
        # XXXX Is this needed??
        self.crc = self.crc & 0xffff
        if filecrc != self.crc:
            raise Error, 'CRC error, computed %x, read %x' \
                  %(self.crc, filecrc)
        self.crc = 0 
Example #17
Source File: binhex.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def _checkcrc(self):
        filecrc = struct.unpack('>h', self.ifp.read(2))[0] & 0xffff
        #self.crc = binascii.crc_hqx('\0\0', self.crc)
        # XXXX Is this needed??
        self.crc = self.crc & 0xffff
        if filecrc != self.crc:
            raise Error, 'CRC error, computed %x, read %x' \
                  %(self.crc, filecrc)
        self.crc = 0 
Example #18
Source File: binhex.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def _writecrc(self):
        # XXXX Should this be here??
        # self.crc = binascii.crc_hqx('\0\0', self.crc)
        if self.crc < 0:
            fmt = '>h'
        else:
            fmt = '>H'
        self.ofp.write(struct.pack(fmt, self.crc))
        self.crc = 0 
Example #19
Source File: binhex.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def _write(self, data):
        self.crc = binascii.crc_hqx(data, self.crc)
        self.ofp.write(data) 
Example #20
Source File: binhex.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def _checkcrc(self):
        filecrc = struct.unpack('>h', self.ifp.read(2))[0] & 0xffff
        #self.crc = binascii.crc_hqx('\0\0', self.crc)
        # XXXX Is this needed??
        self.crc = self.crc & 0xffff
        if filecrc != self.crc:
            raise Error, 'CRC error, computed %x, read %x' \
                  %(self.crc, filecrc)
        self.crc = 0 
Example #21
Source File: binhex.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def _read(self, len):
        data = self.ifp.read(len)
        self.crc = binascii.crc_hqx(data, self.crc)
        return data 
Example #22
Source File: binhex.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def _writecrc(self):
        # XXXX Should this be here??
        # self.crc = binascii.crc_hqx('\0\0', self.crc)
        if self.crc < 0:
            fmt = '>h'
        else:
            fmt = '>H'
        self.ofp.write(struct.pack(fmt, self.crc))
        self.crc = 0 
Example #23
Source File: binhex.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def _write(self, data):
        self.crc = binascii.crc_hqx(data, self.crc)
        self.ofp.write(data) 
Example #24
Source File: test_binascii.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_empty_string(self):
        # A test for SF bug #1022953.  Make sure SystemError is not raised.
        empty = self.type2test('')
        for func in all_functions:
            if func == 'crc_hqx':
                # crc_hqx needs 2 arguments
                binascii.crc_hqx(empty, 0)
                continue
            f = getattr(binascii, func)
            try:
                f(empty)
            except Exception, err:
                self.fail("{}({!r}) raises {!r}".format(func, empty, err)) 
Example #25
Source File: switch.py    From home-assistant-custom-components with MIT License 5 votes vote down vote up
def crc_sign_full_packet_com_key(pData):
    """CRC calculation"""
    try:
        crc = (ba.hexlify(pack('>I', ba.crc_hqx(ba.unhexlify(pData), 0x1021)))).decode('utf-8')
        pData = pData + crc[6:8] + crc[4:6]
        crc = crc[6:8] + crc[4:6] + (ba.hexlify(REMOTE_KEY)).decode('utf-8')
        crc = (ba.hexlify(pack('>I', ba.crc_hqx(ba.unhexlify(crc), 0x1021)))).decode('utf-8')
        pData = pData + crc[6:8] + crc[4:6]
        return pData
    except:
        _LOGGER.exception('failed to sign crc ' + traceback.format_exc())
        return None 
Example #26
Source File: __init__.py    From home-assistant-custom-components with MIT License 5 votes vote down vote up
def crc_sign_full_packet_com_key(data):
    """CRC calculation"""
    try:
        crc = ba.hexlify(pack('>I', ba.crc_hqx(ba.unhexlify(data), 0x1021))).decode(ENCODING_CODEC)
        data = data + crc[6:8] + crc[4:6]
        crc = crc[6:8] + crc[4:6] + (ba.hexlify(REMOTE_KEY)).decode(ENCODING_CODEC)
        crc = ba.hexlify(pack('>I', ba.crc_hqx(ba.unhexlify(crc), 0x1021))).decode(ENCODING_CODEC)
        data = data + crc[6:8] + crc[4:6]
        return data
    except:
        _LOGGER.exception('failed to sign crc ' + traceback.format_exc())
        raise 
Example #27
Source File: test_binascii.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_unicode_b2a(self):
        # Unicode strings are not accepted by b2a_* functions.
        for func in set(all_functions) - set(a2b_functions) | {'rledecode_hqx'}:
            if sys.implementation.name == "ironpython" and func == 'rledecode_hqx': continue
            try:
                self.assertRaises(TypeError, getattr(binascii, func), "test")
            except Exception as err:
                self.fail('{}("test") raises {!r}'.format(func, err))
        # crc_hqx needs 2 arguments
        self.assertRaises(TypeError, binascii.crc_hqx, "test", 0) 
Example #28
Source File: test_binascii.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_empty_string(self):
        # A test for SF bug #1022953.  Make sure SystemError is not raised.
        empty = self.type2test(b'')
        for func in all_functions:
            if func == 'crc_hqx':
                # crc_hqx needs 2 arguments
                binascii.crc_hqx(empty, 0)
                continue
            f = getattr(binascii, func)
            try:
                f(empty)
            except Exception as err:
                self.fail("{}({!r}) raises {!r}".format(func, empty, err)) 
Example #29
Source File: binhex.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def _checkcrc(self):
        filecrc = struct.unpack('>h', self.ifp.read(2))[0] & 0xffff
        #self.crc = binascii.crc_hqx('\0\0', self.crc)
        # XXXX Is this needed??
        self.crc = self.crc & 0xffff
        if filecrc != self.crc:
            raise Error('CRC error, computed %x, read %x'
                        % (self.crc, filecrc))
        self.crc = 0 
Example #30
Source File: binhex.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def _read(self, len):
        data = self.ifp.read(len)
        self.crc = binascii.crc_hqx(data, self.crc)
        return data