def _save_pass_file(self, passwords): """ Encrypt and save a Json file containing the passwords :param passwords: List of the passwords to write :type passwords: Keychain """ json_passwords = passwords.to_json() c = Cryptography() crypted_passwords = c.encrypt(json_passwords, self._master_password) with open(self._file_path, 'wb') as file: file.write(crypted_passwords)
class TestCryptography(TestCase): def setUp(self): self.c = Cryptography() self.msg = "My dirty secret message." self.key = "key1234" def test_validate(self): encrypted = self.c.encrypt(self.msg, self.key) self.assertTrue(self.c.validate(encrypted, self.key), "Validation of a correct key.") self.assertFalse(self.c.validate(encrypted, "wrong_key"), "Validation of an incorrect key.") def test_encrypt(self): encrypted = self.c.encrypt(self.msg, self.key) self.assertNotEqual(self.msg, encrypted, "The encrypted message should be different than " "the original.") self.assertEqual(self.msg, self.c.decrypt(encrypted, self.key), "We should get the original message after decryption.") def test_decrypt(self): encrypted = self.c.encrypt(self.msg, self.key) self.assertEqual(self.msg, self.c.decrypt(encrypted, self.key), "The message should stay the same after encryption " "and decryption.")
def _load_pass_file(self): """ Loads passwords from the crypted Json file :return: List of the passwords in the file :rtype : Keychain """ with open(self._file_path, 'rb') as file: file_crypted = file.read() c = Cryptography() correct_password = False while not correct_password: if self._master_password is not None: try: correct_password = c.validate(file_crypted, self._master_password) except CorruptedError: print(_("mdp: Error: The file '{filename}' seems to be" "corrupted.").format(filename=self._file_path), file=sys.stderr) sys.exit(1) if not correct_password: if self._master_password is not None: print(_("Wrong password, try again.")) self._master_password = getpass(prompt=_("Password: "******"mdp: Error: Unable to decrypt the file. {error}") .format(error=e.__str__()), file=sys.stderr) sys.exit(1) try: passwords = Keychain(file_decrypted) except ValueError as e: print(_("mdp: Error: Unable to parse the file. {error}") .format(error=e.__str__()), file=sys.stderr) sys.exit(1) return passwords
def setUp(self): self.c = Cryptography() self.msg = "My dirty secret message." self.key = "key1234"
def __init__(self, private_key, public_key, P): self.crypto_obj = Cryptography(private_key, public_key, P) self.objects = []