Add simple library for computing digests of files.
[asure.git] / hashing.py
blob4f4dd3cc0575e9fe270b8dd0e50f4fdea3c3745a
1 #! /usr/bin/env python
3 import md5
4 import sys
5 import os
6 import platform
7 import base64
9 # Fast hashing of files.
10 # Initial implementation isn't intended to be fast, just work.
12 # TODO:
13 # - Use mmap() in cases where that would work. Not sure this would
14 # really be any faster.
15 # - Optionally bind to faster MD5 library.
17 # When running on Linux, and root, avoid updating the atime of the
18 # file.
19 extra_flags = 0
20 if platform.system() == 'Linux' and os.getuid() == 0:
21 extra_flags = 01000000
23 def hashof(path):
24 ufd = os.open(path, os.O_RDONLY | extra_flags)
25 fd = os.fdopen(ufd, 'rb', 0)
26 # fd = open(path, 'rb')
27 hash = md5.new()
28 while True:
29 buf = fd.read(32768)
30 if buf == '':
31 break
32 hash.update(buf)
33 fd.close()
34 return hash.digest()
36 if __name__ == '__main__':
37 for name in sys.argv[1:]:
38 print ("%s %s" % (base64.b16encode(hashof(name)), name))