yapf formatted

This commit is contained in:
Crimson Hawk 2024-05-15 08:39:15 +08:00
parent 94b1c908c4
commit 5695cf04bb
Signed by: CrimsonHawk
GPG key ID: 0804DD39BB9BF5AC

View file

@ -7,7 +7,8 @@ import time
BS = 16 BS = 16
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS).encode() pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS).encode()
unpad = lambda s: s[:-ord(s[len(s)-1:])] unpad = lambda s: s[:-ord(s[len(s) - 1:])]
def iv(): def iv():
""" """
@ -16,6 +17,7 @@ def iv():
""" """
return chr(0) * 16 return chr(0) * 16
class AESCipher(object): class AESCipher(object):
""" """
https://github.com/dlitz/pycrypto https://github.com/dlitz/pycrypto
@ -42,8 +44,11 @@ class AESCipher(object):
dec = cipher.decrypt(enc) dec = cipher.decrypt(enc)
return unpad(dec).decode('utf-8') return unpad(dec).decode('utf-8')
class question: class question:
def __init__(self, qtype, qproblem, qoption1, qoption2, qoption3, qoption4):
def __init__(self, qtype, qproblem, qoption1, qoption2, qoption3,
qoption4):
qtype = self.qtype qtype = self.qtype
qproblem = self.qproblem qproblem = self.qproblem
qoption1 = self.qoption1 qoption1 = self.qoption1
@ -61,8 +66,8 @@ class libexam:
password = -1 password = -1
def setmode(modein): def setmode(modein):
mode=modein mode = modein
modeset=1 modeset = 1
print(f"Mode set to: {mode}\n") #1: client 2: admin print(f"Mode set to: {mode}\n") #1: client 2: admin
return 0 return 0
@ -70,9 +75,8 @@ class libexam:
return f"""Mode: {mode} return f"""Mode: {mode}
Username: {username}\n""" Username: {username}\n"""
def setuser(user): def setuser(user):
username=user username = user
print(f"Username set to: {user}\n") print(f"Username set to: {user}\n")
return 0 return 0
@ -83,7 +87,7 @@ class libexam:
return 0 return 0
def readquestions(): def readquestions():
sha256=hashlib.sha256() sha256 = hashlib.sha256()
questiondocs = open("question.txt", "r") questiondocs = open("question.txt", "r")
n = questiondocs.readline() n = questiondocs.readline()
@ -91,20 +95,22 @@ class libexam:
print(f"n={n}\n") print(f"n={n}\n")
counter=n counter = n
while(True):#hash check while (True): #hash check
temp = questiondocs.readline() temp = questiondocs.readline()
if(temp=="chk"): if (temp == "chk"):
sha256.update(temp) sha256.update(temp)
rhash = sha256.digest() rhash = sha256.digest()
ehash = questiondocs.readline() ehash = questiondocs.readline()
if(rhash==ehash): if (rhash == ehash):
print(f"Hashes match: {rhash}\n") print(f"Hashes match: {rhash}\n")
break break
else: else:
print(f"Hashes does not match !\nExpected: {ehash}\nGot: {rhash}\n") print(
f"Hashes does not match !\nExpected: {ehash}\nGot: {rhash}\n"
)
return 1 return 1
sha256.update(temp) sha256.update(temp)
@ -112,27 +118,23 @@ class libexam:
questiondocs = open("question.txt", "r") questiondocs = open("question.txt", "r")
temp = questiondocs.readline() temp = questiondocs.readline()
while (True):
while(True):
temp = questiondocs.readline() temp = questiondocs.readline()
if(temp=="chk"): if (temp == "chk"):
break break
qtype = temp qtype = temp
temp = questiondocs.readline() temp = questiondocs.readline()
temp = temp.split("|") temp = temp.split("|")
questions.append(question(qtype,temp[0],temp[1],temp[2],temp[3],temp[4])) questions.append(
question(qtype, temp[0], temp[1], temp[2], temp[3], temp[4]))
def writeanswer(qn, ans):
if (username == ""):
def writeanswer(qn,ans):
if(username==""):
print(f"Username not set!\n") print(f"Username not set!\n")
return 1 return 1
md5 = hashlib.md5() md5 = hashlib.md5()
md5.update(username) md5.update(username)
filename = md5.digest() filename = md5.digest()
@ -140,33 +142,33 @@ class libexam:
sha256 = hashlib.sha256() sha256 = hashlib.sha256()
datadocs = open(f"{filename}.txt", "r") datadocs = open(f"{filename}.txt", "r")
n = datadocs.readline() n = datadocs.readline()
sha256.update(n) sha256.update(n)
while(True):#hash check while (True): #hash check
temp = data.readline() temp = data.readline()
if(temp=="chk"): if (temp == "chk"):
sha256.update(temp) sha256.update(temp)
rhash = sha256.digest() rhash = sha256.digest()
ehash = data.readline() ehash = data.readline()
if(rhash==ehash): if (rhash == ehash):
print(f"Hashes match: {rhash}\n") print(f"Hashes match: {rhash}\n")
break break
else: else:
print(f"Hashes does not match !\nExpected: {ehash}\nGot: {rhash}\n") print(
f"Hashes does not match !\nExpected: {ehash}\nGot: {rhash}\n"
)
return 1 return 1
sha256.update(temp) sha256.update(temp)
datadocs.close() datadocs.close()
datadocs = open(f"{filename}.txt", "r+") datadocs = open(f"{filename}.txt", "r+")
timestamp = hex(int(time.time()))#part c of a line according to docs timestamp = hex(int(time.time())) #part c of a line according to docs
while(len(timestamp) != 16): while (len(timestamp) != 16):
timestamp = "0" + timestamp timestamp = "0" + timestamp
cd = timestamp + ans cd = timestamp + ans
@ -179,7 +181,7 @@ class libexam:
userhash = hashlib.sha256() userhash = hashlib.sha256()
userhash.update(username) userhash.update(username)
key = userhash.digest()#generated key for encryption key = userhash.digest() #generated key for encryption
print(f"Key generated: {key}\n") print(f"Key generated: {key}\n")
@ -204,14 +206,13 @@ class libexam:
datadocs.writelines(data) datadocs.writelines(data)
datadocs.close() datadocs.close()
datadocs = open(f"{filename}.txt", "r+") datadocs = open(f"{filename}.txt", "r+")
n = datadocs.readline() n = datadocs.readline()
sha256.update(n) sha256.update(n)
while(True):#calculate hash while (True): #calculate hash
temp = data.readline() temp = data.readline()
if(temp=="chk"): if (temp == "chk"):
sha256.update(temp) sha256.update(temp)
targethash = data.readline() targethash = data.readline()
break break
@ -219,12 +220,11 @@ class libexam:
print(f"New hash generated: {targethash}\n") print(f"New hash generated: {targethash}\n")
# read a list of lines into data # read a list of lines into data
data = datadocs.readlines() data = datadocs.readlines()
# now change the answer line, note that you have to add a newline # now change the answer line, note that you have to add a newline
data[1+n] = targethash data[1 + n] = targethash
# and write everything back # and write everything back
datadocs.writelines(data) datadocs.writelines(data)
@ -233,11 +233,10 @@ class libexam:
return 0 return 0
def readanswer(qn): def readanswer(qn):
if(username==""): if (username == ""):
print(f"Username not set!\n") print(f"Username not set!\n")
return 1 return 1
md5 = hashlib.md5() md5 = hashlib.md5()
md5.update(username) md5.update(username)
filename = md5.digest() filename = md5.digest()
@ -245,24 +244,25 @@ class libexam:
sha256 = hashlib.sha256() sha256 = hashlib.sha256()
datadocs = open(f"{filename}.txt", "r") datadocs = open(f"{filename}.txt", "r")
n = datadocs.readline() n = datadocs.readline()
sha256.update(n) sha256.update(n)
while(True):#hash check while (True): #hash check
temp = data.readline() temp = data.readline()
if(temp=="chk"): if (temp == "chk"):
sha256.update(temp) sha256.update(temp)
rhash = sha256.digest() rhash = sha256.digest()
ehash = data.readline() ehash = data.readline()
if(rhash==ehash): if (rhash == ehash):
print(f"Hashes match: {rhash}\n") print(f"Hashes match: {rhash}\n")
break break
else: else:
print(f"Hashes does not match !\nExpected: {ehash}\nGot: {rhash}\n") print(
f"Hashes does not match !\nExpected: {ehash}\nGot: {rhash}\n"
)
return 1 return 1
sha256.update(temp) sha256.update(temp)
@ -277,15 +277,17 @@ class libexam:
s1h.update(s1data) #update hash s1h.update(s1data) #update hash
print(f"Stage 1 integrity check\n") print(f"Stage 1 integrity check\n")
if(s1hash==s1h.digest()): if (s1hash == s1h.digest()):
print(f"Hashes match: {s1hash}\n") print(f"Hashes match: {s1hash}\n")
else: else:
print(f"Hashes does not match !\nExpected: {s1h.digest()}\nGot: {s1hash}\n") print(
f"Hashes does not match !\nExpected: {s1h.digest()}\nGot: {s1hash}\n"
)
return 1 return 1
userhash = hashlib.sha256() userhash = hashlib.sha256()
userhash.update(username) userhash.update(username)
key = userhash.digest()#generated key for decryption key = userhash.digest() #generated key for decryption
print(f"Key generated: {key}\n") print(f"Key generated: {key}\n")
@ -299,10 +301,12 @@ class libexam:
s2h.update(s2data) #update hash s2h.update(s2data) #update hash
print(f"Stage 2 integrity check\n") print(f"Stage 2 integrity check\n")
if(s2hash==s2h.digest()): if (s2hash == s2h.digest()):
print(f"Hashes match: {s2hash}\n") print(f"Hashes match: {s2hash}\n")
else: else:
print(f"Hashes does not match !\nExpected: {s2h.digest()}\nGot: {s2hash}\n") print(
f"Hashes does not match !\nExpected: {s2h.digest()}\nGot: {s2hash}\n"
)
return 1 return 1
timestamp = int(s2data[0:15], 16) timestamp = int(s2data[0:15], 16)
@ -310,5 +314,3 @@ class libexam:
print(f"Found answer: {s2data[16:]}") print(f"Found answer: {s2data[16:]}")
return s2data[16:] return s2data[16:]