-
Notifications
You must be signed in to change notification settings - Fork 1
/
hash.py
executable file
·50 lines (34 loc) · 1.36 KB
/
hash.py
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
43
44
45
46
47
48
49
# code stolen from totem plugin
# should add here correct licence and credits
import struct
import os
import gio
SIZE_ERROR = -1
SEEK_ERROR = -2
def hashFile(name):
""" FIXME Need to handle exceptions !! """
longlongformat = 'q' # long long
bytesize = struct.calcsize(longlongformat)
fp = gio.File(name)
filesize = fp.query_info('standard::size', 0).get_attribute_uint64('standard::size')
hash = filesize
if filesize < 65536 * 2:
return SIZE_ERROR, 0
data = fp.read()
if data.can_seek() != True:
return SEEK_ERROR, 0
for x in range(65536/bytesize):
buffer = data.read(bytesize)
(l_value,)= struct.unpack(longlongformat, buffer)
hash += l_value
hash = hash & 0xFFFFFFFFFFFFFFFF #to remain as 64bit number
if data.seek(max(0,filesize-65536),1) != True:
return SEEK_ERROR, 0
for x in range(65536/bytesize):
buffer = data.read(bytesize)
(l_value,)= struct.unpack(longlongformat, buffer)
hash += l_value
hash = hash & 0xFFFFFFFFFFFFFFFF
data.close()
returnedhash = "%016x" % hash
return returnedhash, filesize