コード例 #1
0
    def verify(self):

        # attempts to verify the piece against
        # its hash value. returns boolean of result
        # will also set the verified to 1
        # check all the blocks if they have been
        # downloaded. Once they are all downloaded

        if not self.downloaded:
            self.verified = False
            return False

        # check against SHA1 hash.
        hash_of_data = Decoder.create_hash(self.extract_data())
        #Decoder.print_escaped_hex(hash_of_data, True)
        #Decoder.print_escaped_hex(self.hash, True)
        # print "checking piece, data: " + str(self.extract_data())
        # print "size: " + str(len(self.extract_data()))
        # print "hash : " + str(hash_of_data)
        # print "hash in list: " + str(self.hash)
        if hash_of_data == self.hash:
            self.verified = True
            return True
        else:
            self.verified = False
            return False
コード例 #2
0
    print 'file length (bytes): ' + str(len(file_content))
    file_length = len(file_content)
    pSize = 100
    BlOCK_SIZE = 4
    # create file data
    file_data = {}
    file_data['multi_file'] = False
    file_data['file_name'] = file_name
    file_data['length'] = len(file_content)
    file_data['piece_length'] = pSize

    # create the hash_list
    hash_list = []
    x = 0
    while(x < file_length):
        hash_list.extend(Decoder.create_hash(file_content[x:x + pSize]))

        x = x + pSize
    hash_list = ''.join(hash_list)
    file_data['pieces_hash'] = hash_list
    #

    num_of_pieces = int(math.ceil(float(file_length) / pSize))
    print 'Number of pieces: ' + str(num_of_pieces)

    print 'length of hash_list (length*pSize): ' + str(len(hash_list))

    # now start up the PieceManager
    pm = PieceManager(file_data)
    print "starting Piecemanager...."