Ejemplo n.º 1
0
 def setUp(self):
     file_fp, file_path = mkstemp()
     self.password = '******'
     self.crypto = Crypto(self.password)
     self.file_path = file_path
     self.file_entry = FileEntry.from_file(file_path,
                                           os.path.basename(file_path))
     os.close(file_fp)
Ejemplo n.º 2
0
 def setUp(self):
     file_fp, file_path = mkstemp()
     self.password = "******"
     self.crypto = Crypto(self.password)
     self.file_path = file_path
     self.file_entry = FileEntry.from_file(file_path, os.path.basename(file_path))
     os.close(file_fp)
 def sync_folders(folder):
     if not DataEncryptionHandler.encryption_enabled():
         return
     encrypted_folder = DataEncryptionHandler.compute_encrypted_folder_path(
         folder)
     crypto_pass = os.environ[
         DataEncryptionHandler.
         CRYPTO_PASS] if DataEncryptionHandler.CRYPTO_PASS in os.environ else None
     if crypto_pass is None:
         raise TVBException(
             "Storage encryption/decryption is not possible because password is not provided."
         )
     crypto = Crypto(crypto_pass)
     syncro = Syncrypto(crypto, encrypted_folder, folder)
     syncro.sync_folder()
     trash_path = os.path.join(encrypted_folder, "_syncrypto", "trash")
     if os.path.exists(trash_path):
         shutil.rmtree(trash_path)
Ejemplo n.º 4
0
    def sync_folders(folder):
        if not DataEncryptionHandler.encryption_enabled():
            return

        project_name = os.path.basename(folder)
        encrypted_folder = DataEncryptionHandler.compute_encrypted_folder_path(
            folder)

        if os.path.exists(encrypted_folder) or os.path.exists(folder):
            crypto_pass = DataEncryptionHandler._project_key(project_name)
            crypto = Crypto(crypto_pass)
            syncro = Syncrypto(crypto, encrypted_folder, folder)
            syncro.sync_folder()
            trash_path = os.path.join(encrypted_folder, "_syncrypto", "trash")
            if os.path.exists(trash_path):
                shutil.rmtree(trash_path)
        else:
            LOGGER.info("Project {} was deleted".format(project_name))
Ejemplo n.º 5
0
 def setUp(self):
     self.crypto = Crypto('password')
     self.plain_folder = mkdtemp()
     self.plain_folder_check = mkdtemp()
     self.encrypted_folder = mkdtemp()
     prepare_filetree(
         self.plain_folder, '''
         sync_file_modify:hello world
         sync_file_delete:delete
         sync/file/modify:hello world
         empty_dir_delete/
         not_empty_dir/dir2/dir3/file
         dir2/file2
     ''')
     self.plain_tree = self.plain_tree = FileTree.from_fs(self.plain_folder)
     self.plain_tree_check = FileTree()
     self.encrypted_tree = FileTree()
     self.snapshot_tree = FileTree()
Ejemplo n.º 6
0
class CryptoTestCase(unittest.TestCase):
    def setUp(self):
        file_fp, file_path = mkstemp()
        self.password = '******'
        self.crypto = Crypto(self.password)
        self.file_path = file_path
        self.file_entry = FileEntry.from_file(file_path,
                                              os.path.basename(file_path))
        os.close(file_fp)

    def tearDown(self):
        os.remove(self.file_path)

    def test_basic_encrypt(self):
        in_fd = BytesIO()
        middle_fd = BytesIO()
        out_fd = BytesIO()
        in_fd.write(b"hello")
        in_fd.seek(0)
        self.crypto.encrypt_fd(in_fd, middle_fd, self.file_entry)
        middle_fd.seek(0)
        self.crypto.decrypt_fd(middle_fd, out_fd)
        self.assertEqual(in_fd.getvalue(), out_fd.getvalue())

    def test_file_api(self):
        fd1, file_path1 = mkstemp()
        fd2, file_path2 = mkstemp()
        fd3, file_path3 = mkstemp()
        os.write(fd1, b'hello world')
        os.close(fd1)
        os.close(fd2)
        os.close(fd3)
        self.crypto.encrypt_file(file_path1, file_path2, self.file_entry)
        self.crypto.decrypt_file(file_path2, file_path3)
        self.assertEqual(open(file_path3, 'rb').read(), b'hello world')
        os.remove(file_path1)
        os.remove(file_path2)
        os.remove(file_path3)

    def test_large_encrypt(self):
        in_fd = BytesIO()
        middle_fd = BytesIO()
        out_fd = BytesIO()
        in_fd.write(os.urandom(1024 * 1024))
        in_fd.seek(0)
        self.crypto.encrypt_fd(in_fd, middle_fd, self.file_entry)
        middle_fd.seek(0)
        self.crypto.decrypt_fd(middle_fd, out_fd)
        self.assertEqual(in_fd.getvalue(), out_fd.getvalue())

    def test_encrypt_twice(self):
        in_fd = BytesIO()
        out_fd1 = BytesIO()
        out_fd2 = BytesIO()
        in_fd.write(b"hello")
        in_fd.seek(0)
        self.crypto.encrypt_fd(in_fd, out_fd1, self.file_entry)
        in_fd.seek(0)
        self.crypto.encrypt_fd(in_fd, out_fd2, self.file_entry)
        self.assertEqual(out_fd1.getvalue(), out_fd2.getvalue())

    def test_repeat_encrypt(self):
        in_fd = BytesIO()
        out_fd1 = BytesIO()
        out_fd2 = BytesIO()
        in_fd.write(os.urandom(1024))
        in_fd.seek(0)
        self.crypto.encrypt_fd(in_fd, out_fd1, self.file_entry)
        in_fd.seek(0)
        self.crypto.encrypt_fd(in_fd, out_fd2, self.file_entry)
        self.assertEqual(out_fd1.getvalue(), out_fd2.getvalue())

    def test_compress(self):
        in_fd = BytesIO()
        middle_fd = BytesIO()
        out_fd = BytesIO()
        in_fd.write(os.urandom(1024))
        in_fd.seek(0)
        self.crypto.encrypt_fd(in_fd, middle_fd, self.file_entry,
                               Crypto.COMPRESS)
        middle_fd.seek(0)
        self.crypto.decrypt_fd(middle_fd, out_fd)
        self.assertEqual(in_fd.getvalue(), out_fd.getvalue())
Ejemplo n.º 7
0
class CryptoTestCase(unittest.TestCase):
    def setUp(self):
        file_fp, file_path = mkstemp()
        self.password = "******"
        self.crypto = Crypto(self.password)
        self.file_path = file_path
        self.file_entry = FileEntry.from_file(file_path, os.path.basename(file_path))
        os.close(file_fp)

    def tearDown(self):
        os.remove(self.file_path)

    def test_basic_encrypt(self):
        in_fd = BytesIO()
        middle_fd = BytesIO()
        out_fd = BytesIO()
        in_fd.write(b"hello")
        in_fd.seek(0)
        self.crypto.encrypt_fd(in_fd, middle_fd, self.file_entry)
        middle_fd.seek(0)
        self.crypto.decrypt_fd(middle_fd, out_fd)
        self.assertEqual(in_fd.getvalue(), out_fd.getvalue())

    def test_file_api(self):
        fd1, file_path1 = mkstemp()
        fd2, file_path2 = mkstemp()
        fd3, file_path3 = mkstemp()
        os.write(fd1, b"hello world")
        os.close(fd1)
        os.close(fd2)
        os.close(fd3)
        self.crypto.encrypt_file(file_path1, file_path2, self.file_entry)
        self.crypto.decrypt_file(file_path2, file_path3)
        self.assertEqual(open(file_path3, "rb").read(), b"hello world")
        os.remove(file_path1)
        os.remove(file_path2)
        os.remove(file_path3)

    def test_large_encrypt(self):
        in_fd = BytesIO()
        middle_fd = BytesIO()
        out_fd = BytesIO()
        in_fd.write(os.urandom(1024 * 1024))
        in_fd.seek(0)
        self.crypto.encrypt_fd(in_fd, middle_fd, self.file_entry)
        middle_fd.seek(0)
        self.crypto.decrypt_fd(middle_fd, out_fd)
        self.assertEqual(in_fd.getvalue(), out_fd.getvalue())

    def test_encrypt_twice(self):
        in_fd = BytesIO()
        out_fd1 = BytesIO()
        out_fd2 = BytesIO()
        in_fd.write(b"hello")
        in_fd.seek(0)
        self.crypto.encrypt_fd(in_fd, out_fd1, self.file_entry)
        in_fd.seek(0)
        self.crypto.encrypt_fd(in_fd, out_fd2, self.file_entry)
        self.assertEqual(out_fd1.getvalue(), out_fd2.getvalue())

    def test_repeat_encrypt(self):
        in_fd = BytesIO()
        out_fd1 = BytesIO()
        out_fd2 = BytesIO()
        in_fd.write(os.urandom(1024 * 1024))
        in_fd.seek(0)
        self.crypto.encrypt_fd(in_fd, out_fd1, self.file_entry)
        in_fd.seek(0)
        self.crypto.encrypt_fd(in_fd, out_fd2, self.file_entry)
        self.assertEqual(out_fd1.getvalue(), out_fd2.getvalue())

    def test_compress(self):
        in_fd = BytesIO()
        middle_fd = BytesIO()
        out_fd = BytesIO()
        in_fd.write(os.urandom(1024 * 1024))
        in_fd.seek(0)
        self.crypto.compress_fd(in_fd, middle_fd)
        middle_fd.seek(0)
        self.crypto.decompress_fd(middle_fd, out_fd)
        self.assertEqual(in_fd.getvalue(), out_fd.getvalue())

    def test_encrypted_compress(self):
        in_fd = BytesIO()
        middle_fd = BytesIO()
        out_fd = BytesIO()
        in_fd.write(os.urandom(1024))
        in_fd.seek(0)
        self.crypto.encrypt_fd(in_fd, middle_fd, self.file_entry, Crypto.COMPRESS)
        middle_fd.seek(0)
        self.crypto.decrypt_fd(middle_fd, out_fd)
        self.assertEqual(in_fd.getvalue(), out_fd.getvalue())

    def test_encrypted_compress_large(self):
        in_fd = BytesIO()
        middle_fd = BytesIO()
        out_fd = BytesIO()
        data = "\n".join([str(x) for x in range(1000000, 1000000 + 8 * 1024 + 39)])
        in_fd.write(data.encode("ascii"))
        in_fd.seek(0)
        self.crypto.encrypt_fd(in_fd, middle_fd, self.file_entry, Crypto.COMPRESS)
        middle_fd.seek(0)
        self.crypto.decrypt_fd(middle_fd, out_fd)
        self.assertEqual(in_fd.getvalue(), out_fd.getvalue())
Ejemplo n.º 8
0
class CryptoTestCase(unittest.TestCase):

    def setUp(self):
        file_fp, file_path = mkstemp()
        self.password = '******'
        self.crypto = Crypto(self.password)
        self.file_path = file_path
        self.file_entry = FileEntry.from_file(file_path,
                                              os.path.basename(file_path))
        os.close(file_fp)

    def tearDown(self):
        os.remove(self.file_path)

    def test_basic_encrypt(self):
        in_fd = BytesIO()
        middle_fd = BytesIO()
        out_fd = BytesIO()
        in_fd.write(b"hello")
        in_fd.seek(0)
        self.crypto.encrypt_fd(in_fd, middle_fd, self.file_entry)
        middle_fd.seek(0)
        self.crypto.decrypt_fd(middle_fd, out_fd)
        self.assertEqual(in_fd.getvalue(), out_fd.getvalue())

    def test_file_api(self):
        fd1, file_path1 = mkstemp()
        fd2, file_path2 = mkstemp()
        fd3, file_path3 = mkstemp()
        os.write(fd1, b'hello world')
        os.close(fd1)
        os.close(fd2)
        os.close(fd3)
        self.crypto.encrypt_file(file_path1, file_path2, self.file_entry)
        self.crypto.decrypt_file(file_path2, file_path3)
        self.assertEqual(open(file_path3, 'rb').read(), b'hello world')
        os.remove(file_path1)
        os.remove(file_path2)
        os.remove(file_path3)

    def test_large_encrypt(self):
        in_fd = BytesIO()
        middle_fd = BytesIO()
        out_fd = BytesIO()
        in_fd.write(os.urandom(1024*1024))
        in_fd.seek(0)
        self.crypto.encrypt_fd(in_fd, middle_fd, self.file_entry)
        middle_fd.seek(0)
        self.crypto.decrypt_fd(middle_fd, out_fd)
        self.assertEqual(in_fd.getvalue(), out_fd.getvalue())

    def test_encrypt_twice(self):
        in_fd = BytesIO()
        out_fd1 = BytesIO()
        out_fd2 = BytesIO()
        in_fd.write(b"hello")
        in_fd.seek(0)
        self.crypto.encrypt_fd(in_fd, out_fd1, self.file_entry)
        in_fd.seek(0)
        self.crypto.encrypt_fd(in_fd, out_fd2, self.file_entry)
        self.assertEqual(out_fd1.getvalue(), out_fd2.getvalue())

    def test_repeat_encrypt(self):
        in_fd = BytesIO()
        out_fd1 = BytesIO()
        out_fd2 = BytesIO()
        in_fd.write(os.urandom(1024))
        in_fd.seek(0)
        self.crypto.encrypt_fd(in_fd, out_fd1, self.file_entry)
        in_fd.seek(0)
        self.crypto.encrypt_fd(in_fd, out_fd2, self.file_entry)
        self.assertEqual(out_fd1.getvalue(), out_fd2.getvalue())

    def test_compress(self):
        in_fd = BytesIO()
        middle_fd = BytesIO()
        out_fd = BytesIO()
        in_fd.write(os.urandom(1024))
        in_fd.seek(0)
        self.crypto.encrypt_fd(in_fd, middle_fd, self.file_entry,
                               Crypto.COMPRESS)
        middle_fd.seek(0)
        self.crypto.decrypt_fd(middle_fd, out_fd)
        self.assertEqual(in_fd.getvalue(), out_fd.getvalue())
Ejemplo n.º 9
0
from time import time

from syncrypto import Crypto, Syncrypto

bufferSize = 64 * 1024
password = "******"

folder = "/Users/bvalean/TVB/PROJECTS/Default_Project"
folder_encrypted = "/Users/bvalean/WORK/test-encryption/Default_Project_encrypted"
folder_decrypted = "/Users/bvalean/WORK/test-encryption/Default_Project_decrypted"


if __name__ == '__main__':
    print("========== ENCRYPTION ==========")
    crypto = Crypto(password)
    syncro1 = Syncrypto(crypto,folder_encrypted,folder)
    t = time()
    syncro1.sync_folder()
    print("========== FINISH ENCRYPTION ==========")
    encryption = time()-t

    print("========== DECRYPTION ==========")
    syncro1 = Syncrypto(crypto,folder_encrypted,folder_decrypted)
    t = time()
    syncro1.sync_folder()
    print("========== FINISH DECRYPTION ==========")
    decryption = time() - t

    print("========== RESULTS ==========")
    print("Ecrypted: {} seconds".format(encryption))
    print("Decrypted: {} seconds".format(decryption))