Advertisement
Guest User

raw

a guest
Jan 28th, 2016
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.00 KB | None | 0 0
  1. import json, os, hashlib
  2.  
  3. username, password = None, None
  4.  
  5.  
  6. def init():
  7.     global datafile, data
  8.     datafile = "data.dat"
  9.     if not os.path.exists(datafile):
  10.         with open(datafile, "w+") as file:
  11.             data = {
  12.             "users" : [{"username" : "admin", "password" : hashPassword("admin")}],
  13.             "settings" : {"hashAmount" : 100, "login" : 1}
  14.             }
  15.             json.dump(data, file, ensure_ascii=False)
  16.     with open(datafile, "r") as file:
  17.         data = file.read()
  18.     data = json.loads(data)
  19.  
  20. def phraseencrypt(what, phrase): # accepts to arguments a string to enceypt and some tex to tncrypt it with.
  21.     key = [] # create a list to store the phrase to encrypt with.
  22.     for char in phrase: #for loop to itirate through each charcter in the tet stored as prhrase (what to encrypt with)
  23.         key.append(ord(char)) # convert letter to ascii number and apend it to the list key. repeat for all letters
  24.     result = "" # variable ued to append the end result to
  25.     indexkey = 0 # used to keep track of which letter up to in the phrase encryption
  26.     for i in what: # for every letter in the text the user wants encrypting
  27.         i = ord(i) # convert letter to ascii number
  28.         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.
  29.             indexkey = 0 # if previouse if statment is true then reset it to 0
  30.         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
  31.             if i < 1: # if i is less than 1 then then reset to 127
  32.                 i = 127
  33.             i -= 1 # take away one from i (change shift of letter)
  34.         result+= chr(i) # convert ascii number back to letter
  35.         indexkey+=1 # append to string
  36.     return result # return the result
  37.  
  38.  
  39. def hashPassword(password):
  40.     global data
  41.     passwordlen = len(password)
  42.     run = 0
  43.     type = 0
  44.     try:
  45.         hashAmount = data['settings']['hashAmount'] * passwordlen
  46.     except:
  47.         hashAmount = 100
  48.     while run != hashAmount:
  49.         password = str(password).encode('utf-8')
  50.         if type == 0:
  51.             password = hashlib.md5(password).hexdigest() * passwordlen
  52.         elif type == 1:
  53.             password = hashlib.sha1(password).hexdigest() * passwordlen
  54.         elif type == 2:
  55.             password = hashlib.sha224(password).hexdigest() * passwordlen
  56.         elif type == 3:
  57.             password = hashlib.sha256(password).hexdigest() * passwordlen
  58.         elif type == 4:
  59.             password = hashlib.sha384(password).hexdigest() * passwordlen
  60.         elif type == 5:
  61.             password = hashlib.sha512(password).hexdigest() * passwordlen
  62.         elif type == 6:
  63.             type = 0
  64.             run += 1
  65.         type += 1
  66.     return phraseencrypt(str(password), str(password))
  67.  
  68.  
  69. def addUser():
  70.     global data, datafile
  71.     newuserName = input("New Users Username: ").strip()
  72.     if newuserName == "":
  73.         print("Username cant be nothing\n")
  74.         return False
  75.     for user in data['users']:
  76.         if user["username"] == newuserName:
  77.             print("Username Taken\n")
  78.             return False
  79.     newuserPassword = input("New Users Password: ").strip()
  80.     if newuserPassword == "":
  81.         print("Password cant be nothing\n")
  82.         return False
  83.     newuserPassword = hashPassword(newuserPassword)
  84.     data['users'].append({"username" : newuserName, "password" : newuserPassword})
  85.     os.unlink(datafile)
  86.     with open(datafile, "w+") as file:
  87.         json.dump(data, file, ensure_ascii=False)
  88.         print("Created User\n")
  89.     return True
  90.  
  91. def deleteUser():
  92.     global data, datafile
  93.     index = 0
  94.     delUserName = input("User To Delete: ")
  95.     for user in data['users']:
  96.         if user["username"] == delUserName:
  97.             data['users'].pop(index)
  98.             os.unlink(datafile)
  99.             with open(datafile, "w+") as file:
  100.                 json.dump(data, file, ensure_ascii=False)
  101.             print("Deleted User\n")
  102.             return True
  103.         index += 1
  104.     print("Unable to find user\n")
  105.     return False
  106.  
  107. def changePass():
  108.     global data, datafile
  109.     new = input("New Password: ")
  110.     index = 0
  111.     for user in data['users']:
  112.         if user['username'] == username:
  113.             data['users'][index]['password'] = hashPassword(new)
  114.             os.unlink(datafile)
  115.             with open(datafile, "w+") as file:
  116.                 json.dump(data, file, ensure_ascii=False)
  117.             print("Password Chnaged\n")
  118.             return True
  119.         index += 1
  120.  
  121.  
  122. def login():
  123.     global username, password, data, datafile
  124.     if data['settings']['login'] == 0:
  125.         return True
  126.     if not username and not password:
  127.         username = input("Username: ").strip()
  128.         password = hashPassword(input("Password: ").strip())
  129.  
  130.     for user in data['users']:
  131.         if user['username'] == username and user['password'] == password:
  132.             return True
  133.     return False
  134.  
  135.  
  136.  
  137. def main():
  138.     global data, datafile, username, password
  139.     if not login():
  140.         username, password = None, None
  141.         main()
  142.     while True:
  143.         print("\nWhat would you like to do?\n")
  144.         q = input("For Help Use 'help'\n")
  145.         if q == "addUser":
  146.             addUser()
  147.         elif q == "delUser":
  148.             deleteUser()
  149.         elif q == "pass":
  150.             changePass()
  151.         elif q == "q":
  152.             quit()
  153.         elif q == "reload":
  154.             init()
  155.         elif q == "logout":
  156.             print("\n"*10000)
  157.             print("Logged Out")
  158.             username, password = None, None
  159.             main()
  160.             print("Reload complete")
  161.         elif q == "help":
  162.             print("""
  163.    Help:
  164.            addUser) Adds a user o the system
  165.            delUser) Deletes a user
  166.            pass) Change password
  167.            q) quit/exit
  168.            SET) Used To modify settings
  169.            reload) Reloads settings
  170.            logout) logs the user out
  171.            """)
  172.         elif "SET" in q:
  173.             try:
  174.                 q = q.split(" ")
  175.                 if q[1] == "?":
  176.                     try:
  177.                         if q[2] == "list":
  178.                             print("SET option list: ")
  179.                             for option in data['settings']:
  180.                                 print(option)
  181.                             main()
  182.                         elif q[2] in data['settings']:
  183.                             print("Current value of " + q[2] + " is: " + str(data['settings'][q[2]]))
  184.                             main()
  185.                         else:
  186.                             print("Unknowen Option")
  187.                             print("Use 'SET ? list' for a list of options")
  188.                             main()
  189.  
  190.                     except:
  191.                         pass
  192.                     print("""
  193.    SET Help:
  194.                SET ?) Brings up hrlp
  195.                SET optiopn value) sets an option to a specific value
  196.                SET ? list
  197.                    """)
  198.  
  199.  
  200.                 try:
  201.                     if q[2] and q[1] in data['settings']:
  202.                         print("Changed " + q[1] + " to " + q[2])
  203.                         try:
  204.                             data['settings'][q[1]] = int(q[2])
  205.                         except:
  206.                             data['settings'][q[1]] = q[2]
  207.                         os.unlink(datafile)
  208.                         with open(datafile, "w+") as file:
  209.                             json.dump(data, file, ensure_ascii=False)
  210.                         init()
  211.  
  212.                 except:
  213.                     print("Unknowen Option")
  214.                     print("Use 'SET ? list' for a list of options")
  215.                     main()
  216.             except:
  217.                 print("Unknowen Command")
  218.                 print("Use 'SET ?' for help")
  219.                 main()
  220.     print("Unknowen Command")
  221.  
  222.  
  223.  
  224. init()
  225. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement