Exemple #1
0
def main():
    int1 = utility.hextoint(string1)
    int2 = utility.hextoint(string2)
    int_xor_result = utility.xor(int1, int2)
    hex_xor_result = utility.inttohex(int_xor_result)

    print hex_xor_result[2:-1]
Exemple #2
0
    def handle_fn_qdata(self, data, addr):
        print "Receive find node query"
        
        target_node_id = data["a"]["target"]
        rtable_index = utility.get_rtable_index(utility.xor(self.node_id, target_node_id))
        
        response_nodes = []
        for node in self.rtable[rtable_index]:
            if node[0] == target_node_id:
                response_nodes.append(node)
                break

        if len(response_nodes) == 0:
            response_nodes = self.get_k_closest_nodes(target_node_id)

        node_message = utility.encode_nodes(response_nodes)

        response = {}
        response["t"] = data["t"]
        response["y"] = "r"
        response["r"] = {}
        response["r"]["id"] = self.node_id
        response["r"]["nodes"] = node_message
        response = bencode(response)
        
        try:
            self.socket.sendto(response, addr)
        except:
            pass
Exemple #3
0
    def handle_find_nodes_query(self, data, address):
        target_node_id = data["a"]["target"]
        r_table_index = get_routing_table_index(
            xor(self.node_id, target_node_id))

        response_nodes = []
        for node in self.routing_table[r_table_index]:
            if node[0] == target_node_id:
                response_nodes.append(node)
                break

        if len(response_nodes) == 0:
            response_nodes = self.get_k_closest_nodes(target_node_id)

        node_message = encode_nodes(response_nodes)

        response = {
            "t": data["t"],
            "y": "r",
            "r": {
                "id": self.node_id,
                "nodes": node_message
            }
        }

        self._send_message(response, address)

        if self._on_find_nodes is not None:
            self._on_find_nodes()
Exemple #4
0
    def handle_fn_qdata(self, data, addr):
        print "Receive find node query"

        target_node_id = data["a"]["target"]
        rtable_index = utility.get_rtable_index(
            utility.xor(self.node_id, target_node_id))

        response_nodes = []
        for node in self.rtable[rtable_index]:
            if node[0] == target_node_id:
                response_nodes.append(node)
                break

        if len(response_nodes) == 0:
            response_nodes = self.get_k_closest_nodes(target_node_id)

        node_message = utility.encode_nodes(response_nodes)

        response = {}
        response["t"] = data["t"]
        response["y"] = "r"
        response["r"] = {}
        response["r"]["id"] = self.node_id
        response["r"]["nodes"] = node_message
        response = bencode(response)

        try:
            self.socket.sendto(response, addr)
        except:
            pass
Exemple #5
0
 def add_nodes_to_rtable(self, nodes):
     if self.rtable_mutex.acquire():
         for node in nodes:
             rtable_index = utility.get_rtable_index(utility.xor(node[0], self.node_id))
             if len(self.rtable[rtable_index]) < NEW_K:
                 self.rtable[rtable_index].append(node)
             else:
                 if random.randint(0, 1):
                     index = random.randint(0, NEW_K - 1)
                     self.rtable[rtable_index][index] = node
                 else:
                     self.find_node(node)
         self.rtable_mutex.release()
Exemple #6
0
 def add_nodes_to_routing_table(self, nodes):
     with self.routing_table_lock:
         for node in nodes:
             r_table_index = get_routing_table_index(
                 xor(node[0], self.node_id))
             if len(self.routing_table[r_table_index]) < DHTProtocol.NEW_K:
                 self.routing_table[r_table_index].append(node)
             else:
                 if random.randint(0, 1):
                     index = random.randint(0, DHTProtocol.NEW_K - 1)
                     self.routing_table[r_table_index][index] = node
                 else:
                     self.find_node(node)
Exemple #7
0
 def add_nodes_to_rtable(self, nodes):
     if self.rtable_mutex.acquire():
         for node in nodes:
             rtable_index = utility.get_rtable_index(
                 utility.xor(node[0], self.node_id))
             if len(self.rtable[rtable_index]) < NEW_K:
                 self.rtable[rtable_index].append(node)
             else:
                 if random.randint(0, 1):
                     index = random.randint(0, NEW_K - 1)
                     self.rtable[rtable_index][index] = node
                 else:
                     self.find_node(node)
         self.rtable_mutex.release()
Exemple #8
0
 def get_k_closest_nodes(self, id):
     rtable_index = utility.get_rtable_index(utility.xor(self.node_id, id))
     
     k_closest_nodes = []
     
     index = rtable_index
     while index >= 0 and len(k_closest_nodes) < K:
         for node in self.rtable[index]:
             if len(k_closest_nodes) < K:
                 k_closest_nodes.append(node)
             else:
                 break
         index -= 1
     index = rtable_index + 1
     while index < 160  and len(k_closest_nodes) < K:
         for node in self.rtable[index]:
             if len(k_closest_nodes) < K:
                 k_closest_nodes.append(node)
             else:
                 break
         index += 1
     
     return k_closest_nodes
Exemple #9
0
    def get_k_closest_nodes(self, id):
        rtable_index = utility.get_rtable_index(utility.xor(self.node_id, id))

        k_closest_nodes = []

        index = rtable_index
        while index >= 0 and len(k_closest_nodes) < K:
            for node in self.rtable[index]:
                if len(k_closest_nodes) < K:
                    k_closest_nodes.append(node)
                else:
                    break
            index -= 1
        index = rtable_index + 1
        while index < 160 and len(k_closest_nodes) < K:
            for node in self.rtable[index]:
                if len(k_closest_nodes) < K:
                    k_closest_nodes.append(node)
                else:
                    break
            index += 1

        return k_closest_nodes
Exemple #10
0
    def get_k_closest_nodes(self, node_id):
        r_table_index = get_routing_table_index(xor(self.node_id, node_id))

        k_closest_nodes = []

        index = r_table_index
        while index >= 0 and len(k_closest_nodes) < DHTProtocol.K:
            for node in self.routing_table[index]:
                if len(k_closest_nodes) < DHTProtocol.K:
                    k_closest_nodes.append(node)
                else:
                    break
            index -= 1

        index = r_table_index + 1
        while index < 160 and len(k_closest_nodes) < DHTProtocol.K:
            for node in self.routing_table[index]:
                if len(k_closest_nodes) < DHTProtocol.K:
                    k_closest_nodes.append(node)
                else:
                    break
            index += 1

        return k_closest_nodes
Exemple #11
0
def encrypt(plaintext, key):
    key = repeat_key(key, len(plaintext))
    return xor(str2hex(plaintext), str2hex(key))