-
Notifications
You must be signed in to change notification settings - Fork 0
/
dwCrypt.py
152 lines (123 loc) · 4.42 KB
/
dwCrypt.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
#!/usr/bin/python3
import os
from Crypto.Cipher import AES
from Crypto.Hash import SHA256
import string
import random
from dwUtils import *
def getKey(userID, keyLocation):
for roots, dirs, files in os.walk(keyLocation):
for f in files:
if userID + '.key' == f:
keyFile = os.path.join(roots, f)
try:
rFile = open(keyFile, 'r', encoding='utf-8')
key = rFile.readlines()[0]
rFile.close()
keyHash = SHA256.new(bytes(key, 'utf-8')).hexdigest()
except:
return None
else:
return (key, keyHash, keyFile)
class dwClientCTRL:
def __init__(self, usrSocket, dwSettings, usrID, usrIP, usrKey, usrKeyFile, usrKeyHash):
self.cmdSock = usrSocket
self.dwConfig = dwSettings
self.id = usrID
self.ip = usrIP
self.key = usrKey
self.keyFile = usrKeyFile
self.keyHash = usrKeyHash
self.cipher = AES.new(self.key)
def destroy(self):
try:
self.sWrite('000')
except:
pass
finally:
self.sock.close()
def strPad(chunk):
while len(chunk) % 16 != 0:
chunk += ' '
return chunk
def encrypt(self, data):
data = self.cipher.encrypt(self.strPad(data))
return(data)
def decrypt(self, data):
try:
data = strTrim(self.cipher.decrypt(data).decode('utf-8'))
except UnicodeDecodeError:
data = self.cipher.decrypt(data)
return data
return data
def sRead(self, buff):
data = self.cryptoSock.recv(buff)
if len(data) == 0:
return None
else:
data = self.decrypt(data)
return data
def sWrite(self, data):
self.sock.send(self.encrypt(data))
def newKey(finCount=None):
if finCount is None:
finCount = 32
finKey = ''
daPool = []
xtraChar = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')']
for i in list(string.ascii_lowercase):
daPool.extend(i)
for i in list(string.ascii_uppercase):
daPool.extend(i)
for i in range(10):
daPool.extend(str(i))
for i in range(len(xtraChar)):
daPool.extend(xtraChar[i])
for i in range(finCount):
finKey = finKey + daPool[random.randrange(0, len(daPool), 1)]
del daPool
return finKey
class fileMap:
def __init__(self):
MB = (1024 * 1024)
self.buff = (MB)
self.fileChunk = (5 * MB)
self.fileDir = '/home/omgimdrunk/Documents/files/'
self.dwFile = 'deb.iso'
self.thsFile = self.fileDir + self.dwFile
self.fs = os.path.getsize(self.thsFile)
self.numFulChunks = round(self.fs / self.fileChunk) # How many full chunks are in file
def makeMap(self):
totalChunkBytes = self.numFulChunks * self.fileChunk # Total bytes in full chunks
lastChunk = self.fs - totalChunkBytes # Remainder of bytes that does not equal 25 megs
newFile = open(self.thsFile, 'rb')
saveMap = open(self.dwFile + '.map', 'wb')
saveMap.write(bytes('file_name:' + self.dwFile + '\n', 'utf-8'))
saveMap.write(bytes('file_size:' + str(self.fs) + '\n', 'utf-8'))
saveMap.write(bytes('file_chunks:' + str(self.numFulChunks) + '\n', 'utf-8'))
saveMap.write(bytes('last_chunk:' + str(lastChunk) + '\n', 'utf-8'))
print('Mapping ' + self.thsFile)
bufCount = 0
for i in range(self.numFulChunks):
cHash = SHA256.new()
while not bufCount == self.fileChunk:
bufCount = bufCount + self.buff
bitGrab = newFile.read(self.buff)
cHash.update(bitGrab)
bufCount = 0
chunkHash = str(cHash.digest())
saveMap.write(bytes(str(i + 1) + ':' + chunkHash + '\n', 'utf-8'))
assBytes = lastChunk
while not assBytes == 0:
if assBytes < self.buff:
bitGrab = newFile.read(assBytes)
else:
bitGrab = newFile.read(self.buff)
cHash.update(bitGrab)
assBytes = assBytes - len(bitGrab)
chunkHash = str(cHash.digest())
saveMap.write(bytes('lc:' + chunkHash + '\n', 'utf-8'))
newFile.close()
saveMap.close()
def getChunk(self, numChunk):
pass