def save_settings_to_file(self, password): """ This actually saves the settings to a file on the disk. The file is encrypted so you need to supply the password. :param password: masterpassword :type password: str """ salt = os.urandom(32) crypter = Crypter(salt, password) file = open(self.settings_file, 'bw') encrypted_sync_settings = crypter.encrypt( self.sync_manager.get_binary_sync_settings()) file.write( salt + struct.pack('!I', len(encrypted_sync_settings)) + encrypted_sync_settings + crypter.encrypt( Packer.compress(json.dumps(self.get_settings_as_dict())))) file.close() try: import win32con import win32api win32api.SetFileAttributes(self.settings_file, win32con.FILE_ATTRIBUTE_HIDDEN) except ImportError: pass
def test_get_export_data(self): settings = { 'settings': { 'unit.test': { 'domain': 'unit.test', 'length': 11, 'iterations': 5000, 'notes': 'Nice note!', 'salt': 'cGVwcGVy', 'usedCharacters': 'abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQRTUVWXYZ0123456789' + '#!"§$%&/()[]{}=-_+*<>;:.', 'cDate': '2011-02-12T11:07:31', 'mDate': '2011-02-12T11:07:32' }, 'some.domain': { 'domain': 'some.domain', 'length': 4, 'iterations': 4096, 'salt': 'cGVwcGVy', 'usedCharacters': '6478593021', 'cDate': '2013-06-17T04:03:41', 'mDate': '2014-08-02T10:37:12' } }, 'synced': [] } crypter = Crypter('xyz') f = open(os.path.expanduser('~/.ctSESAM_test.pws'), 'bw') f.write(crypter.encrypt(Packer.compress(json.dumps(settings).encode('utf-8')))) f.close() self.manager.load_settings_from_file('xyz') self.assertEqual( b64encode(b'\x00' + crypter.encrypt(Packer.compress(json.dumps(settings['settings']).encode('utf-8')))), self.manager.get_export_data('xyz') )
def get_export_data(self, password): """ This gives you a base64 encoded string of encrypted settings data (the blob). :param str password: :return: encrypted settings blob :rtype: str """ settings_list = self.get_settings_as_dict()['settings'] if self.remote_data: for domain_name in self.remote_data.keys(): data_set = self.remote_data[domain_name] if 'deleted' in data_set and data_set['deleted']: for i, setting_dict in enumerate(settings_list): if setting_dict['domain'] == setting_dict['domain'] and datetime.strptime( data_set['mDate'], "%Y-%m-%dT%H:%M:%S") > datetime.strptime( setting_dict['mDate'], "%Y-%m-%dT%H:%M:%S"): settings_list[i] = data_set if domain_name not in settings_list.keys(): settings_list[domain_name] = { 'mDate': datetime.now(), 'deleted': True } salt = os.urandom(32) crypter = Crypter(salt, password) return b64encode(b'\x00' + salt + crypter.encrypt(Packer.compress(json.dumps(settings_list))))
def get_export_data(self, password, salt=None): """ This gives you a base64 encoded string of encrypted settings data (the blob). :param password: masterpassword :type password: str :param salt: salt for the encryption: This is for testing only! Do not set it normally! :type salt: bytes :return: encrypted settings blob :rtype: str """ settings_list = self.get_settings_as_dict()['settings'] if self.remote_data: for domain_name in self.remote_data.keys(): data_set = self.remote_data[domain_name] if 'deleted' in data_set and data_set['deleted']: for i, setting_dict in enumerate(settings_list): if setting_dict['domain'] == setting_dict[ 'domain'] and datetime.strptime( data_set['mDate'], "%Y-%m-%dT%H:%M:%S") > datetime.strptime( setting_dict['mDate'], "%Y-%m-%dT%H:%M:%S"): settings_list[i] = data_set if domain_name not in settings_list.keys(): settings_list[domain_name] = { 'mDate': datetime.now(), 'deleted': True } if not salt: salt = os.urandom(32) crypter = Crypter(salt, password) return b64encode( b'\x00' + salt + crypter.encrypt(Packer.compress(json.dumps(settings_list))))
def test_load_settings_from_file(self): settings = { 'settings': { 'unit.test': { 'domain': 'unit.test', 'length': 11, 'iterations': 5000, 'notes': 'Nice note!', 'cDate': '2011-02-12T11:07:31', 'mDate': '2011-02-12T11:07:32' }, 'some.domain': { 'domain': 'some.domain', 'length': 4, 'usedCharacters': '6478593021', 'cDate': '2013-06-17T04:03:41', 'mDate': '2014-08-02T10:37:12' } }, 'synced': [] } salt = os.urandom(32) crypter = Crypter(salt, 'xyz') f = open(os.path.expanduser('~/.ctSESAM_test.pws'), 'bw') f.write(salt + struct.pack('!I', 0) + crypter.encrypt(Packer.compress(json.dumps(settings).encode('utf-8')))) f.close() self.manager.load_settings_from_file('xyz') self.assertIn('unit.test', self.manager.get_domain_list()) self.assertIn('some.domain', self.manager.get_domain_list()) self.assertEqual(11, self.manager.get_setting('unit.test').get_length()) self.assertEqual(5000, self.manager.get_setting('unit.test').get_iterations()) self.assertEqual('Nice note!', self.manager.get_setting('unit.test').get_notes()) self.assertEqual(4, self.manager.get_setting('some.domain').get_length()) self.assertEqual('6478593021', self.manager.get_setting('some.domain').get_character_set())
def test_get_export_data(self): settings = { 'settings': { 'unit.test': { 'domain': 'unit.test', 'length': 11, 'iterations': 5000, 'notes': 'Nice note!', 'salt': 'cGVwcGVy', 'usedCharacters': 'abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQRTUVWXYZ0123456789' + '#!"§$%&/()[]{}=-_+*<>;:.', 'cDate': '2011-02-12T11:07:31', 'mDate': '2011-02-12T11:07:32' }, 'some.domain': { 'domain': 'some.domain', 'length': 4, 'iterations': 4096, 'salt': 'cGVwcGVy', 'usedCharacters': '6478593021', 'cDate': '2013-06-17T04:03:41', 'mDate': '2014-08-02T10:37:12' } }, 'synced': [] } crypter = Crypter('xyz') f = open(os.path.expanduser('~/.ctSESAM_test.pws'), 'bw') f.write( crypter.encrypt( Packer.compress(json.dumps(settings).encode('utf-8')))) f.close() self.manager.load_settings_from_file('xyz') self.assertEqual( b64encode(b'\x00' + crypter.encrypt( Packer.compress( json.dumps(settings['settings']).encode('utf-8')))), self.manager.get_export_data('xyz'))
def encrypt(): # get the raw post data request.get_data() data = request.data # create the crypter object crypter = Crypter(Config.MASTER_SECRET) # encrypt and return a string holding the ciphertext, iv and salt return crypter.encrypt(data)
def save_settings_to_file(self, password): """ This actually saves the settings to a file on the disk. The file is encrypted so you need to supply the password. :param password: masterpassword :type password: str """ salt = os.urandom(32) crypter = Crypter(salt, password) file = open(self.settings_file, 'bw') encrypted_sync_settings = crypter.encrypt(self.sync_manager.get_binary_sync_settings()) file.write(salt + struct.pack('!I', len(encrypted_sync_settings)) + encrypted_sync_settings + crypter.encrypt(Packer.compress(json.dumps(self.get_settings_as_dict())))) file.close() try: import win32con import win32api win32api.SetFileAttributes(self.settings_file, win32con.FILE_ATTRIBUTE_HIDDEN) except ImportError: pass
def test_encrypt(self): message_string = "Important information with quite some length. " + \ "This message is as long as this because otherwise only one cipher block would " + \ "be encrypted. This long message insures that more than one block is needed." password = "******" message = message_string.encode('utf-8') crypter = Crypter("pepper".encode('utf-8'), password) ciphertext = crypter.encrypt(message) self.assertEqual( b'1lsDkebMaZZeO+/DnvVAUYPmXrQOdCAFQ79C3sElpwamOLtX444tRMiecg4/a9394w51dbmKo89CYKpw19nOaKkbF8Dy' + b'll9MQJSRUXZEoc3aoaBvgGBCy4rVM62hEQLfpOUdBcrJTPAU3l8zM8V+AN560z7Rj9gXoGkXsotIpEjNg0+hwdmcVRAw' + b'JAiDnAbH7K1Q0olPdkM187tbF5A9OEzCU5M36qzUyr/68a1oGL65JCaMAGoHTpQa2i4DlTEkVF1xPkB40ZF167jo360lEQ==', b64encode(ciphertext))
def test_encrypt(self): message_string = "Important information with quite some length. " + \ "This message is as long as this because otherwise only one cipher block would " + \ "be encrypted. This long message insures that more than one block is needed." password = "******" message = message_string.encode('utf-8') crypter = Crypter(password) ciphertext = crypter.encrypt(message) self.assertEqual( b'FRQFCWa38eSIrPnhELojAPrOb8oKzs2yoAbNqVONBEuac3OhUKY12mP+TNyZs1MRUbY9hnqvIG18' + b'7MqTAVTzI0fCJhmR4stc/k4YpS+HptmzcTgEfXeli56davPUkmJ59yz2vvF3t/pCUOk0qWNQ2vv9' + b'dU2sJhvOdQ7RVKzbw2DJAFtEM2BxJq8Oqa4mB4sBC/GpIP3xtNxANJPyN8xTSL2F4Ktt5hIcX3AV' + b'UrnGYSjGeDHGua8iKNFohYtaPj3vvzaSVpGyzAfmlVEdN5/8zQ==', b64encode(ciphertext))
def save_settings_to_file(self, password): """ This actually saves the settings to a file on the disk. The file is encrypted so you need to supply the password. :param str password: """ salt = os.urandom(32) crypter = Crypter(salt, password) file = open(self.settings_file, 'bw') file.write(salt + crypter.encrypt(Packer.compress(json.dumps(self.get_settings_as_dict())))) file.close() try: import win32con import win32api win32api.SetFileAttributes(self.settings_file, win32con.FILE_ATTRIBUTE_HIDDEN) except ImportError: pass
def save_settings_to_file(self, password): """ This actually saves the settings to a file on the disk. The file is encrypted so you need to supply the password. :param str password: """ salt = os.urandom(32) crypter = Crypter(salt, password) file = open(self.settings_file, 'bw') file.write(salt + crypter.encrypt( Packer.compress(json.dumps(self.get_settings_as_dict())))) file.close() try: import win32con import win32api win32api.SetFileAttributes(self.settings_file, win32con.FILE_ATTRIBUTE_HIDDEN) except ImportError: pass
def test_update_from_sync(self): settings = { 'settings': { 'unit.test': { 'domain': 'unit.test', 'length': 11, 'iterations': 5000, 'notes': 'Nice note!', 'salt': 'cGVwcGVy', 'usedCharacters': 'abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQRTUVWXYZ0123456789' + '#!"§$%&/()[]{}=-_+*<>;:.', 'cDate': '2011-02-12T11:07:31', 'mDate': '2011-02-12T11:07:32' }, 'some.domain': { 'domain': 'some.domain', 'length': 4, 'iterations': 4096, 'salt': 'cGVwcGVy', 'usedCharacters': '6478593021', 'cDate': '2013-06-17T04:03:41', 'mDate': '2014-08-02T10:37:12' } }, 'synced': [] } salt = os.urandom(32) crypter = Crypter(salt, 'xyz') f = open(os.path.expanduser('~/.ctSESAM_test.pws'), 'bw') f.write(salt + struct.pack('!I', 0) + crypter.encrypt( Packer.compress(json.dumps(settings).encode('utf-8')))) f.close() self.manager.load_settings_from_file('xyz') self.manager.sync_manager = MockSyncManager() self.manager.update_from_sync('xyz') self.assertIn('unit.test', self.manager.get_domain_list()) self.assertIn('some.domain', self.manager.get_domain_list()) self.assertIn('third.domain', self.manager.get_domain_list()) self.assertEqual(5001, self.manager.get_setting('unit.test').get_iterations()) self.assertEqual(4096, self.manager.get_setting('some.domain').get_iterations()) self.assertEqual(4098, self.manager.get_setting('third.domain').get_iterations())
def pull(self): """ Returns some mock data tor the sync test. :return: base64 mock data blob :rtype: (bool, str) """ remote_data = { 'unit.test': { 'domain': 'unit.test', 'length': 12, 'iterations': 5001, 'notes': 'another note!', 'salt': 'cGVwcGVy', 'usedCharacters': 'abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQRTUVWXYZ0123456789', 'cDate': '2011-02-12T11:07:31', 'mDate': '2013-07-12T14:46:11' }, 'some.domain': { 'domain': 'some.domain', 'length': 4, 'iterations': 4097, 'salt': 'cGVwcGVy', 'usedCharacters': '6478593021', 'cDate': '2013-06-17T04:03:41', 'mDate': '2014-08-02T10:37:11' }, 'third.domain': { 'domain': 'third.domain', 'length': 10, 'iterations': 4098, 'salt': 'cGVwcGVy', 'usedCharacters': 'aeiou', 'cDate': '2013-06-17T04:03:41', 'mDate': '2014-08-02T10:37:11' } } salt = os.urandom(32) crypter = Crypter(salt, 'xyz') return True, str(b64encode(b'\x00' + salt + crypter.encrypt( Packer.compress(json.dumps(remote_data).encode('utf-8')))), encoding='utf-8')