Python six.iterbytes() Examples

The following are 30 code examples of six.iterbytes(). 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 six , or try the search function .
Example #1
Source File: mutable_data.py    From pwnypack with MIT License 6 votes vote down vote up
def gnu_as_mutable_data_finalizer(get_pc, comment_char, align=True):
    def data_finalizer(env, code, data):
        return (get_pc(env, code) if data or env.buffers else []) + code + [
            '',
            '.pool',
        ] + ([
            '.align',
        ] if align else []) + [
            '__data:',
        ] + [
            '\t.byte %s  %s %r' % (
                ', '.join(hex(b) for b in six.iterbytes(datum)),
                comment_char,
                orig_datum,
            )
            for datum, (_, orig_datum) in six.iteritems(data)
        ]
    return data_finalizer 
Example #2
Source File: test_protocol.py    From python-suitcase with Mozilla Public License 2.0 6 votes vote down vote up
def test_protocol_magic_scan_single_byte(self):
        rx = []

        def cb(packet):
            rx.append(packet)

        protocol_handler = StreamProtocolHandler(MagicSchema, cb)
        test_sequence = MagicSchema()
        test_sequence.value = -29939
        pack = test_sequence.pack()

        # garbage with our bytes in the middle
        test_bytes = b'\x1A\x3fadbsfkasdf;aslkfjasd;f' + pack + b'\x00\x00asdfn234r'
        for b in six.iterbytes(test_bytes):
            protocol_handler.feed(six.b(chr(b)))
        self.assertEqual(len(rx), 1)
        self.assertEqual(rx[0].value, -29939) 
Example #3
Source File: types.py    From PGPy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def crc24(data):
        # CRC24 computation, as described in the RFC 4880 section on Radix-64 Conversions
        #
        # The checksum is a 24-bit Cyclic Redundancy Check (CRC) converted to
        # four characters of radix-64 encoding by the same MIME base64
        # transformation, preceded by an equal sign (=).  The CRC is computed
        # by using the generator 0x864CFB and an initialization of 0xB704CE.
        # The accumulation is done on the data before it is converted to
        # radix-64, rather than on the converted data.
        crc = Armorable.__crc24_init

        if not isinstance(data, bytearray):
            data = six.iterbytes(data)

        for b in data:
            crc ^= b << 16

            for i in range(8):
                crc <<= 1
                if crc & 0x1000000:
                    crc ^= Armorable.__crc24_poly

        return crc & 0xFFFFFF 
Example #4
Source File: instruction.py    From ida-minsc with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def memory(ea, op):
        '''Operand type decoder for returning a memory reference on either the AArch32 or AArch64 architectures.'''
        get_dtype_attribute = operator.attrgetter('dtyp' if idaapi.__version__ < 7.0 else 'dtype')
        get_dtype_size = idaapi.get_dtyp_size if idaapi.__version__ < 7.0 else idaapi.get_dtype_size
        get_bytes = idaapi.get_many_bytes if idaapi.__version__ < 7.0 else idaapi.get_bytes

        # get the address and the operand size
        addr, size = op.addr, get_dtype_size(get_dtype_attribute(op))
        maxval = 1<<size*8

        # dereference the address and return its integer.
        res = get_bytes(addr, size) or ''
        res = reversed(res) if database.config.byteorder() == 'little' else iter(res)
        res = reduce(lambda agg, n: (agg*0x100)|n, six.iterbytes(res), 0)
        sf = bool(res & maxval>>1)

        return armops.memory(long(addr), long(res-maxval) if sf else long(res)) 
Example #5
Source File: __init__.py    From privacyidea with GNU Affero General Public License v3.0 6 votes vote down vote up
def checksum(msg):
    """
    Calculate CRC-16 (16-bit ISO 13239 1st complement) checksum.
    (see Yubikey-Manual - Chapter 6: Implementation details)

    :param msg: input byte string for crc calculation
    :type msg: bytes
    :return: crc16 checksum of msg
    :rtype: int
    """
    crc = 0xffff
    for b in six.iterbytes(msg):
        crc = crc ^ (b & 0xff)
        for _j in range(0, 8):
            n = crc & 1
            crc = crc >> 1
            if n != 0:
                crc = crc ^ 0x8408
    return crc 
Example #6
Source File: test_six.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def test_bytesiter():
    it = six.iterbytes(six.b("hi"))
    assert six.next(it) == ord("h")
    assert six.next(it) == ord("i")
    py.test.raises(StopIteration, six.next, it) 
Example #7
Source File: test_six.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def test_bytesiter():
    it = six.iterbytes(six.b("hi"))
    assert six.next(it) == ord("h")
    assert six.next(it) == ord("i")
    py.test.raises(StopIteration, six.next, it) 
Example #8
Source File: protocol.py    From android_universal with MIT License 5 votes vote down vote up
def feed(self, data):
        """
        Feed data to the parser.
        """
        assert isinstance(data, binary_type)
        for b in iterbytes(data):
            self._parser.send(int2byte(b)) 
Example #9
Source File: can_signal_conversion.py    From nixnet-python with MIT License 5 votes vote down vote up
def main():
    database_name = 'NIXNET_example'
    cluster_name = 'CAN_Cluster'
    signal_names = ['CANEventSignal1', 'CANEventSignal2']

    with convert.SignalConversionSinglePointSession(
            database_name,
            cluster_name,
            signal_names) as session:

        user_value = six.moves.input('Enter {} signal values [float, float]: '.format(len(signal_names)))
        try:
            expected_signals = [float(x.strip()) for x in user_value.split(",")]
        except ValueError:
            expected_signals = [24.5343, 77.0129]
            print('Unrecognized input ({}). Setting data buffer to {}'.format(user_value, expected_signals))

        if len(expected_signals) != len(signal_names):
            expected_signals = [24.5343, 77.0129]
            print('Invalid number of signal values entered. Setting data buffer to {}'.format(expected_signals))

        frames = session.convert_signals_to_frames(expected_signals)
        print('Frames:')
        for frame in frames:
            print('    {}'.format(frame))
            print('        payload={}'.format(list(six.iterbytes(frame.payload))))

        converted_signals = session.convert_frames_to_signals(frames)
        print('Signals: {}'.format([v for (_, v) in converted_signals])) 
Example #10
Source File: test_six.py    From c4ddev with MIT License 5 votes vote down vote up
def test_bytesiter():
    it = six.iterbytes(six.b("hi"))
    assert six.next(it) == ord("h")
    assert six.next(it) == ord("i")
    py.test.raises(StopIteration, six.next, it) 
Example #11
Source File: rq.py    From MIA-Dictionary-Addon with GNU General Public License v3.0 5 votes vote down vote up
def pack_value(self, val):
        """Convert 8-byte string into 16-byte list"""
        if isinstance(val, bytes):
            val = list(iterbytes(val))

        slen = len(val)

        if self.pad:
            pad = b'\0\0' * (slen % 2)
        else:
            pad = b''

        return struct.pack('>' + 'H' * slen, *val) + pad, slen, None 
Example #12
Source File: conversion.py    From DRTTView with MIT License 5 votes vote down vote up
def hex_to_byte_list(data):
    """! @brief Convert string of hex bytes to list of integers"""
    return list(six.iterbytes(binascii.unhexlify(data))) 
Example #13
Source File: conversion.py    From DMCUProg with MIT License 5 votes vote down vote up
def hex_to_byte_list(data):
    """! @brief Convert string of hex bytes to list of integers"""
    return list(six.iterbytes(binascii.unhexlify(data))) 
Example #14
Source File: des.py    From komodo-wakatime with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def str_to_key56(key_str):

    if not type(key_str) == six.binary_type:
        # TODO rsanders high - figure out how to make this not necessary
        key_str = key_str.encode('ascii')

    if len(key_str) < 7:
        key_str = key_str + b'\000\000\000\000\000\000\000'[:(7 - len(key_str))]
    key_56 = []
    for i in six.iterbytes(key_str[:7]):
        key_56.append(i)

    return key_56 
Example #15
Source File: des_c.py    From komodo-wakatime with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def decrypt(self, str):
        # block - UChar[]
        block = []

        for i in six.iterbytes(str):
            block.append(i)

        # print block
        block = des_ecb_encrypt(block, self.KeySched, 0)

        res = b''
        for i in block:
            res = res + six.int2byte(i)

        return res 
Example #16
Source File: rq.py    From python-xlib with GNU Lesser General Public License v2.1 5 votes vote down vote up
def pack_value(self, val):
        """Convert 8-byte string into 16-byte list"""
        if isinstance(val, bytes):
            val = list(iterbytes(val))

        slen = len(val)

        if self.pad:
            pad = b'\0\0' * (slen % 2)
        else:
            pad = b''

        return struct.pack('>' + 'H' * slen, *val) + pad, slen, None 
Example #17
Source File: types.py    From PGPy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def bytes_to_int(b, order='big'):  # pragma: no cover
        """convert bytes to integer"""
        if six.PY2:
            # save the original type of b without having to copy any data
            _b = b.__class__()
            if order != 'little':
                b = reversed(b)

            if not isinstance(_b, bytearray):
                b = six.iterbytes(b)

            return sum(c << (i * 8) for i, c in enumerate(b))

        return int.from_bytes(b, order) 
Example #18
Source File: bm_pyflate.py    From scalene with Apache License 2.0 5 votes vote down vote up
def bwt_transform(L):
    # Semi-inefficient way to get the character counts
    if six.PY3:
        F = bytes(sorted(L))
    else:
        F = b''.join(sorted(L))
    base = []
    for i in range(256):
        base.append(F.find(six.int2byte(i)))

    pointers = [-1] * len(L)
    for i, symbol in enumerate(six.iterbytes(L)):
        pointers[base[symbol]] = i
        base[symbol] += 1
    return pointers 
Example #19
Source File: test_six.py    From six with MIT License 5 votes vote down vote up
def test_bytesiter():
    it = six.iterbytes(six.b("hi"))
    assert six.next(it) == ord("h")
    assert six.next(it) == ord("i")
    pytest.raises(StopIteration, six.next, it) 
Example #20
Source File: bm_pyflate.py    From scalene with Apache License 2.0 5 votes vote down vote up
def bwt_transform(L):
    # Semi-inefficient way to get the character counts
    if six.PY3:
        F = bytes(sorted(L))
    else:
        F = b''.join(sorted(L))
    base = []
    for i in range(256):
        base.append(F.find(six.int2byte(i)))

    pointers = [-1] * len(L)
    for i, symbol in enumerate(six.iterbytes(L)):
        pointers[base[symbol]] = i
        base[symbol] += 1
    return pointers 
Example #21
Source File: des_c.py    From python-ntlm3 with GNU Lesser General Public License v3.0 5 votes vote down vote up
def decrypt(self, str):
        # block - UChar[]
        block = []

        for i in six.iterbytes(str):
            block.append(i)

        # print block
        block = des_ecb_encrypt(block, self.KeySched, 0)

        res = b''
        for i in block:
            res = res + six.int2byte(i)

        return res 
Example #22
Source File: mutable_data.py    From pwnypack with MIT License 5 votes vote down vote up
def _pack_data(data):
    return ['__data:'] + [
        '\tdb ' + ','.join(hex(b) for b in six.iterbytes(datum)) + '  ; ' + repr(orig_datum)
        for datum, (_, orig_datum) in six.iteritems(data)
    ] 
Example #23
Source File: codec.py    From pwnypack with MIT License 5 votes vote down vote up
def xor(key, data):
    """
    Perform cyclical exclusive or operations on ``data``.

    The ``key`` can be a an integer *(0 <= key < 256)* or a byte sequence. If
    the key is smaller than the provided ``data``, the ``key`` will be
    repeated.

    Args:
        key(int or bytes): The key to xor ``data`` with.
        data(bytes): The data to perform the xor operation on.

    Returns:
        bytes: The result of the exclusive or operation.

    Examples:
        >>> from pwny import *
        >>> xor(5, b'ABCD')
        b'DGFA'
        >>> xor(5, b'DGFA')
        b'ABCD'
        >>> xor(b'pwny', b'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
        b'15-=51)19=%5=9!)!%=-%!9!)-'
        >>> xor(b'pwny', b'15-=51)19=%5=9!)!%=-%!9!)-')
        b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    """

    if type(key) is int:
        key = six.int2byte(key)
    key_len = len(key)

    return b''.join(
        six.int2byte(c ^ six.indexbytes(key, i % key_len))
        for i, c in enumerate(six.iterbytes(data))
    ) 
Example #24
Source File: codec.py    From pwnypack with MIT License 5 votes vote down vote up
def frequency_app(parser, cmd, args):  # pragma: no cover
    """
    perform frequency analysis on a value.
    """

    parser.add_argument('value', help='the value to analyse, read from stdin if omitted', nargs='?')
    args = parser.parse_args(args)
    data = frequency(six.iterbytes(pwnypack.main.binary_value_or_stdin(args.value)))
    return '\n'.join(
        '0x%02x (%c): %d' % (key, chr(key), value)
        if key >= 32 and chr(key) in string.printable else
        '0x%02x ---: %d' % (key, value)
        for key, value in data.items()
    ) 
Example #25
Source File: human_ui.py    From pycolab with Apache License 2.0 5 votes vote down vote up
def _display(self, screen, observations, score, elapsed):
    """Redraw the game board onto the screen, with elapsed time and score.

    Args:
      screen: the main, full-screen curses window.
      observations: a list of `rendering.Observation` objects containing
          subwindows of the current game board.
      score: the total return earned by the player, up until now.
      elapsed: a `datetime.timedelta` with the total time the player has spent
          playing this game.
    """
    screen.erase()  # Clear the screen

    # Display the game clock and the current score.
    screen.addstr(0, 2, _format_timedelta(elapsed), curses.color_pair(0))
    screen.addstr(0, 20, 'Score: {}'.format(score), curses.color_pair(0))

    # Display cropped observations side-by-side.
    leftmost_column = 0
    for observation in observations:
      # Display game board rows one-by-one.
      for row, board_line in enumerate(observation.board, start=1):
        screen.move(row, leftmost_column)  # Move to start of this board row.
        # Display game board characters one-by-one. We iterate over them as
        # integer ASCII codepoints for easiest compatibility with python2/3.
        for codepoint in six.iterbytes(board_line.tostring()):
          screen.addch(
              codepoint, curses.color_pair(self._colour_pair[codepoint]))

      # Advance the leftmost column for the next observation.
      leftmost_column += observation.board.shape[1] + 3

    # Redraw the game screen (but in the curses memory buffer only).
    screen.noutrefresh() 
Example #26
Source File: basic_ops.py    From D-VAE with MIT License 5 votes vote down vote up
def _generate_kernel_bin(self, k, ctx):
        gk = gpuarray.GpuKernel(k.code, k.name, k.params, context=ctx,
                                **k.flags)
        bin = gk._binary
        bcode = ','.join(hex(c) for c in iterbytes(bin))
        return ("""static const char %(bname)s[] = { %(bcode)s };""" %
                dict(bname=k.binvar, bcode=bcode)) 
Example #27
Source File: test_lib_tokens_vasco.py    From privacyidea with GNU Affero General Public License v3.0 5 votes vote down vote up
def _set_next_blob(data):
    """
    update ``data._obj.Blob``: Increment the contents by 1.
    i.e. "XXX...X" is replaced with "YYY...Y".
    """
    data._obj.Blob = b"".join(six.int2byte(x + 1) for x in six.iterbytes(data._obj.Blob)) 
Example #28
Source File: session.py    From pytest-localstack with MIT License 5 votes vote down vote up
def generate_container_name():
    """Generate a random name for a Localstack container."""
    valid_chars = set(string.ascii_letters)
    chars = []
    while len(chars) < 6:
        new_chars = [chr(c) for c in six.iterbytes(os.urandom(6 - len(chars)))]
        chars += [c for c in new_chars if c in valid_chars]
    return "pytest-localstack-" + "".join(chars) 
Example #29
Source File: des.py    From python-ntlm3 with GNU Lesser General Public License v3.0 5 votes vote down vote up
def str_to_key56(key_str):

    if not type(key_str) == six.binary_type:
        # TODO rsanders high - figure out how to make this not necessary
        key_str = key_str.encode('ascii')

    if len(key_str) < 7:
        key_str = key_str + b'\000\000\000\000\000\000\000'[:(7 - len(key_str))]
    key_56 = []
    for i in six.iterbytes(key_str[:7]):
        key_56.append(i)

    return key_56 
Example #30
Source File: mysrp.py    From autoflashgui with GNU General Public License v3.0 5 votes vote down vote up
def bytes_to_long(s):
    n = 0
    for b in six.iterbytes(s):
        n = (n << 8) | b
    return n