Skip to main content

Encrypt large files without loading the entire file into memory.

Project description

Buffered 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, verification data --> encrypt and sign --> encrypted file, iv, tag

big unencrypted file <-- decrypt and verify <-- encrypted file, iv, tag, verification data

Example

import os
from buffered_encryption import EncryptionIterator, DecryptionIterator

plaintext = open("plain.txt","rb")

key = os.urandom(32)
sig = os.urandom(12)

enc = EncryptionIterator(plaintext,key,sig)
with open("cipher","wb") as ciphertext:
    for chunk in enc:
        ciphertext.write(chunk)

plaintext.close()

ciphertext = open("cipher","rb")

dec = DecryptionIterator(ciphertext,key,sig,enc.iv,enc.tag)
with open("plain.dec.txt","wb") as decrypted:
    for chunk in dec:
        decrypted.write(chunk)

ciphertext.close()

Classes

class EncryptionIterator:
    """
    Encrypt a file iteratively

    Parameters
    ----------
    file : io.BytesIO
        The file buffer to encrypt
    key : bytes
        The secret key for AES encryption
    signature : bytes
        Additional data used to verify the key later
    chunk_size : int
        How much data to encrypt per iteration

    Attributes
    ----------
    iv : bytes
        The 12-byte initialization vector for GCM. You will need this value for decryption.
    tag : bytes
        The tag to verfiy data integrity on decryption
    """
class DecryptionIterator:
    """
    Decrypt a file iteratively

    Parameters
    ----------
    file : io.BytesIO
        The file buffer to encrypt
    key : bytes
        The secret key for AES encryption
    signature : bytes
        Additional data used to verify the key later
    iv : bytes
        The initialization vector from the EncryptionIterator object
    tag : bytes
        The tag used for data integrity from the EncryptionIterator object
    chunk_size : int
        How much data to encrypt per iteration
    """

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

buffered_encryption-0.0.0.tar.gz (3.6 kB view hashes)

Uploaded Source

Built Distribution

buffered_encryption-0.0.0-py3-none-any.whl (4.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page