Commit dbe05f21 authored by wx002's avatar wx002

added hashing for proj1

parent 70d49be9
from socket import *
import re
import base64
dest = ('127.0.0.1', 5000)
......@@ -11,15 +12,29 @@ fileList = []
lineStr = ''
currentHeader = None
def getHeader(string):
pass
def get_decode_list(byteString):
string = byteString.decode('utf-8')
stringList = re.split('\t', string, maxsplit=2)
return stringList
def get_line_index(stringList):
return stringList[0]
def verify_hash(stringList):
hash_str = stringList[1].encode()
recv_str = stringList[2]
if base64.b64decode(hash_str) == recv_str:
return True
else:
return False
while True:
data, addr = sock.recvfrom(1048)
print("recv data: {}".format(data))
while data != b'####':
lineStr += data.decode('utf-8')
print(lineStr)
print(get_decode_list(lineStr))
data, addr = sock.recvfrom(1048)
if data == b'####':
......
......@@ -22,18 +22,18 @@ def load_data(fileName):
def chunk(line, size=1028):
return [line[i:i+size] for i in range(0,len(line), size)]
def buildHeader(string):
header = base64.b64encode(bytes('*'+string+'*'))
return header
def hash_string(string):
str_hash = base64.b64encode(string.encode()) + b'\t'
return str_hash
def send_line(socket, line, size):
def send_line(socket, line_bytes, size):
if len(line) < size:
#send the line directly if is less than the max size
socket.sendto(str.encode(line), dest)
socket.sendto(line_bytes, dest)
else:
lineChunks = chunk(line, size)
lineChunks = chunk(line_bytes, size)
for c in lineChunks:
socket.sendto(str.encode(c), dest)
socket.sendto(c, dest)
socket.sendto(b'####', dest)
......@@ -46,10 +46,11 @@ if __name__ == '__main__':
ack = False
while not ack:
try:
index = testLines.index(line)
header = buildHeader(bytes(index))
line = header + line
send_line(sock, line, 1048)
# build hash
hash_index = hash_string(str(testLines.index(line)))
hash_str = hash_string(line)
hashed_line = hash_index+hash_str + line.encode()
send_line(sock, hashed_line, 1048)
data = sock.recv(1048)
print(data)
if data == b'ACK':
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment