def testEncryptDecrypt(self): temp_file_path = os.path.join(os.path.dirname(__file__), "data/cfile") f = open(temp_file_path, 'w') f.write("foobar") f.close() key = Crypto.generate_key("secret_key") Crypto.encrypt_file(key, temp_file_path) self.assertTrue(os.path.exists(temp_file_path + ".enc")) contents = FileManager.read_file(temp_file_path + ".enc") # TODO should do a better verification that it's actually encrypted properly self.assertTrue(contents != "foobar") os.remove(temp_file_path) Crypto.decrypt_file(key, temp_file_path + ".enc") contents = FileManager.read_file(temp_file_path) self.assertTrue(contents == "foobar") os.remove(temp_file_path) os.remove(temp_file_path + ".enc")
def extract(self): '''extract the snapfile into the snapdirectory @raises - MissingFileError if the snapfile does not exist ''' # decrypt the file if we've set a key if not snap.osregistry.OS.is_windows() and self.encryption_key != None: if snap.config.options.log_level_at_least('verbose'): snap.callback.snapcallback.message("Decyrpting snapfile") Crypto.decrypt_file(self.encryption_key, self.snapfile, self.snapfile + ".dec") FileManager.mv(self.snapfile + ".dec", self.snapfile) # open the tarball tarball = tarfile.open(self.snapfile) # temp store the working directory, before changing to the snapdirectory cwd = os.getcwd() os.chdir(self.snapdirectory) # extract files from it for tarinfo in tarball: tarball.extract(tarinfo) # close it out tarball.close() if snap.config.options.log_level_at_least('normal'): snap.callback.snapcallback.message("Snapfile " + self.snapfile + " opened") # restore the working directory os.chdir(cwd)