Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json, os, hashlib
- username, password = None, None
- def init():
- global datafile, data
- datafile = "data.dat"
- if not os.path.exists(datafile):
- with open(datafile, "w+") as file:
- data = {
- "users" : [{"username" : "admin", "password" : hashPassword("admin")}],
- "settings" : {"hashAmount" : 100, "login" : 1}
- }
- json.dump(data, file, ensure_ascii=False)
- with open(datafile, "r") as file:
- data = file.read()
- data = json.loads(data)
- def phraseencrypt(what, phrase): # accepts to arguments a string to enceypt and some tex to tncrypt it with.
- key = [] # create a list to store the phrase to encrypt with.
- for char in phrase: #for loop to itirate through each charcter in the tet stored as prhrase (what to encrypt with)
- key.append(ord(char)) # convert letter to ascii number and apend it to the list key. repeat for all letters
- result = "" # variable ued to append the end result to
- indexkey = 0 # used to keep track of which letter up to in the phrase encryption
- for i in what: # for every letter in the text the user wants encrypting
- i = ord(i) # convert letter to ascii number
- if indexkey == len(phrase): # test to see that the indexkey (place in the llist) is not over the amount of letters that are in it.
- indexkey = 0 # if previouse if statment is true then reset it to 0
- for num in range(1, key[indexkey]+1): # for every number inbtween 1 and the key. Since i store the index of the list in a varibale i can then call the letter that i am up to. +1 to fix bug/error
- if i < 1: # if i is less than 1 then then reset to 127
- i = 127
- i -= 1 # take away one from i (change shift of letter)
- result+= chr(i) # convert ascii number back to letter
- indexkey+=1 # append to string
- return result # return the result
- def hashPassword(password):
- global data
- passwordlen = len(password)
- run = 0
- type = 0
- try:
- hashAmount = data['settings']['hashAmount'] * passwordlen
- except:
- hashAmount = 100
- while run != hashAmount:
- password = str(password).encode('utf-8')
- if type == 0:
- password = hashlib.md5(password).hexdigest() * passwordlen
- elif type == 1:
- password = hashlib.sha1(password).hexdigest() * passwordlen
- elif type == 2:
- password = hashlib.sha224(password).hexdigest() * passwordlen
- elif type == 3:
- password = hashlib.sha256(password).hexdigest() * passwordlen
- elif type == 4:
- password = hashlib.sha384(password).hexdigest() * passwordlen
- elif type == 5:
- password = hashlib.sha512(password).hexdigest() * passwordlen
- elif type == 6:
- type = 0
- run += 1
- type += 1
- return phraseencrypt(str(password), str(password))
- def addUser():
- global data, datafile
- newuserName = input("New Users Username: ").strip()
- if newuserName == "":
- print("Username cant be nothing\n")
- return False
- for user in data['users']:
- if user["username"] == newuserName:
- print("Username Taken\n")
- return False
- newuserPassword = input("New Users Password: ").strip()
- if newuserPassword == "":
- print("Password cant be nothing\n")
- return False
- newuserPassword = hashPassword(newuserPassword)
- data['users'].append({"username" : newuserName, "password" : newuserPassword})
- os.unlink(datafile)
- with open(datafile, "w+") as file:
- json.dump(data, file, ensure_ascii=False)
- print("Created User\n")
- return True
- def deleteUser():
- global data, datafile
- index = 0
- delUserName = input("User To Delete: ")
- for user in data['users']:
- if user["username"] == delUserName:
- data['users'].pop(index)
- os.unlink(datafile)
- with open(datafile, "w+") as file:
- json.dump(data, file, ensure_ascii=False)
- print("Deleted User\n")
- return True
- index += 1
- print("Unable to find user\n")
- return False
- def changePass():
- global data, datafile
- new = input("New Password: ")
- index = 0
- for user in data['users']:
- if user['username'] == username:
- data['users'][index]['password'] = hashPassword(new)
- os.unlink(datafile)
- with open(datafile, "w+") as file:
- json.dump(data, file, ensure_ascii=False)
- print("Password Chnaged\n")
- return True
- index += 1
- def login():
- global username, password, data, datafile
- if data['settings']['login'] == 0:
- return True
- if not username and not password:
- username = input("Username: ").strip()
- password = hashPassword(input("Password: ").strip())
- for user in data['users']:
- if user['username'] == username and user['password'] == password:
- return True
- return False
- def main():
- global data, datafile, username, password
- if not login():
- username, password = None, None
- main()
- while True:
- print("\nWhat would you like to do?\n")
- q = input("For Help Use 'help'\n")
- if q == "addUser":
- addUser()
- elif q == "delUser":
- deleteUser()
- elif q == "pass":
- changePass()
- elif q == "q":
- quit()
- elif q == "reload":
- init()
- elif q == "logout":
- print("\n"*10000)
- print("Logged Out")
- username, password = None, None
- main()
- print("Reload complete")
- elif q == "help":
- print("""
- Help:
- addUser) Adds a user o the system
- delUser) Deletes a user
- pass) Change password
- q) quit/exit
- SET) Used To modify settings
- reload) Reloads settings
- logout) logs the user out
- """)
- elif "SET" in q:
- try:
- q = q.split(" ")
- if q[1] == "?":
- try:
- if q[2] == "list":
- print("SET option list: ")
- for option in data['settings']:
- print(option)
- main()
- elif q[2] in data['settings']:
- print("Current value of " + q[2] + " is: " + str(data['settings'][q[2]]))
- main()
- else:
- print("Unknowen Option")
- print("Use 'SET ? list' for a list of options")
- main()
- except:
- pass
- print("""
- SET Help:
- SET ?) Brings up hrlp
- SET optiopn value) sets an option to a specific value
- SET ? list
- """)
- try:
- if q[2] and q[1] in data['settings']:
- print("Changed " + q[1] + " to " + q[2])
- try:
- data['settings'][q[1]] = int(q[2])
- except:
- data['settings'][q[1]] = q[2]
- os.unlink(datafile)
- with open(datafile, "w+") as file:
- json.dump(data, file, ensure_ascii=False)
- init()
- except:
- print("Unknowen Option")
- print("Use 'SET ? list' for a list of options")
- main()
- except:
- print("Unknowen Command")
- print("Use 'SET ?' for help")
- main()
- print("Unknowen Command")
- init()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement