Exemplo n.º 1
0
 def test_constructor_creates_aes_if_none(self):
     file_structure = {'folder1': {}}
     add_files(file_structure)
     cfg = Config()
     cfg.aes_dir = 'folder1'
     AesLayer(cfg)
     assert (len(os.listdir('folder1')) == 1)
Exemplo n.º 2
0
 def test_rsa_pub_is_read(self, mocker):
     mocker.spy(lib.aes_layer.lib_rsa.Rsa, '__init__')
     cfg = Config()
     cfg.aes_dir = AES_DIR
     AesLayer(cfg)
     cfg.public_key = RSA_PUB_FILE
     AesLayer(cfg)
     rsa = lib.aes_layer.lib_rsa.Rsa.__init__
     assert (rsa.call_args_list[0][1] == {
         'passphrase': None,
         'private_key': None,
         'public_key': None
     })
     assert (rsa.call_args_list[1][1] == {
         'passphrase': None,
         'private_key': None,
         'public_key': RSA_PUB
     })
Exemplo n.º 3
0
 def test_constructor_creates_aes_if_cant_decrypt(self):
     file_structure = {'folder1': {AES_FILENAME: None}}
     add_files(file_structure)
     with mock.patch(
             'lib.aes_layer.lib_rsa.Rsa.can_decrypt') as can_decrypt:
         can_decrypt.return_value = False
         cfg = Config()
         cfg.aes_dir = 'folder1'
         AesLayer(cfg)
         assert (len(os.listdir('folder1')) == 2)
Exemplo n.º 4
0
 def test_constructor_invalid_config(self):
     with pytest.raises(ValueError):
         AesLayer(None)
Exemplo n.º 5
0
 def setup(self):
     self.aes_filename = AES_FILENAME
     self.aes_layer = AesLayer(CONFIG)
     self.mock_aes = mock_aes.Aes(None)
Exemplo n.º 6
0
class TestAesLayer:
    def setup(self):
        self.aes_filename = AES_FILENAME
        self.aes_layer = AesLayer(CONFIG)
        self.mock_aes = mock_aes.Aes(None)

    def test_constructor_uses_aes(self):
        assert (len(os.listdir(AES_FOLDER)) == 1)

    def test_constructor_creates_aes_if_none(self):
        file_structure = {'folder1': {}}
        add_files(file_structure)
        cfg = Config()
        cfg.aes_dir = 'folder1'
        AesLayer(cfg)
        assert (len(os.listdir('folder1')) == 1)

    def test_constructor_creates_aes_if_cant_decrypt(self):
        file_structure = {'folder1': {AES_FILENAME: None}}
        add_files(file_structure)
        with mock.patch(
                'lib.aes_layer.lib_rsa.Rsa.can_decrypt') as can_decrypt:
            can_decrypt.return_value = False
            cfg = Config()
            cfg.aes_dir = 'folder1'
            AesLayer(cfg)
            assert (len(os.listdir('folder1')) == 2)

    def test_constructor_invalid_config(self):
        with pytest.raises(ValueError):
            AesLayer(None)

    def test_encode(self):
        data = b'1234'
        res = self.aes_layer.do_encode(data=data)
        assert (len(res) == 2)
        assert (res['data'] == self.mock_aes.encrypt(data))
        assert (res['aes_filename'] == AES_FILENAME)

    def test_decode(self):
        data = b'12345'
        res = self.aes_layer.do_decode(data=data, aes_filename=AES_FILENAME)
        assert (res['data'] == self.mock_aes.decrypt(data))

    def test_rsa_priv_is_read(self, mocker):
        mocker.spy(lib.aes_layer.lib_rsa.Rsa, '__init__')
        cfg = Config()
        cfg.aes_dir = AES_DIR
        AesLayer(cfg)
        cfg.private_key = RSA_PRIV_FILE
        AesLayer(cfg)
        AesLayer(cfg, PASSPHRASE)
        rsa = lib.aes_layer.lib_rsa.Rsa.__init__
        assert (rsa.call_args_list[0][1] == {
            'passphrase': None,
            'private_key': None,
            'public_key': None
        })
        assert (rsa.call_args_list[1][1] == {
            'passphrase': None,
            'private_key': RSA_PRIV,
            'public_key': None
        })
        assert (rsa.call_args_list[2][1] == {
            'passphrase': PASSPHRASE,
            'private_key': RSA_PRIV,
            'public_key': None
        })

    def test_rsa_pub_is_read(self, mocker):
        mocker.spy(lib.aes_layer.lib_rsa.Rsa, '__init__')
        cfg = Config()
        cfg.aes_dir = AES_DIR
        AesLayer(cfg)
        cfg.public_key = RSA_PUB_FILE
        AesLayer(cfg)
        rsa = lib.aes_layer.lib_rsa.Rsa.__init__
        assert (rsa.call_args_list[0][1] == {
            'passphrase': None,
            'private_key': None,
            'public_key': None
        })
        assert (rsa.call_args_list[1][1] == {
            'passphrase': None,
            'private_key': None,
            'public_key': RSA_PUB
        })
Exemplo n.º 7
0
 def __init__(self, config, passphrase=None):
     self.aes_layer = AesLayer(config, passphrase)
     self.header_layer = HeaderLayer()
     self.aes_layer.attach(self.header_layer)
Exemplo n.º 8
0
class Cipher:
    def __init__(self, config, passphrase=None):
        self.aes_layer = AesLayer(config, passphrase)
        self.header_layer = HeaderLayer()
        self.aes_layer.attach(self.header_layer)
        #self.hidden_layer = HiddenLayer()
        #self.hidden_layer.attach(self.aes_layer)

    def encrypt_file(self, path):
        """Encrypts a file/folder
        """
        for obj in parse_fs(path):
            self.encrypt_regular_filenames(obj.root, obj.regular_filenames)
            self.encrypt_hidden_filenames(obj.root, obj.hidden_filenames)

    def encrypt_regular_filenames(self, path, regular_filenames):
        for filename in regular_filenames:
            filepath = os.path.join(path, filename)
            path_enc = filepath+'.enc'
            self.cipher_and_move(filepath, path_enc, self.aes_layer.do_encode)

    def cipher_and_move(self, filepath, path_enc, fn):
        if os.path.isfile(path_enc):
            logger.info('%s already exists, refusing' % path_enc)
            return
        
        with open(path_enc, 'wb') as fout:
            with open(filepath, 'rb') as fin:
                out_data = fn(data=fin.read())
                fout.write(out_data['data'])
        _copy_modified_time(filepath, path_enc)
        os.remove(filepath)

    def encrypt_hidden_filenames(self, path, hidden_filenames):
        if len(hidden_filenames):
            hidden_dir = os.path.join(path, HIDDEN_FILE)
            os.mkdir(hidden_dir)
            for filename in hidden_filenames:
                filepath = os.path.join(path, filename)
                os.rename(filepath, os.path.join(hidden_dir, filename))
            tmp_file = hidden_dir+'.tmp'
            with open(tmp_file, 'wb') as fout:
                fout.write(b'1')#self.hidden_layer.encode(hidden_dir)
            shutil.rmtree(hidden_dir)
            os.rename(tmp_file, hidden_dir)


    def decrypt_file(self, path):
        """Decrypts a file/folder.
        """
        for obj in parse_fs(path):
            self.decrypt_encrypted_filenames(obj.root, obj.encrypted_filenames)
            self.decrypt_encrypted_hidden_filenames(obj.root, obj.encrypted_hidden_filenames)

    def decrypt_encrypted_filenames(self, path, encrypted_filenames):
        for filename in encrypted_filenames:
            filepath = os.path.join(path, filename)
            path_dec = filepath[:-4]
            self.cipher_and_move(filepath, path_dec, self.aes_layer.do_decode)

    def decrypt_encrypted_hidden_filenames(self, path, hidden_filenames):
        raise NotImplementedError