forked from astrosonic/bazooka-share
-
Notifications
You must be signed in to change notification settings - Fork 0
/
splitter.py
278 lines (267 loc) · 12.3 KB
/
splitter.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import hashlib, sqlite3, os, time
from colorama import init, Fore, Style
init()
def nogenten(numvalue):
strvalue=""
if numvalue<10:
strvalue="0"+str(numvalue)
else:
strvalue=str(numvalue)
return strvalue
def nogenhun(numvalue):
strvalue=""
if numvalue<10:
strvalue="00"+str(numvalue)
elif numvalue>=10 and numvalue<100:
strvalue="0"+str(numvalue)
else:
strvalue=str(numvalue)
return strvalue
def nogenthd(numvalue):
strvalue=""
if numvalue<10:
strvalue="000"+str(numvalue)
elif numvalue>=10 and numvalue<100:
strvalue="00"+str(numvalue)
elif numvalue>=100 and numvalue<1000:
strvalue="0"+str(numvalue)
else:
strvalue=str(numvalue)
return strvalue
def allcbyte(bytename,partcunt):
buffsize=len(bytename)
sizelist,poselist=[],[]
if buffsize%partcunt==0:
genptsiz=buffsize//partcunt
for i in range(0,partcunt):
sizelist.append(genptsiz)
else:
genptsiz=buffsize//partcunt
endptsiz=buffsize%partcunt
sizelist.append(genptsiz+endptsiz)
for i in range(0,partcunt-1):
sizelist.append(genptsiz)
poselist.append(0)
for i in range(1,partcunt+1):
poselist.append(sum(sizelist[0:i]))
return poselist
def spltsize(filename,partsize):
actifile=open(filename,"rb")
actibuff=actifile.read()
actifile.close()
buffsize=len(actibuff)
hashlist={}
count=0
for i in range(0,buffsize,partsize):
blocname=filename+"-"+str(count)
blocbuff=actibuff[i:i+partsize]
hashlist[blocname]=hashlib.sha512(blocbuff).hexdigest()
blocfile=open(blocname,"wb")
blocfile.write(blocbuff)
blocfile.close()
count+=1
print("The file has been splitted into blocks of "+str(partsize)+" bytes")
for i in hashlist.keys():
print(i+"\t"+hashlist[i])
def makeldgr(filename,hashlist,sizelist,cuntlist):
ldgrname=filename+".ldg"
actifile=open(ldgrname,"wb")
actifile.close()
ldgrbase=sqlite3.connect(ldgrname)
ldgrbase.execute("create table ldgrbase (partnumb text primary key not null, partname text not null, partsize int not null, sha512dg text not null);")
for i in hashlist.keys():
querystr="insert into ldgrbase (partnumb, partname, partsize, sha512dg) values ('"+str(cuntlist[i])+"', '"+str(i)+"', "+str(sizelist[i])+", '"+str(hashlist[i])+"')"
ldgrbase.execute(querystr)
ldgrbase.commit()
ldgrbase.close()
def readldgr(ldgrname):
ldgrlist=[]
try:
ldgrbase=sqlite3.connect(ldgrname)
dbcursor=ldgrbase.execute("select * from ldgrbase")
hashlist,sizelist,cuntlist={},{},{}
for row in dbcursor:
cuntlist[row[1]]=row[0]
sizelist[row[1]]=row[2]
hashlist[row[1]]=row[3]
ldgrbase.close()
ldgrlist=[cuntlist,sizelist,hashlist]
except:
ldgrlist=None
return ldgrlist
def displdgr(filename):
ldgrname=filename+".ldg"
ldgrbase=sqlite3.connect(ldgrname)
dbcursor=ldgrbase.execute("select * from ldgrbase")
hashlist,sizelist,cuntlist={},{},{}
print(Fore.CYAN+"[LEDGER CONTENTS]"+Fore.RESET)
for row in dbcursor:
print("Part count : "+str(row[0])+"\n"+\
"Part name : "+str(row[1])+"\n"+\
"Part size : "+str(row[2])+"\n"+\
"SHA512 hash : "+str(row[3]))
ldgrbase.close()
def pthealth(cuntlist,sizelist,hashlist):
failcunt,passcunt=0,0
chekcunt,misscunt=0,0
startsec=time.time()
for i in cuntlist.keys():
chekcunt+=1
try:
actifile=open(i,"rb")
actibuff=actifile.read()
actifile.close()
preshash=hashlib.sha512(actibuff).hexdigest()
if preshash==hashlist[i]:
passcunt+=1
else:
failcunt+=1
except FileNotFoundError:
misscunt+=1
endinsec=time.time()
totatime=str(endinsec-startsec).split(".")[0]+"."+str(endinsec-startsec).split(".")[1][0:2]
joinable=False
if chekcunt==passcunt:
joinable=True
print(Fore.CYAN+"[BLOCK INTEGRITY CHECK]"+"\n"+Fore.RESET+\
"Total checks : "+str(chekcunt)+"\n"+\
"Files with wrong hash : "+str(failcunt)+"\n"+\
"Files with match hash : "+str(passcunt)+"\n"+\
"Files missing : "+str(misscunt)+"\n"+\
"Time taken for check : "+str(totatime)+" seconds\n"+\
"Integrity result : "+str(joinable)+"\n")
return joinable
def joincunt(filename):
ldgrname=filename+".ldg"
ldgrlist=readldgr(ldgrname)
if ldgrlist==None:
print(Fore.RED+"[ERROR OCCURRED]"+Fore.RESET+"\n"+\
"The ledger could not be read properly!\n"+\
"Make sure you have \n"+\
"- Privileges to access the ledger file \n"+\
"- the ledger file present in the directory \n"+\
"- been pointing towards to right location")
else:
cuntlist=ldgrlist[0]
sizelist=ldgrlist[1]
hashlist=ldgrlist[2]
totlpart=int(list(cuntlist.values())[-1])
totlprog=0
if (pthealth(cuntlist,sizelist,hashlist)):
print(Fore.CYAN+"[STARTING JOIN OPERATION]"+Fore.RESET)
startsec=time.time()
actibuff=b""
for i in cuntlist.keys():
blocfile=open(i,"rb")
blocbuff=blocfile.read()
blocfile.close()
os.system("rm "+i)
actibuff+=blocbuff
totlprog=totlprog+100/totlpart
print("Joined "+i+" to parent file "+Style.DIM+"("+str(totlprog)[0:4]+"% completed)"+Style.RESET_ALL)
actifile=open(filename,"wb")
actifile.write(actibuff)
actifile.close()
os.system("rm "+ldgrname)
endinsec=time.time()
totatime=str(endinsec-startsec).split(".")[0]+"."+str(endinsec-startsec).split(".")[1][0:2]
print("\n"+Fore.CYAN+"[JOIN OPERATION COMPLETED]"+Fore.RESET+"\n"+\
"Time taken : "+totatime+" seconds"+Fore.RESET)
else:
print("\n"+Fore.RED+"[ERROR OCCURRED]"+Fore.RESET+"\n"+\
"Join procedure could not be initiated!\n"+\
"Some parts are missing or corrupted - Download them again")
def spltcunt(filename,partcunt):
try:
actifile=open(filename,"rb")
actibuff=actifile.read()
buffsize=len(actibuff)
actifile.close()
if (partcunt>=10 and partcunt<10000):
if (partcunt>buffsize):
print(Fore.RED+"[ERROR OCCURRED]"+Fore.RESET+"\n"+\
"Splitting operation could not be initiated!\n"+\
"The number of parts is greater than the byte size of your file")
else:
poselist=allcbyte(actibuff,partcunt)
hashlist,sizelist,cuntlist={},{},{}
totlprog=0
print(Fore.CYAN+"[PROTEXON SPLITTER by t0xic0der]"+Fore.RESET+"\n"+\
"File name : "+filename+"\n"+\
"File size : "+str(buffsize)+" bytes\n"+\
"Part count : "+str(partcunt)+" parts\n"+\
"Ledger name : "+filename+".ldg\n")
if partcunt>=10 and partcunt<=100:
print(Fore.CYAN+"[STARTING SPLIT OPERATION]"+Fore.RESET)
startsec=time.time()
for i in range(1,partcunt+1):
blocname=filename+"."+nogenten(i)
blocbuff=actibuff[poselist[i-1]:poselist[i]]
hashlist[blocname]=hashlib.sha512(blocbuff).hexdigest()
sizelist[blocname]=len(blocbuff)
cuntlist[blocname]=nogenten(i)
blocfile=open(blocname,"wb")
blocfile.write(blocbuff)
blocfile.close()
totlprog=totlprog+(100/partcunt)
print(str(cuntlist[blocname])+"\t"+str(blocname)+ " created!\t"+Style.DIM+str(sizelist[blocname])+" bytes\t"+Style.RESET_ALL+str(hashlist[blocname])+"\t"+Style.DIM+str(totlprog)[0:4]+"% completed"+Style.RESET_ALL)
makeldgr(filename,hashlist,sizelist,cuntlist)
endinsec=time.time()
totatime=str(endinsec-startsec).split(".")[0]+"."+str(endinsec-startsec).split(".")[1][0:2]
print("\n"+Fore.CYAN+"[SPLIT OPERATION COMPLETED]"+Fore.RESET+"\n"+\
"Parts created : "+str(partcunt)+" parts \n"+\
"Ledger created : "+str(filename)+".ldg \n"+\
"Time taken : "+str(totatime)+" seconds \n")
elif partcunt>=100 and partcunt<=1000:
print(Fore.CYAN+"[STARTING SPLIT OPERATION]"+Fore.RESET)
startsec=time.time()
for i in range(1,partcunt+1):
blocname=filename+"."+nogenhun(i)
blocbuff=actibuff[poselist[i-1]:poselist[i]]
hashlist[blocname]=hashlib.sha512(blocbuff).hexdigest()
sizelist[blocname]=len(blocbuff)
cuntlist[blocname]=nogenhun(i)
blocfile=open(blocname,"wb")
blocfile.write(blocbuff)
blocfile.close()
totlprog=totlprog+(100/partcunt)
print(str(cuntlist[blocname])+"\t"+str(blocname)+ " created!\t"+Style.DIM+str(sizelist[blocname])+" bytes\t"+Style.RESET_ALL+str(hashlist[blocname])+"\t"+Style.DIM+str(totlprog)[0:4]+"% completed"+Style.RESET_ALL)
makeldgr(filename,hashlist,sizelist,cuntlist)
endinsec=time.time()
totatime=str(endinsec-startsec).split(".")[0]+"."+str(endinsec-startsec).split(".")[1][0:2]
print("\n"+Fore.CYAN+"[SPLIT OPERATION COMPLETED]"+Fore.RESET+"\n"+\
"Parts created : "+str(partcunt)+" parts \n"+\
"Ledger created : "+str(filename)+".ldg \n"+\
"Time taken : "+str(totatime)+" seconds \n")
elif partcunt>=1000 and partcunt<10000:
print(Fore.CYAN+"[STARTING SPLIT OPERATION]"+Fore.RESET)
startsec=time.time()
for i in range(1,partcunt+1):
blocname=filename+"."+nogenthd(i)
blocbuff=actibuff[poselist[i-1]:poselist[i]]
hashlist[blocname]=hashlib.sha512(blocbuff).hexdigest()
sizelist[blocname]=len(blocbuff)
cuntlist[blocname]=nogenthd(i)
blocfile=open(blocname,"wb")
blocfile.write(blocbuff)
blocfile.close()
totlprog=totlprog+(100/partcunt)
print(str(cuntlist[blocname])+"\t"+str(blocname)+ " created!\t"+Style.DIM+str(sizelist[blocname])+" bytes\t"+Style.RESET_ALL+str(hashlist[blocname])+"\t"+Style.DIM+str(totlprog)[0:4]+"% completed"+Style.RESET_ALL)
makeldgr(filename,hashlist,sizelist,cuntlist)
endinsec=time.time()
totatime=str(endinsec-startsec).split(".")[0]+"."+str(endinsec-startsec).split(".")[1][0:2]
print("\n"+Fore.CYAN+"[SPLIT OPERATION COMPLETED]"+Fore.RESET+"\n"+\
"Parts created : "+str(partcunt)+" parts \n"+\
"Ledger created : "+str(filename)+".ldg \n"+\
"Time taken : "+str(totatime)+" seconds \n")
else:
print(Fore.RED+"[ERROR OCCURRED]"+Fore.RESET+"\n"+\
"Splitting operation could not be initiated!\n"+\
"We do not recommend splitting in this many parts")
except FileNotFoundError:
print(Fore.RED+"[ERROR OCCURRED]"+Fore.RESET+"\n"+\
"Splitting operation could not be initiated!\n"+\
"The requested file is not accessible. Make sure that - \n"+\
"- You have sufficient privileges to the directory \n"+
"- The path you have provided is correct \n"+
"- The file is indeed present in the given path")