-
Notifications
You must be signed in to change notification settings - Fork 0
/
oshash
executable file
·42 lines (33 loc) · 998 Bytes
/
oshash
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
#!/usr/bin/env python3
import sys
import os
import struct
def usage():
print(f"Usage: {sys.argv[0]} <file>")
sys.exit(1)
def compute_hash(filename):
try:
with open(filename, 'rb') as f:
filesize = os.path.getsize(filename)
hashval = filesize
if filesize < 65536 * 2:
print("Error: File is too small")
return
for x in range(65536//8):
buffer = f.read(8)
(chunk,) = struct.unpack('<Q', buffer)
hashval += chunk
f.seek(-65536, 2)
for x in range(65536//8):
buffer = f.read(8)
(chunk,) = struct.unpack('<Q', buffer)
hashval += chunk
return format(hashval, '016x')
except IOError:
usage()
if __name__ == "__main__":
if len(sys.argv) != 2:
usage()
result = compute_hash(sys.argv[1])
if result is not None:
print(result[4:])