Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Python - - recursive hash of directory tree files in hashdeep format
Build recursive hash of files in directory tree in hashdeep format.
Hashdeep format description: differences from original hashdeep:
- if called without arguments, automatically starts to build
recursive hash starting from the current directory
(original hashdeep waits for the output from stdin)
- uses only sha256 (original uses md5 and sha256)
- uses relative paths only (original works with absolute) output example:
%%%% HASHDEEP-1.0
%%%% size,sha256,filename
## $
__author__ = 'anatoly techtonik <>'
__license__ = 'Public Domain'
__version__ = '1.0'
import os
import os.path as osp
import hashlib
# --- helpers ---
def write(text):
""" helper for writing output, as a single point for replacement """
def filehash(filepath):
blocksize = 64*1024
sha = hashlib.sha256()
with open(filepath, 'rb') as fp:
while True:
data =
if not data:
return sha.hexdigest()
# --- /helpers ---
%%%% HASHDEEP-1.0
%%%% size,sha256,filename
## $
ROOT = '.'
for root, dirs, files in os.walk(ROOT):
for fpath in [osp.join(root, f) for f in files]:
size = osp.getsize(fpath)
sha = filehash(fpath)
name = osp.relpath(fpath, ROOT)
write('%s,%s,%s' % (size, sha, name))
#for ignored in ['.hg', '.svn', 'git']:
# if ignored in dirs:
# dirs.remove(ignored)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment