pydc_client.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:recobot 作者: h4ck3rk3y 项目源码 文件源码
def tth_generate(self,file): # Generates the Tiger Tree Hash (Merkle Tree) for a given file
        # During the hashing of the raw data from the file, the leaf hash function uses the marker 0x00 prepended to the data before tiger hashing it. Similarly, the marker 0x01 is prepended in case of internal nodes.
        blocksize = 1024 # Standard Block Size
        filesize = os.path.getsize(file) # Get filesize for subsequent calculations
        if filesize==0: return [[self.tiger_hash(chr(0))]] # On failure of getsize or if file is empty, return hash for empty file.
        try: handle = open(file,"rb") # Open file for reading in binary mode
        except: return None # If it doesnt exist or is inaccessible, dont bother.
        level = [[]] # List of Levels, Level 0 Empty
        for i in range(int(math.ceil(float(filesize)/blocksize))):
            block = handle.read(blocksize) # Read part of the file
            level[0].append(self.tiger_hash(chr(0)+block)) # Hash that part only, and put it in Level 0
        handle.close() # Close file
        current = 0 # Starting from level 0
        while len(level[0])>1: # If whole file hasent been hashed yet
            level.append([]) # Create new level
            for i in range(len(level[current])/2): # For all hash pairs
                level[1].append( self.tiger_hash(chr(1)+level[0][2*i]+level[0][2*i+1]) ) # Combine two hashes to get a binary tree like structure.
            if len(level[0])%2==1: level[1].append(level[0][-1]) # If last item cannot be paired, promote it untouched.
            del level[0] # Discard lower level hashes, as they are no longer necessary
        return base64.b32encode(level[0][0])[:-1] # Result will be 40 characters; discarding the trailing '=' makes it 39
评论列表


问题


面经


文章

微信
公众号

扫码关注公众号