def cipher(self, username, text): """ hashes text+user's salt together for added security Args: username to locate user's file and salt text to have something to cipher Returns: List of hashed characters, each char as an element """ n = 0 self.text = self.timeStamp(self.username) + text + "\n" with open(DB, "r") as database: dbContents = json.loads(database.read()) self.salt = dbContents[self.username][1] # this is utterly f*****g retarded but im not doing a circular import with open(USERFILES_PATH + "\\" + self.username + "\\" + self.username + ".json") as userFile: userFileData = json.loads(userFile.read()) with open(CHAR_FILE, "r") as charFile: charList = json.loads(charFile.read()) while n < len(self.text): if not self.text[n] in charList: charList.append(self.text[n]) char = (self.text[n] + self.salt).encode('utf-8') char = hashlib.sha256(char).hexdigest() userFileData.append(char) n += 1 with open(CHAR_FILE, "w") as charFile: charFile.write(json.dumps(sorted(charList), indent=4)) debug("CIP | SUCCESFUL") return userFileData
def addText(self, username, text): """ appends the user's json file with secured text Args: username to locate file text to hash and save """ textToAdd = Sec(self.username, self.password) hashedText = textToAdd.cipher(self.username, text) with open(self.filePath + self.username + ".json", "w") as userFile: userFile.write(json.dumps(hashedText, indent=4)) debug("ADDTXT | SUCCESSFUL")
def deleteUser(self, username): """ deletes the user's main directory, along with removing them from the database Args: username to decide which user to delete """ with open(DB, "r") as database: with open(DB, "r") as database: dbContents = json.loads(database.read()) del dbContents[self.username] with open(DB, "w") as database: updatedDB = json.dumps(dbContents, sort_keys=True, indent=4) database.write(updatedDB) shutil.rmtree(USERFILES_PATH + self.username) debug("DEL | username: " + self.username)
def register(self, username, password): """ Registers the user, stored in DB, checks if username is already taken """ if not self.inDB(self.username): self.makeUserFile(self.username) with open(DB, "r") as database: dbContents = json.loads(database.read()) self.password = Sec(self.username, self.password) self.password, self.salt = self.password.encrypt(self.password) dbContents.update({self.username: [self.password, self.salt]}) with open(DB, "w") as database: updatedDB = json.dumps(dbContents, sort_keys=True, indent=4) database.write(updatedDB) debug("REG | username : "******", password: "******"REG | user " + self.username + " already in database")
def setup(): """ sets up necessary directories """ try: os.mkdir(PATH + "\\database") os.mkdir(PATH + "\\userFiles") debug("SETUP | clreated DB, USER_FILES dirs") except FileExistsError: pass with open(DB, "a+"): pass with open(PATH + "\\database\\charList.json", "a+") as charList: pass if os.stat(PATH + "\\database\\charList.json").st_size == 0: with open(PATH + "\\database\\charList.json", "w") as charList: json.dumps(charList.write("[]")) if os.stat(DB).st_size == 0: with open(DB, "w") as db: json.dumps(db.write("{}"))
def loggedIn(self, username, password): """ determines whether an user is registered and if so whether their password is correct Args: username, password for obvious reasons Returns: True or False based on the entered password """ if self.inDB(self.username): with open(DB, "r") as database: dbContents = json.loads(database.read()) pw = Sec(self.username, self.password) self.password = pw.decrypt(self.username, self.password) if self.password == dbContents[self.username][0]: debug("LOGIN | " + self.username + " SUCCESS") return True else: debug("LOGIN | " + self.username + " FAILED") return False else: debug("LOGIN | " + self.username + " NOT FOUND") return False