big-file-encryption 0.0.0
A package to encrypt large files without loading the entire file into memory.
License: MIT
Requires Python: >=3.6, <4
Keywords: cryptography,big,large,encryption

Big File Encryption
===================

Encrypt large data files chunk-by-chunk, securely.

This package uses AES in GCM mode to encrypt and decrypt file streams.

It relies on the cryptography library to perform the encryption.

```
big unencrypted file, signature --> encrypt and sign --> encrypted file, nonce, tag

big unencrypted file <-- decrypt and verify <-- encrypted file, nonce, tag, signature
```

Example
-------
```python
from encrypted_storage.encryption import EncryptionStream, DecryptionStream
import os
import io

# This is not used, it's just an example of how to read data in chunks
# Credit to this answer on StackOverflow: https://stackoverflow.com/a/519653
def read_chunked(file,chunk_size=64*1024):
    """Function to read chunks from a file"""
    while True:
        data = file.read(chunk_size)
        if not data:
            break
        yield data

# Random key and signature
key = os.urandom(32)
sig = os.urandom(20)

# Open file to write encryption to
with open('encrypted','wb') as ciphertext:
    enc = EncryptionStream(ciphertext,key,sig)
    # I have a plain text file with two lines, read it line-by-line
    with open('plain.txt','r') as plaintext:
        for line in plaintext:
            # Encode to bytes, and encrypt
            line = line.encode()
            enc.write(line)

        # After file is done, finalize. Make sure to keep the file open for this step.
        # The file will not seek to start, so call .seek(0) on the buffer to re-read data
        iv, tag = enc.finalize()


# Open the file to decrypt
with open('plain.decrypted.txt','wb') as plaintext:
    # We need to pass the iv and tag bytes to the decryptor
    dec = DecryptionStream(plaintext,key,sig,iv,tag)
    # Open the encrypted file
    with open('encrypted','rb') as ciphertext:
        # Write decrypted data to the plaintext buffer
        dec.write(ciphertext.read())
        dec.finalize()
```

Classes
-------
```python
class EncryptionStream(StreamMixin):
    """
    Encrypt an incoming byte stream into an outgoing byte stream
    
    Parameters
    ----------
    output_buffer : io.BytesIO
        The output buffer to write encrypted data 
    key : bytes
        The secret key len 16, 24, or 32. 32 bytes recommended.
        You can use os.urandom(32) to generate a secure key.
        Keep this a secret!
    signature : bytes
        A signature to verify the message after decryption
    """
```

```python
class DecryptionStream(StreamMixin):
    """
    Decrypt an incoming byte stream into an outgoing byte stream
    
    Parameters
    ----------
    output_buffer : io.BytesIO
        The output buffer to write decrypted data to
    key : bytes
        The secret key len 16, 24, or 32. 32 bytes recommended.
    signature : bytes
        The signature used to sign the encrypted file.
    iv : bytes
        The .iv attribute from the EncryptionStream object used to encrypt this file.
    tag : bytes
        The .tag attribute from the EncryptionStream object used to encrypt this file.
    """
```