Exemple #1
0
def decrypt_file(input_file_name,
                 output_file_name,
                 password=None,
                 private_key=None):
    if not os.path.exists(input_file_name):
        LOGGER.warn(
            'skipping decryption of "%s": encrypted input file does not exist',
            input_file_name)
        return
    if os.path.exists(output_file_name):
        LOGGER.warn(
            'skipping decryption of "%s": chosen output file "%s" already exists',
            input_file_name, output_file_name)
        return
    LOGGER.info('decrypting "%s" to "%s"', input_file_name, output_file_name)
    try:
        with open(input_file_name, 'rb') as instream:
            if not os.path.isdir(os.path.dirname(output_file_name)):
                os.makedirs(os.path.dirname(output_file_name))
            with open(output_file_name, 'wb') as outstream:
                core.decrypt_stream(instream,
                                    outstream,
                                    password=password,
                                    private_key=private_key)
    except:
        LOGGER.error('decryption failed, exception occurred: %s: %s',
                     sys.exc_info()[0],
                     sys.exc_info()[1])
        if os.path.exists(output_file_name):
            os.remove(output_file_name)
        raise
Exemple #2
0
def test_decrypt_single_line_stream_fails_without_key():
        outstream = io.BytesIO()
        try:
                with open('tests/testfiles-v1/csenc/single-line.txt', 'rb') as f:
                        core.decrypt_stream(f, outstream)
                assert False, 'expected exception'
        except Exception as e:
                assert_that(e.args[0]).matches('not enough information to decrypt')
def test_decrypt_single_line_stream_fails_without_key():
        outstream = io.BytesIO()
        try:
                with open('tests/testfiles-csenc/single-line.txt', 'rb') as f:
                        core.decrypt_stream(f, outstream)
                assert False, 'expected exception'
        except Exception as e:
                assert_that(e.args[0]).matches('not enough information to decrypt')
Exemple #4
0
def decrypt_file(input_file_name, output_file_name, password=None, private_key=None):
        if not os.path.exists(input_file_name):
                LOGGER.warn('skipping decryption of "%s": encrypted input file does not exist',
                        input_file_name
                )
                return
        if os.path.exists(output_file_name):
                LOGGER.warn('skipping decryption of "%s": chosen output file "%s" already exists',
                        input_file_name, output_file_name
                )
                return
        LOGGER.info('decrypting "%s" to "%s"', input_file_name, output_file_name)
        try:
                with open(input_file_name, 'rb') as instream:
                        if not os.path.isdir(os.path.dirname(output_file_name)):
                                os.makedirs(os.path.dirname(output_file_name))
                        with open(output_file_name, 'wb') as outstream:
                                core.decrypt_stream(instream, outstream, password=password, private_key=private_key)
        except:
                LOGGER.error('decryption failed, exception occurred: %s: %s', sys.exc_info()[0], sys.exc_info()[1])
                if os.path.exists(output_file_name):
                        os.remove(output_file_name)
                raise
Exemple #5
0
def test_decrypt_single_line_stream_with_private_key_v3():
        outstream = io.BytesIO()
        with open('tests/testfiles-v3/csenc/ssingle-line.txt', 'rb') as f:
                core.decrypt_stream(f, outstream, private_key=PRIVATE_KEY)
        assert outstream.getvalue() == b'Just a single line, no newline character at the end...'
Exemple #6
0
def test_decrypt_binary_stream_with_password_v31():
        outstream = io.BytesIO()
        with open('tests/testfiles-v3/csenc/42-bytes.txt', 'rb') as f:
                core.decrypt_stream(f, outstream, password=PASSWORD)
        with open('tests/testfiles-v3/plain/42-bytes.txt', 'rb') as f:
                assert outstream.getvalue() == f.read()
Exemple #7
0
def test_decrypt_larger_textfile_with_password_v31():
        outstream = io.BytesIO()
        with open('tests/testfiles-v3/csenc/5000words-3.1.txt', 'rb') as f:
                core.decrypt_stream(f, outstream, password=PASSWORD)
        with open('tests/testfiles-v3/plain/5000words-3.1.txt', 'rb') as f:
                assert outstream.getvalue() == f.read()
Exemple #8
0
def test_decrypt_single_line_stream_with_password_v31():
        outstream = io.BytesIO()
        with open('tests/testfiles-v3/csenc/ssingle-line-3.1.txt', 'rb') as f:
                core.decrypt_stream(f, outstream, password=PASSWORD)
        assert outstream.getvalue() == b'Just a single line, no newline character at the end...'
def test_decrypt_single_line_stream_with_private_key():
        outstream = io.BytesIO()
        with open('tests/testfiles-csenc/single-line.txt', 'rb') as f:
                core.decrypt_stream(f, outstream, private_key=PRIVATE_KEY)
        assert outstream.getvalue() == b'Just a single line, no newline character at the end...'
def test_decrypt_single_line_stream_with_password():
        outstream = io.BytesIO()
        with open('tests/testfiles-csenc/single-line.txt', 'rb') as f:
                core.decrypt_stream(f, outstream, password=PASSWORD)
        assert outstream.getvalue() == b'Just a single line, no newline character at the end...'