使用Python的hashdeep检测目录中所有文件的hash值

Posted by Tesla9527 on May 10, 2017

使用的是https://gist.github.com/techtonik/5175896,下面我只是增加了一下对比

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import os
import os.path as osp
import hashlib


def filehash(filepath):
    blocksize = 64 * 1024
    sha = hashlib.sha256()
    with open(filepath, 'rb') as fp:
        while True:
            data = fp.read(blocksize)
            if not data:
                break
            sha.update(data)
    return sha.hexdigest()


def get_recursive_hash(dir_root):
    print('size,sha256,filename')
    hash_list = []
    for root, dirs, files in os.walk(dir_root):
        for fpath in [osp.join(root, f) for f in files]:
            size = osp.getsize(fpath)
            sha = filehash(fpath)
            name = osp.relpath(fpath, dir_root)
            hash_list.append({'size': size, 'sha': sha, 'name':name})
	print hash_list
    return hash_list


def compare_recursive_hash(dir1, dir2):
    hash_list1 = get_recursive_hash(dir1)
    hash_list2 = get_recursive_hash(dir2)
    if hash_list1 == hash_list2:
        print('hash same')
        return True
    else:
        print('hash not same')
        return False


compare_recursive_hash('E:\\dir1\\', 'E:\\dir2\\')