-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSha512.py
111 lines (75 loc) · 3.4 KB
/
Sha512.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import os
from datetime import datetime
import subprocess
# Example: teracopy
#; SHA-512 checksums generated by TeraCopy
#; teracopy.com
#
#83E8D27AC05EC9664767DFB41B9649DFC8971B3867C40E01E77EC35A8E324D66922E12AEF5ADF5068380F26D68FEEDB67626FBD3C20B8D15F4689EC9433C494C *HC.Tales.of.the.Gun.15of16.Guns.of.the.Commandos.x264.AC3.MVGroup.org.mkv
class Sha512:
def __init__(self, dirname, filename = None):
self.dirname = dirname
self.header = []
self.footer = []
self.file_list = []
self.last_verified = None
if filename is None:
self.header.append("; SHA-512 checksums generated by zchecksum\n")
self.header.append("; zchecksum.com\n")
self.header.append(";\n")
# self.footer.append(";\n")
else:
with open(self.dirname + filename, 'r', encoding="utf-8") as file:
lines = file.readlines()
self.last_verified = datetime.fromtimestamp(os.path.getmtime(self.dirname + filename))
for line in lines:
line = line.strip()
if line.startswith(';'):
if len(self.file_list) == 0:
self.header.append(line)
else:
# if not line.startswith("; Last verified:"):
self.footer.append(line)
# else:
# l = line.split("; Last verified: ")
# self.last_verified = datetime.strptime(l[1], "%m/%d/%Y %H:%M:%S")
else:
index = line.find(" *")
if index == -1:
raise Exception(f'Error parsing SHA512 file, line: {line}')
self.file_list.append(dict(checksum=line[:index], filename=line[index+2:]))
def __eq__(self, other):
if isinstance(other, Sha512):
return self.file_list == other.file_list
return NotImplemented
def addFileAndChecksum(self, filename, checksum):
self.file_list.append(dict(checksum=checksum, filename=filename))
def findChecksum(self, filename):
for f in self.file_list:
if f['filename'] == filename:
return f['checksum']
return None
def print(self):
print("\nPrinting SHA512\n")
count = 0
for line in self.header:
print("Line{}: {}".format(count, line.strip("\n")))
count += 1
for f in self.file_list:
print(f'Line{count}: {f["checksum"]} *{f["filename"]}')
count += 1
for line in self.footer:
print("Line{}: {}".format(count, line.strip("\n")))
count += 1
if self.last_verified:
print(f'Last verified: {self.last_verified.strftime("%m/%d/%Y %H:%M:%S")}')
def update_modified(self):
os.utime(self.dirname + "zchecksum.sha512", None)
def writeFile(self):
# self.last_verified = datetime.now()
with open(self.dirname + "zchecksum.sha512", 'w', encoding="utf-8") as sha_file:
sha_file.writelines(self.header)
for f in self.file_list:
sha_file.write(f'{f["checksum"]} *{f["filename"]}\n')
sha_file.writelines(self.footer)
# sha_file.write(f'; Last verified: {self.last_verified.strftime("%m/%d/%Y %H:%M:%S")}')