def startButtonClicked(self): if self.rememberMeCheckBox.checkState(): if len(self.passwordDBPasswordEntry.text()) == 0: self.loginButton.setText("PasswordDB field cannot be empty!") return None if len(self.accNameEntry.text()) == 0: self.loginButton.setText("Account name cannot be empty") return None self.setElementsEnableAtribute(False,"Ok") self.state = States.OK self.password = Cipher(self.passwordDBPasswordEntry.text()) self.account = Account(self.accNameEntry.text(),self.loginEntry.text(),self.passwordEntry1.text(),self.passwordEntry2.text()) if self.fun is not None: self.fun(self.getAccount()) self.close()
def __init__(self, mainPassword=None): """ This class takes care of encryption layer :param mainPassword: <--- password from user, len() can't be 0 """ # check for a correct type and value if not mainPassword == None: self.mainPw = Cipher(self.checkAndGeneratePassword(mainPassword)) else: # self.pw = "".join([chr(random.randrange(33, 126)) for x in range(1, 33)]) # 32 is "space" in ascii 126 is list char ~ self.mainPw = Cipher(Fernet.generate_key()) key = bytes(self.mainPw.getDecrypted().encode("ASCII")) # init main math logic module self.f = Fernet(base64.urlsafe_b64encode(key))
def changeAccountFieldValue(self, field, value): if field == Fields.NAME: self.name = Cipher(value) elif field == Fields.LOGIN: self.login = Cipher(value) elif field == Fields.PASSWORD1: self.password1 = Cipher(value) elif field == Fields.PASSWORD2: self.password2 = Cipher(value) print(self)
def __init__(self,accName="",fun=None,currentAccList=None): super().__init__() self.fun = fun self.currentAccList = currentAccList self.accName = accName # type: NagiosConnectionManager self.mainGrid = QGridLayout() # type: QGridLayout self.mainGrid.setSizeConstraint(QLayout.SetMinimumSize) self.wSize = 450 self.hSize = 180 self.setMinimumSize(self.wSize, self.hSize) self.setFixedSize(self.wSize, self.hSize) self.setWindowTitle("Adding account") self.password = Cipher("") self.populateGrid() self.account = None self.setLayout(self.mainGrid) self.state = States.UNDEFINE center(self) self.show() print("Show") self.setFocus(QtCore.Qt.ActiveWindowFocusReason)
def __init__(self, name="", login="", password1="", password2=""): self.name = Cipher(name) self.login = Cipher(login) self.password1 = Cipher(password1) self.password2 = Cipher(password2)
class Account: def __init__(self, name="", login="", password1="", password2=""): self.name = Cipher(name) self.login = Cipher(login) self.password1 = Cipher(password1) self.password2 = Cipher(password2) #---------------------------------------------------------------------------------------------------------------- def getName(self): return self.name.getDecrypted() #---------------------------------------------------------------------------------------------------------------- def getLogin(self): return self.login.getDecrypted() #---------------------------------------------------------------------------------------------------------------- def getPassword1(self): return self.password1.getDecrypted() #---------------------------------------------------------------------------------------------------------------- def getPassword2(self): return self.password2.getDecrypted() #---------------------------------------------------------------------------------------------------------------- def getValuesAsList(self): return [ self.name.getDecrypted(), self.login, self.password1, self.password2 ] #---------------------------------------------------------------------------------------------------------------- def getValueByField(self, field): if field == Fields.NAME: return self.name.getDecrypted() elif field == Fields.LOGIN: return self.login.getDecrypted() elif field == Fields.PASSWORD1: return self.password1.getDecrypted() elif field == Fields.PASSWORD2: return self.password2.getDecrypted() #---------------------------------------------------------------------------------------------------------------- def changeAccountFieldValue(self, field, value): if field == Fields.NAME: self.name = Cipher(value) elif field == Fields.LOGIN: self.login = Cipher(value) elif field == Fields.PASSWORD1: self.password1 = Cipher(value) elif field == Fields.PASSWORD2: self.password2 = Cipher(value) print(self) #---------------------------------------------------------------------------------------------------------------- def getAsStrCsv(self): n = self.name.getDecrypted() l = self.login.getDecrypted() p1 = self.password1.getDecrypted() p2 = self.password2.getDecrypted() return f"{n},{l},{p1},{p2}" #---------------------------------------------------------------------------------------------------------------- def getDecryptedAttribute(self, attributeValue): if Fields.NAME == attributeValue: return self.name.getDecrypted() elif Fields.LOGIN == attributeValue: return self.login.getDecrypted() elif Fields.PASSWORD1 == attributeValue: return self.password1.getDecrypted() elif Fields.PASSWORD2 == attributeValue: return self.password2.getDecrypted() raise ValueError("Impossibru, filed does not exist.") #---------------------------------------------------------------------------------------------------------------- def setAccountValues(self, csvLine): csvLine = str(csvLine) splited = csvLine.split(",") try: self.name = Cipher(splited[0]) except Exception: self.name = Cipher("") try: self.login = Cipher(splited[1]) except Exception: self.login = Cipher("") try: self.password1 = Cipher(splited[2]) except Exception: self.password1 = Cipher("") try: self.password2 = Cipher(splited[3]) except Exception: self.password2 = Cipher("") return self #---------------------------------------------------------------------------------------------------------------- def __hash__(self): """Implement custom __hash__ method""" strVal = self.name.getDecrypted() hashValue = sum([ord(char) for char in strVal]) return hash(hashValue) #---------------------------------------------------------------------------------------------------------------- def __eq__(self, other): if other == None: return False if self.name.getDecrypted().__hash__() == other.name.getDecrypted( ).__hash__(): return True else: return False #---------------------------------------------------------------------------------------------------------------- def __str__(self): rv = self.name.getDecrypted() return str(rv) #---------------------------------------------------------------------------------------------------------------- def __copy__(self): return Account(self.name.getDecrypted(), self.login.getDecrypted(), self.password1.getDecrypted(), self.password2.getDecrypted())
def setAccountValues(self, csvLine): csvLine = str(csvLine) splited = csvLine.split(",") try: self.name = Cipher(splited[0]) except Exception: self.name = Cipher("") try: self.login = Cipher(splited[1]) except Exception: self.login = Cipher("") try: self.password1 = Cipher(splited[2]) except Exception: self.password1 = Cipher("") try: self.password2 = Cipher(splited[3]) except Exception: self.password2 = Cipher("") return self
class CryptoManager: def __init__(self, mainPassword=None): """ This class takes care of encryption layer :param mainPassword: <--- password from user, len() can't be 0 """ # check for a correct type and value if not mainPassword == None: self.mainPw = Cipher(self.checkAndGeneratePassword(mainPassword)) else: # self.pw = "".join([chr(random.randrange(33, 126)) for x in range(1, 33)]) # 32 is "space" in ascii 126 is list char ~ self.mainPw = Cipher(Fernet.generate_key()) key = bytes(self.mainPw.getDecrypted().encode("ASCII")) # init main math logic module self.f = Fernet(base64.urlsafe_b64encode(key)) #---------------------------------------------------------------------------------------------------------------- def generateKey(self, string): """Extends value to 32 len() str """ index = 0 rv = "" string = str(string.encode("ASCII")) iter = cycle(string) key = string[0:] increment = 1 # generate a 32 size string while len(rv) != 32: nextValue = next(iter) try: amount = (ord(key[index])) + increment except IndexError: # case when user input only one character index -= 2 amount = (ord(key[index])) + increment if (index + increment) > 126: amount = 126 rv += nextValue + chr(amount) if index + increment >= len(key): key = str(rv) index = 1 increment += 1 index += increment return rv #---------------------------------------------------------------------------------------------------------------- def checkAndGeneratePassword(self, mainPassword): assert isinstance( mainPassword, str ), f"mainPassword value must be a str type got {type(mainPassword)}" assert len(mainPassword) != 0 and len( mainPassword ) <= 32 or None, f"len of a string not int range 0<x<33 x int type provided {len(mainPassword)}" return self.generateKey(mainPassword) #---------------------------------------------------------------------------------------------------------------- def encrypt(self, valueOrObject): """Encrypt given string based on a initial key, return it""" value = str(valueOrObject) self.mainPw.getDecrypted() encryptedValue = self.f.encrypt(bytes(value, encoding="UTF-8")) return str(encryptedValue)[2:-1] # remove b' and ' before return #---------------------------------------------------------------------------------------------------------------- def decrypt(self, valueOrObject): """Decrypt given string based on a initial key, return it""" value = str(valueOrObject) assert isinstance( valueOrObject, str ), f"valueOrObject value must be a str type got {type(valueOrObject)}" assert len( valueOrObject) != 0, f"len of a string must be bigger then 0" decryptedValue = self.f.decrypt(bytes(value, encoding="UTF-8")) return str(decryptedValue)[2:-1] #---------------------------------------------------------------------------------------------------------------- def decryptLine(self, line): toReturn = "" try: for element in line.split(","): toReturn += self.decrypt(element) + "," toReturn = toReturn.strip(",") except Exception: return "" return toReturn #---------------------------------------------------------------------------------------------------------------- def changePassword(self, newPassword): self.mainPw = Cipher(self.checkAndGeneratePassword(newPassword)) key = bytes(self.mainPw.getDecrypted().encode("ASCII")) # init main math logic module self.f = Fernet(base64.urlsafe_b64encode(key))
def changePassword(self, newPassword): self.mainPw = Cipher(self.checkAndGeneratePassword(newPassword)) key = bytes(self.mainPw.getDecrypted().encode("ASCII")) # init main math logic module self.f = Fernet(base64.urlsafe_b64encode(key))
class AddAccountFrame(QWidget): def __init__(self,accName="",fun=None,currentAccList=None): super().__init__() self.fun = fun self.currentAccList = currentAccList self.accName = accName # type: NagiosConnectionManager self.mainGrid = QGridLayout() # type: QGridLayout self.mainGrid.setSizeConstraint(QLayout.SetMinimumSize) self.wSize = 450 self.hSize = 180 self.setMinimumSize(self.wSize, self.hSize) self.setFixedSize(self.wSize, self.hSize) self.setWindowTitle("Adding account") self.password = Cipher("") self.populateGrid() self.account = None self.setLayout(self.mainGrid) self.state = States.UNDEFINE center(self) self.show() print("Show") self.setFocus(QtCore.Qt.ActiveWindowFocusReason) #--------------------------------------------------------------------------------------- #override closeEvent def closeEvent(self, event): if self.state == States.UNDEFINE: self.state = States.CLOSE event.accept() #--------------------------------------------------------------------------------------- def exitAction(self): self.state = States.CLOSE #--------------------------------------------------------------------------------------- def populateGrid(self): accNameLabel = QLabel("Account name: ") loginLabel = QLabel("Login: "******"Password1: ") passwordLabel2 = QLabel("Password2: ") checkBoxLabel = QLabel("Remember me: ") checkBoxLabel.setAlignment(QtCore.Qt.AlignLeft) self.loginEntry = QLineEdit() self.loginEntry.setPlaceholderText("login optional") self.accNameEntry = QLineEdit(self.accName) self.accNameEntry.setPlaceholderText("not optional") self.accNameEntry.textChanged.connect(self.nameEntryChanged) if len(self.accName) != 0: self.accNameEntry.setEnabled(False) self.passwordEntry1 = QLineEdit() self.passwordEntry1.setEchoMode(QLineEdit.Password) self.passwordEntry1.setPlaceholderText("password1 optional") self.passwordEntry2 = QLineEdit() self.passwordEntry2.setEchoMode(QLineEdit.Password) self.passwordEntry2.setPlaceholderText("password2 optional") self.passwordDBLabel = QLabel("PasswordDB:") self.passwordDBPasswordEntry = QLineEdit() self.passwordDBPasswordEntry.setEchoMode(QLineEdit.Password) self.passwordDBPasswordEntry.setMaxLength(32) self.passwordDBPasswordEntry.setPlaceholderText("password for encryption") self.rememberMeCheckBox = QCheckBox() self.rememberMeCheckBox.stateChanged.connect(lambda : self.checkBoxEvent()) if self.fun is not None: self.rememberMeCheckBox.setEnabled(False) self.loginButton = QPushButton("Start") self.loginButton.clicked.connect(lambda x: self.startButtonClicked()) self.mainGrid.addWidget(accNameLabel, 0, 0) self.mainGrid.addWidget(self.accNameEntry, 0, 1) self.mainGrid.addWidget(loginLabel,1,0) self.mainGrid.addWidget(self.loginEntry,1,1) self.mainGrid.addWidget(passwordLabel1,2,0) self.mainGrid.addWidget(self.passwordEntry1,2,1) self.mainGrid.addWidget(passwordLabel2,3,0) self.mainGrid.addWidget(self.passwordEntry2,3,1) self.mainGrid.addWidget(checkBoxLabel,4,0) self.mainGrid.addWidget(self.rememberMeCheckBox,4,1) self.mainGrid.addWidget(self.loginButton,5,0,2,2) #--------------------------------------------------------------------------------------- def nameEntryChanged(self,text): if self.currentAccList is not None: if True in [True if text == x else False for x in self.currentAccList]: self.loginButton.setEnabled(False) self.loginButton.setText("Account with this name already exist.") else: if not self.loginButton.isEnabled(): self.loginButton.setEnabled(True) self.loginButton.setText("Start") #--------------------------------------------------------------------------------------- def checkBoxEvent(self): if self.rememberMeCheckBox.checkState(): self.resize(self.wSize, (self.hSize+30)) self.setFixedSize(self.wSize, self.hSize+30) self.mainGrid.addWidget(self.passwordDBLabel,5,0) self.mainGrid.addWidget(self.passwordDBPasswordEntry,5,1) self.loginButton.setParent(None) self.mainGrid.addWidget(self.loginButton, 6, 0, 2, 2) else: self.resize(self.wSize, self.hSize) self.setFixedSize(self.wSize, self.hSize) self.passwordDBLabel.setParent(None) self.passwordDBPasswordEntry.setParent(None) self.mainGrid.addWidget(self.loginButton, 5, 0, 2, 2) if "Start" not in self.loginButton.text(): self.loginButton.setText("Start") #--------------------------------------------------------------------------------------- def startButtonClicked(self): if self.rememberMeCheckBox.checkState(): if len(self.passwordDBPasswordEntry.text()) == 0: self.loginButton.setText("PasswordDB field cannot be empty!") return None if len(self.accNameEntry.text()) == 0: self.loginButton.setText("Account name cannot be empty") return None self.setElementsEnableAtribute(False,"Ok") self.state = States.OK self.password = Cipher(self.passwordDBPasswordEntry.text()) self.account = Account(self.accNameEntry.text(),self.loginEntry.text(),self.passwordEntry1.text(),self.passwordEntry2.text()) if self.fun is not None: self.fun(self.getAccount()) self.close() #--------------------------------------------------------------------------------------- def getAccount(self): return self.account #--------------------------------------------------------------------------------------- # def checkCreds(self): # # boolValue = self.ncm.checkCredentials(self.loginEntry.text(), self.passwordEntry.text()) # boolValue = True # self.callbackAction(boolValue) # #--------------------------------------------------------------------------------------- # def callbackAction(self, boolValue): # if boolValue: # self.loginButton.setText("OK!") # if self.rememberMeCheckBox.checkState(): # self.pwDB.startFullInit(self.passwordDBPasswordEntry.text()) # acc = Account("Nagios",self.loginEntry.text(),self.passwordEntry1.text(),self.passwordEntry2.text()) # self.pwDB.addAccount(acc) # else: # self.setElementsEnableAtribute(True, "Try again") #--------------------------------------------------------------------------------------- def setElementsEnableAtribute(self,value,text): self.loginButton.setText(text) self.passwordEntry1.setEnabled(value) self.passwordEntry2.setEnabled(value) self.passwordDBPasswordEntry.setEnabled(value) self.rememberMeCheckBox.setEnabled(value) self.loginEntry.setEnabled(value) self.loginButton.setEnabled(value) #--------------------------------------------------------------------------------------- def getWindowState(self): return self.state #--------------------------------------------------------------------------------------- def getRememberMeValue(self): if self.rememberMeCheckBox.checkState() == 2: return True if self.rememberMeCheckBox.checkState() == 0: return False #--------------------------------------------------------------------------------------- def getPassword(self): return self.password.getDecrypted()