def __init__(self, cache_file, password): self._cache_file = cache_file self._password = password if not os.path.isfile(cache_file) or os.path.getsize(cache_file) < 2: # initialize a non-existent or a nearly-empty file with open(cache_file, 'w') as f: f.write(encrypt("{}", password))
def _update_data(self, process_data): with open(self._cache_file, 'r+') as f: decrypted = decrypt_file(f, self._password) json_data = json.loads(decrypted) process_data(json_data) encrypted = encrypt(json.dumps(json_data), self._password) f.seek(0) f.truncate() f.write(encrypted)
def _update_data(self, process_data): with open(self._cache_file, 'r+') as f: decrypted = decrypt_file(f, self._password) json_data = json.loads(decrypted) process_data(json_data) encrypted = encrypt( json.dumps(json_data), self._password) f.seek(0) f.truncate() f.write(encrypted)
def test_crypt_str(): for key, plain, cipher in plain_cipher_pairs: assert encrypt(plain, key) == cipher assert decrypt(cipher, key) == plain