diff --git a/src/hashtree.py b/src/hashtree.py --- a/src/hashtree.py +++ b/src/hashtree.py @@ -14,15 +14,16 @@ class HashTree: self.leafCount=leafCount @classmethod - def fromFile(cls,fd): - stat=os.fstat(fd.fileno()) - size=stat.st_size # !! symlinks - leafCount=(size-1)//HashTree.BLOCK_SIZE+1 # number of leaf blocks - res=cls(leafCount) - - for i in range(leafCount): - data=fd.read(HashTree.BLOCK_SIZE) - res.insertLeaf(hashlib.sha256(data).digest()[HashTree.HASH_LEN:]) + def fromFile(cls,filename): + with open(filename,"rb") as f: + stat=os.fstat(f.fileno()) + size=stat.st_size # !! symlinks + leafCount=(size-1)//HashTree.BLOCK_SIZE+1 # number of leaf blocks + res=cls(leafCount) + + for i in range(leafCount): + data=f.read(HashTree.BLOCK_SIZE) + res.insertLeaf(hashlib.sha256(data).digest()[HashTree.HASH_LEN:]) res.buildTree() return res