def attempt_decryption(self, keys): if self.decryption_keypage: try: response = requests.get(self.decryption_keypage) response.raise_for_status() keys_data = response.content.decode('utf-8') keys = keys + encryption.get_keys(keys_data) except Exception as e: print_error("Could not load decryption page {}: {}.".format( self.decryption_keypage, e)) print_error( "You can pass in a key directly by running python3 ok --decrypt [KEY]" ) decrypted_files = [] undecrypted_files = [] for file in self._get_files(): with open(file) as f: if not encryption.is_encrypted(f.read()): continue for key in keys: success = self._decrypt_file(file, key) if success: decrypted_files.append(file) break else: undecrypted_files.append(file) return decrypted_files, undecrypted_files
def encrypt(data): if encryption.is_encrypted(data): try: data = encryption.decrypt(data, key) except encryption.InvalidKeyException: raise ValueError("Attempt to re-encrypt file with an invalid key") return encryption.encrypt(data, key, padding)
def invalid_key_test(self): key1 = encryption.generate_key() key2 = encryption.generate_key() data = "test data 123" ciphertext = encryption.encrypt(data, key1) self.assertTrue(encryption.is_encrypted(ciphertext)) self.assertRaises(encryption.InvalidKeyException, lambda: encryption.decrypt(ciphertext, key2))
def decrypt(ciphertext): if not encryption.is_encrypted(ciphertext): return ciphertext try: plaintext = encryption.decrypt(ciphertext, key) nonlocal success success = True print_success("decrypted", path, "with", key) return plaintext except encryption.InvalidKeyException: return ciphertext
def load(file, parameter, assign): """Loads an OK-style test from a specified filepath. PARAMETERS: file -- str; a filepath to a Python module containing OK-style tests. RETURNS: Test """ filename, ext = os.path.splitext(file) if not os.path.isfile(file) or ext != '.py': log.info('Cannot import {} as an OK test'.format(file)) raise ex.LoadingException( 'Cannot import {} as an OK test'.format(file)) if os.path.exists(file): with open(file) as f: data = f.read() if encryption.is_encrypted(data): decrypted, _ = assign.attempt_decryption([]) if file not in decrypted: name = os.path.basename(filename) return {name: models.EncryptedOKTest(name=name, points=1)} try: test = importing.load_module(file).test test = copy.deepcopy(test) except Exception as e: raise ex.LoadingException('Error importing file {}: {}'.format( file, str(e))) name = os.path.basename(filename) try: return { name: models.OkTest(file, SUITES, assign.endpoint, assign, assign.cmd_args.verbose, assign.cmd_args.interactive, assign.cmd_args.timeout, **test) } except ex.SerializeException as e: raise ex.LoadingException('Cannot load OK test {}: {}'.format(file, e))
def assertInverses(self, data, padding=None): key = encryption.generate_key() ciphertext = encryption.encrypt(data, key, padding) self.assertTrue(encryption.is_encrypted(ciphertext)) self.assertEqual(encryption.decrypt(ciphertext, key), data)