Example #1
0
def test_simpletree():
    ''' test a 3 node merkel tree'''
    ''' UNDERSTOOD WHY THERE ARE SEVERAL DUPLICATES
        (WHEN NOT 2^n). ITS THE REASON THAT YOU GUESSED
        EACH NODE CREATED FROM HASHING ITSELF, SEES ITS 
        CHILDREN IN MULTIPLE OF TWO HENCE FOR EVERY SUCH
        NODE CREATED, WE WILL SEE THE NUMBER OF ITS CHILDREN
        AS 2

        TODO: HOPEFULLY THIS IS NOT GOING TO BE A PROBLEM 
        WHICH VERIFYING THE INTEGRITY OF FILES
        BETWEEN TWO PROCESSES
    '''
    # h1,h2 example hash of file data chunks
    h1 = hashlib.sha256(b'1'*1024).hexdigest()
    h2 = hashlib.sha256(b'2'*1024).hexdigest()
    h3 = hashlib.sha256(b'3'*1024).hexdigest()
    h4 = hashlib.sha256(b'4'*1024).hexdigest()
    h5 = hashlib.sha256(b'5'*1024).hexdigest()

    hs = [h1,h2,h3,h4,h5]
    hl = [merkeltree.MerkelNode(nodehash=i) for i in hs]

    tree = merkeltree.MerkelTree(hl)
    print("TREE HASH: ", tree.treehash())
    tree.traverse()
Example #2
0
def check_hash(FileHash):
    # compute the hash and assign EXPECTED_HASH with that value
    # obviously we are simulating here, so we will end up reading
    # the same file as client
    # But, it is supposed to work when the two files might be different
    # computers and they are communicating with each other to match
    # the consistency such that both have the same version of the file.
    tree = merkeltree.MerkelTree(getfile())
    EXPECTED_HASH = tree.treehash()
    return FileHash == EXPECTED_HASH
Example #3
0
def sender():
    global HOST, PORT
    # compute hash on the fly, return root of merkletree
    tree = merkeltree.MerkelTree(getfile())

    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.connect((HOST, PORT))
        # first try merkleroot
        s.sendall(bytes.fromhex(tree.treehash()))
        data = s.recv(1024)
        if data.decode('utf-8') == 'inconsistent':
            print("BAAB")
            # send the merkledict
            s.sendall(pickle.dumps(tree.merkledict, -1))
            #sender_merkledict(tree)
    print("Received ", repr(data))
Example #4
0
def test_leafnodes():
    ''' test the number of leaf nodes
        number of leaf node hashes = number of MerkelNodes in 
        hashlist 
    '''
    ''' THERE ARE DUPLICATES IN WHEN WE TRAVERSE 
        THE TREE

        DUPLICATES ARE BECAUSE OF THE FACT THAT SOME NODES HASH
        WITH THEMSELVES AS A RESULT THE TRAVERSAL WILL SEE ALL OF 
        THEM INCLUDING THE DUPLICATES
    '''
    tree = merkeltree.MerkelTree()
    print(len(tree.hashlist))
    f = 'hashlist.txt'
    with open(f, 'w') as mf:
        lst = [node.nodehash for node in tree.hashlist]
        for v in lst:
            mf.write(str(v) + '\n')
import hashlib
import sys
sys.path.insert(1, '/Users/aditya/Desktop/languages/python/merkeltree')
import merkeltree


def compare_return(st, ct):
    ''' compare the hash values for 
        servertree and clienttree,
        construct diff which stores 
        the relevant chunks which differ
        and return it back
    '''
    diff = {}
    st_hashes = st.merkledict.keys()
    ct_hashes = ct.merkledict.keys()
    assert len(st_hashes) == len(ct_hashes)
    for s,c in zip(st_hashes, ct_hashes):
        if s != c:
            if ct.isLeaf(c):
                diff[c] = ct.merkledict[c]
    return diff

if __name__ == '__main__':
    # servertree is ground truth
    servertree = merkeltree.MerkelTree('../serverfile.txt')
    clienttree = merkeltree.MerkelTree('../clientfile.txt')
    diff_chunks = compare_return(servertree, clienttree)
    print(diff_chunks)