Exemplo n.º 1
0
 def bytes2op(b, mode):
     type = chr(b[0])
     length = b[1]
     index = utils.bytes2int(b[2:length+2])
     if type == 'i':
         data = b[length+2:].decode()
     else:
         data = utils.bytes2int(b[length+2:])
     return operation(type, index, data)
Exemplo n.º 2
0
def chopstring(message, key, n, funcref):
    """
    Splits 'message' into chops that are at most as long as n,
    converts these into integers, and calls funcref(integer, key, n)
    for each chop.

    Used by 'encrypt' and 'sign'.
    """
    msglen = len(message)
    mbits = msglen * 8
    nbits = int(math.floor(math.log(n, 2)))
    nbytes = nbits / 8
    blocks = msglen / nbytes

    if msglen % nbytes > 0:
        blocks += 1

    cypher = []

    for bindex in range(blocks):
        offset = bindex * nbytes
        block = message[offset:offset + nbytes]
        value = utils.bytes2int(block)
        cypher.append(funcref(value, key, n))

    return picklechops(cypher)
Exemplo n.º 3
0
def chopstring(message, key, n, funcref):
    """
    Splits 'message' into chops that are at most as long as n,
    converts these into integers, and calls funcref(integer, key, n)
    for each chop.

    Used by 'encrypt' and 'sign'.
    """
    msglen = len(message)
    mbits = msglen * 8
    nbits = int(math.floor(math.log(n, 2)))
    nbytes = nbits / 8
    blocks = msglen / nbytes

    if msglen % nbytes > 0:
        blocks += 1

    cypher = []

    for bindex in range(blocks):
        offset = bindex * nbytes
        block = message[offset:offset+nbytes]
        value = utils.bytes2int(block)
        cypher.append(funcref(value, key, n))

    return picklechops(cypher)
Exemplo n.º 4
0
def flags(flag_str):
    ""
    res = ''
    res = res + '%s\n\n' % utils.str2hex(flag_str)
    flags = utils.bytes2int(flag_str[0:2])
    res = res + '%s                   # flags\n' % (utils.int2hex_str(flags))
    res = res + 'Binary:\nlayout 87654321 87654321\n'
    res = res + '       %s %s\n' % (utils.byte2bin_str(flag_str[1]), utils.byte2bin_str(flag_str[0]))

    flags2 = utils.bytes2int(flag_str[2:4])
    res = res + '%s                   # more flags ???\n' % (utils.int2hex_str(flags2))
    res = res + 'Binary:\nlayout 87654321 87654321\n'
    res = res + '       %s %s\n' % (utils.byte2bin_str(flag_str[3]), utils.byte2bin_str(flag_str[2]))

    #res = res + '%s                   # delimiter ???\n' % m_hex[(cur + 2) * 2: (cur + 4) * 2]

    return res
Exemplo n.º 5
0
def item(item_str):
    ""
    item = {}
    res = ''
    item['len1'] = utils.bytes2int(item_str[0:2])
    item['len2'] = utils.bytes2int(item_str[2:4])
    item['offset'] = utils.bytes2int(item_str[4:6])

    res = res + '%s\n\nlength (two times), offset, delimiter\n' % (utils.str2hex(item_str))

    res = res + '%s decimal: %3d    # length 1\n' % (utils.int2hex_str(item['len1']), item['len1'])
    res = res + '%s decimal: %3d    # length 2\n' % (utils.int2hex_str(item['len2']), item['len2'])
    res = res + '%s decimal: %3d    # offset\n' % (utils.int2hex_str(item['offset']), item['offset'])
    res = res + '%s                   # delimiter (two zeros)\n\n' % utils.str2hex(item_str[-2:])
    item['string'] = res

    return item
Exemplo n.º 6
0
def read_random_int(nbits):
    """
    Reads a random integer of approximately nbits bits rounded up
    to whole bytes.
    """
    nbytes = ceil(nbits/8.)
    randomdata = os.urandom(nbytes)
    return utils.bytes2int(randomdata)
Exemplo n.º 7
0
def read_random_int(nbits):
    """
    Reads a random integer of approximately nbits bits rounded up
    to whole bytes.
    """
    nbytes = ceil(nbits / 8.)
    randomdata = os.urandom(nbytes)
    return utils.bytes2int(randomdata)
Exemplo n.º 8
0
def flags(flag_str):
    ""
    res = ''
    res = res + '%s\n\n' % utils.str2hex(flag_str)
    flags = utils.bytes2int(flag_str[0:2])
    res = res + '%s                   # flags\n' % (utils.int2hex_str(flags))
    res = res + 'Binary:\nlayout 87654321 87654321\n'
    res = res + '       %s %s\n' % (utils.byte2bin_str(
        flag_str[1]), utils.byte2bin_str(flag_str[0]))

    flags2 = utils.bytes2int(flag_str[2:4])
    res = res + '%s                   # more flags ???\n' % (
        utils.int2hex_str(flags2))
    res = res + 'Binary:\nlayout 87654321 87654321\n'
    res = res + '       %s %s\n' % (utils.byte2bin_str(
        flag_str[3]), utils.byte2bin_str(flag_str[2]))

    #res = res + '%s                   # delimiter ???\n' % m_hex[(cur + 2) * 2: (cur + 4) * 2]

    return res
Exemplo n.º 9
0
 def bytes2oplist(self, b):
     pos = 0
     length = len(b)
     oplist = []
     while pos < length:
         ll = b[pos]
         pos += 1
         op_len = utils.bytes2int(b[pos:pos+ll])
         pos += ll
         oplist.append(operation.bytes2op(b[pos : pos + op_len], self.mode))
         pos += op_len
     return oplist
Exemplo n.º 10
0
def item(item_str):
    ""
    item = {}
    res = ''
    item['len1'] = utils.bytes2int(item_str[0:2])
    item['len2'] = utils.bytes2int(item_str[2:4])
    item['offset'] = utils.bytes2int(item_str[4:6])

    res = res + '%s\n\nlength (two times), offset, delimiter\n' % (
        utils.str2hex(item_str))

    res = res + '%s decimal: %3d    # length 1\n' % (utils.int2hex_str(
        item['len1']), item['len1'])
    res = res + '%s decimal: %3d    # length 2\n' % (utils.int2hex_str(
        item['len2']), item['len2'])
    res = res + '%s decimal: %3d    # offset\n' % (utils.int2hex_str(
        item['offset']), item['offset'])
    res = res + '%s                   # delimiter (two zeros)\n\n' % utils.str2hex(
        item_str[-2:])
    item['string'] = res

    return item
Exemplo n.º 11
0
    def handle_stream(self, stream, address):
        magic_number = yield stream.read_bytes(2)
        # gen_log.info('receive magic number success')
        if magic_number != '\xab\xcd':
            return
        interface_len = yield stream.read_bytes(4)
        interface_len = bytes2int(interface_len)
        gen_log.info('interface len: %d', interface_len)
        interface = yield stream.read_bytes(interface_len)
        gen_log.info('get interface: [%s]', interface)
        method_len = yield stream.read_bytes(4)
        method_len = bytes2int(method_len)
        method = yield stream.read_bytes(method_len)
        pts_len = yield stream.read_bytes(4)
        pts_len = bytes2int(pts_len)
        pts = yield stream.read_bytes(pts_len)
        channel_key = (interface, method, pts)
        gen_log.info('get method: [%s]', method)
        gen_log.info('get pts: [%s]', pts)
        if channel_key in self.dubbo_channel_map:
            dubbo_client = self.dubbo_channel_map[channel_key]
        else:
            dubbo_client = DubboClient('localhost', 20880)
            self.dubbo_channel_map[channel_key] = dubbo_client

        def make_request(Id):
            ar = ActRequest()
            ar.interface = interface
            ar.method = method
            ar.parameter_types_string = pts
            ar.Id = Id
            return ar

        def make_dubbo_request(act_request):
            dr = DubboRequest()
            dr.Id = act_request.Id
            # dr.Id = random.randint(1, 2100000000)
            dr.interface_name = act_request.interface
            dr.method_name = act_request.method
            # dr.dubbo_version = ''
            # dr.version = ''
            dr.args = act_request.parameter
            return dr

        def make_act_response(dubbo_response):
            aresp = ActResponse()
            aresp.Id = dubbo_response.Id
            aresp.result = dubbo_response.result
            return aresp

        while True:
            try:
                Id = yield stream.read_bytes(4)
                request = make_request(bytes2int(Id))
                p_len = yield stream.read_bytes(4)
                request.parameter = yield stream.read_bytes(bytes2int(p_len))
                gen_log.info("get request %d %s", request.Id, request.parameter)

                def callback(dubbo_response):
                    # gen_log.info("get dubbo_resp %d [%s] \nstream write act resp", dubbo_response.Id, dubbo_response.result)
                    stream.write(make_act_response(dubbo_response).encode_body())

                dubbo_client.fetch(make_dubbo_request(request), callback=callback)
            except StreamClosedError:
                break
Exemplo n.º 12
0
 def _on_rlen(self, data):
     self.stream.read_bytes(bytes2int(data), self._on_res_body)
Exemplo n.º 13
0
 def _on_id(self, data):
     resp = ActResponse()
     resp.Id = bytes2int(data)
     self.prof[resp.Id].append(time.time())
     self.pepv_act_resp = resp
     self.stream.read_bytes(4, self._on_rlen)