Skip to main content

Python bindings for self-encryption library

Project description

self_encryption

Self encrypting files (convergent encryption plus obfuscation)

Crate Documentation
Documentation
MaidSafe website SAFE Dev Forum SAFE Network Forum

Table of Contents

Overview

A version of convergent encryption with an additional obfuscation step. This pattern allows secured data that can also be de-duplicated. This library presents an API that takes a set of bytes and returns a secret key derived from those bytes, and a set of encrypted chunks.

Important Security Note: While this library provides very secure encryption of the data, the returned secret key requires the same secure handling as would be necessary for any secret key.

image of self encryption

Documentation

Features

  • Content-based chunking
  • Convergent encryption
  • Self-validating chunks
  • Hierarchical data maps for handling large files
  • Streaming encryption/decryption
  • Python bindings
  • Flexible storage backend support
  • Custom storage backends via functors

Usage

Rust Usage

Installation

Add this to your Cargo.toml:

[dependencies]
self_encryption = "0.30"
bytes = "1.0"

Basic Operations

use self_encryption::{encrypt, decrypt_full_set};
use bytes::Bytes;

// Basic encryption/decryption
fn basic_example() -> Result<()> {
    let data = Bytes::from("Hello, World!".repeat(1000));  // Must be at least 3072 bytes
    
    // Encrypt data
    let (data_map, encrypted_chunks) = encrypt(data.clone())?;
    
    // Decrypt data
    let decrypted = decrypt_full_set(&data_map, &encrypted_chunks)?;
    assert_eq!(data, decrypted);
    
    Ok(())
}

Storage Backends

use self_encryption::{shrink_data_map, get_root_data_map, decrypt_from_storage};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};

// Memory Storage Example
fn memory_storage_example() -> Result<()> {
    let storage = Arc::new(Mutex::new(HashMap::new()));
    
    // Store function
    let store = |hash, data| {
        storage.lock().unwrap().insert(hash, data);
        Ok(())
    };
    
    // Retrieve function
    let retrieve = |hash| {
        storage.lock().unwrap()
            .get(&hash)
            .cloned()
            .ok_or_else(|| Error::Generic("Chunk not found".into()))
    };
    
    // Use with data map operations
    let shrunk_map = shrink_data_map(data_map, store)?;
    let root_map = get_root_data_map(shrunk_map, retrieve)?;
    
    Ok(())
}

// Disk Storage Example
fn disk_storage_example() -> Result<()> {
    let chunk_dir = PathBuf::from("chunks");
    
    // Store function
    let store = |hash, data| {
        let path = chunk_dir.join(hex::encode(hash));
        std::fs::write(path, data)?;
        Ok(())
    };
    
    // Retrieve function
    let retrieve = |hash| {
        let path = chunk_dir.join(hex::encode(hash));
        Ok(Bytes::from(std::fs::read(path)?))
    };
    
    // Use with data map operations
    let shrunk_map = shrink_data_map(data_map, store)?;
    let root_map = get_root_data_map(shrunk_map, retrieve)?;
    
    Ok(())
}

Streaming Operations

use self_encryption::{StreamSelfEncryptor, StreamSelfDecryptor};

fn streaming_example() -> Result<()> {
    // Streaming encryption
    let mut encryptor = StreamSelfEncryptor::encrypt_from_file(
        PathBuf::from("input.txt"),
        Some(PathBuf::from("chunks"))
    )?;
    
    let mut all_chunks = Vec::new();
    let mut final_map = None;
    
    while let (chunk, map) = encryptor.next_encryption()? {
        if let Some(chunk) = chunk {
            all_chunks.push(chunk);
        }
        if let Some(map) = map {
            final_map = Some(map);
            break;
        }
    }
    
    // Streaming decryption
    let mut decryptor = StreamSelfDecryptor::decrypt_to_file(
        PathBuf::from("output.txt"),
        &final_map.unwrap()
    )?;
    
    for chunk in all_chunks {
        if decryptor.next_encrypted(chunk)? {
            break;  // Decryption complete
        }
    }
    
    Ok(())
}

Advanced Usage

use self_encryption::{decrypt_range, seek_info};

fn advanced_example() -> Result<()> {
    // Partial decryption (seeking)
    let start_pos = 1024;
    let length = 4096;
    
    let seek = seek_info(file_size, start_pos, length);
    let data = decrypt_range(&data_map, &chunks, seek.relative_pos, length)?;
    
    // Hierarchical data maps
    let store = |hash, data| -> Result<()> {
        // Store chunk
        Ok(())
    };
    
    let shrunk_map = shrink_data_map(large_data_map, store)?;
    
    // Custom error handling
    match encrypt(small_data) {
        Err(Error::Generic(msg)) if msg.contains("Too small") => {
            println!("Data too small for encryption");
        }
        Ok(_) => println!("Encryption successful"),
        Err(e) => return Err(e),
    }
    
    Ok(())
}

Python Usage

Installation

pip install self-encryption

Python Basic Operations

from self_encryption import encrypt_bytes, decrypt_chunks

def basic_example():
    # Create test data (must be at least 3072 bytes)
    data = b"Hello, World!" * 1000
    
    # Encrypt data
    data_map, chunks = encrypt_bytes(data)
    
    # Decrypt data
    decrypted = decrypt_chunks(data_map, chunks)
    assert data == decrypted

Python File Operations

from self_encryption import encrypt_file, decrypt_from_files
import os

def file_example():
    # Encrypt file
    data_map, chunk_names = encrypt_file("input.txt", "chunks")
    
    # Decrypt file
    decrypt_from_files("chunks", data_map, "output.txt")
    
    # Verify content
    with open("input.txt", "rb") as f:
        original = f.read()
    with open("output.txt", "rb") as f:
        decrypted = f.read()
    assert original == decrypted

Python Advanced Features

from self_encryption import (
    shrink_data_map, 
    get_root_data_map,
    StreamSelfEncryptor,
    StreamSelfDecryptor
)

def advanced_example():
    # Hierarchical data maps
    shrunk_map = shrink_data_map(data_map, "chunks")
    root_map = get_root_data_map(shrunk_map, "chunks")
    
    # Streaming encryption
    encryptor = StreamSelfEncryptor("input.txt", "chunks")
    while True:
        chunk, map = encryptor.next_encryption()
        if chunk:
            process_chunk(chunk)
        if map:
            break
    
    # Streaming decryption
    decryptor = StreamSelfDecryptor("output.txt", map)
    for chunk in chunks:
        if decryptor.next_encrypted(chunk):
            break  # Decryption complete

Implementation Details

  • Files are split into chunks of up to 1MB
  • Each chunk is encrypted using AES-256-GCM
  • Chunk names are SHA3-256 hashes of their content
  • Large data maps are automatically shrunk into a hierarchy
  • All operations support custom storage backends
  • Streaming operations for memory-efficient processing

License

Licensed under the General Public License (GPL), version 3 (LICENSE http://www.gnu.org/licenses/gpl-3.0.en.html).

Linking Exception

self_encryption is licensed under GPLv3 with linking exception. This means you can link to and use the library from any program, proprietary or open source; paid or gratis. However, if you modify self_encryption, you must distribute the source to your modified version under the terms of the GPLv3.

See the LICENSE file for more details.

Contributing

Want to contribute? Great :tada:

There are many ways to give back to the project, whether it be writing new code, fixing bugs, or just reporting errors. All forms of contributions are encouraged!

For instructions on how to contribute, see our Guide to contributing.

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

self_encryption-0.30.265.tar.gz (1.1 MB view details)

Uploaded Source

Built Distributions

self_encryption-0.30.265-cp312-none-win_amd64.whl (920.9 kB view details)

Uploaded CPython 3.12 Windows x86-64

self_encryption-0.30.265-cp312-cp312-macosx_11_0_arm64.whl (1.0 MB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

self_encryption-0.30.265-cp312-cp312-macosx_10_12_x86_64.whl (1.0 MB view details)

Uploaded CPython 3.12 macOS 10.12+ x86-64

self_encryption-0.30.265-cp311-none-win_amd64.whl (920.5 kB view details)

Uploaded CPython 3.11 Windows x86-64

self_encryption-0.30.265-cp311-cp311-macosx_11_0_arm64.whl (1.0 MB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

self_encryption-0.30.265-cp311-cp311-macosx_10_12_x86_64.whl (1.0 MB view details)

Uploaded CPython 3.11 macOS 10.12+ x86-64

self_encryption-0.30.265-cp310-none-win_amd64.whl (920.5 kB view details)

Uploaded CPython 3.10 Windows x86-64

self_encryption-0.30.265-cp310-cp310-macosx_11_0_arm64.whl (1.0 MB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

self_encryption-0.30.265-cp310-cp310-macosx_10_12_x86_64.whl (1.0 MB view details)

Uploaded CPython 3.10 macOS 10.12+ x86-64

self_encryption-0.30.265-cp39-none-win_amd64.whl (921.0 kB view details)

Uploaded CPython 3.9 Windows x86-64

self_encryption-0.30.265-cp39-cp39-macosx_11_0_arm64.whl (1.0 MB view details)

Uploaded CPython 3.9 macOS 11.0+ ARM64

self_encryption-0.30.265-cp39-cp39-macosx_10_12_x86_64.whl (1.0 MB view details)

Uploaded CPython 3.9 macOS 10.12+ x86-64

self_encryption-0.30.265-cp38-none-win_amd64.whl (920.4 kB view details)

Uploaded CPython 3.8 Windows x86-64

self_encryption-0.30.265-cp38-cp38-macosx_11_0_arm64.whl (1.0 MB view details)

Uploaded CPython 3.8 macOS 11.0+ ARM64

self_encryption-0.30.265-cp38-cp38-macosx_10_12_x86_64.whl (1.0 MB view details)

Uploaded CPython 3.8 macOS 10.12+ x86-64

self_encryption-0.30.265-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

File details

Details for the file self_encryption-0.30.265.tar.gz.

File metadata

  • Download URL: self_encryption-0.30.265.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for self_encryption-0.30.265.tar.gz
Algorithm Hash digest
SHA256 d57814813e6db8319a24c3aebc848f097edb54b6c2f519ec27eb3fb12efaa25a
MD5 7494e4d5d3eacdd69d82ded43706b59b
BLAKE2b-256 2ac8e715ac2211fdcdfc74935b74b0fd8b81c3b4b734288a81ce80024d860d88

See more details on using hashes here.

Provenance

The following attestation bundles were made for self_encryption-0.30.265.tar.gz:

Publisher: python-publish.yml on dirvine/self_encryption

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file self_encryption-0.30.265-cp312-none-win_amd64.whl.

File metadata

File hashes

Hashes for self_encryption-0.30.265-cp312-none-win_amd64.whl
Algorithm Hash digest
SHA256 67e5bcd1116ae304c024274b7fe9d0772d436d01638526be42bac9e06dd75ae9
MD5 8ab59a37652882972448b902a4a64f59
BLAKE2b-256 9d46f695a010711baa4b4fa272b51cde2411532c91eafda977db16a7a74e44c8

See more details on using hashes here.

Provenance

The following attestation bundles were made for self_encryption-0.30.265-cp312-none-win_amd64.whl:

Publisher: python-publish.yml on dirvine/self_encryption

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file self_encryption-0.30.265-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for self_encryption-0.30.265-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f1c73cf7232c8175f05293f521bbc7eef453a3a3a6cf7be89649442a3eba6fde
MD5 15df21a2ec11ec84aab12de0f124e0bd
BLAKE2b-256 169ff06d3bcb5983aa3b2a3a851acc6f94e7ededef5cd1dd2b87df575d8c10df

See more details on using hashes here.

Provenance

The following attestation bundles were made for self_encryption-0.30.265-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: python-publish.yml on dirvine/self_encryption

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file self_encryption-0.30.265-cp312-cp312-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for self_encryption-0.30.265-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 8ac9a7b33c66062a6909b7b76d1623df3bcd33c39a832f9aea2e16073f0c491d
MD5 c091af44f726edf1ddc3a2edf8af789f
BLAKE2b-256 fe62a0de5804a9e6a3f0927909710922c61b2c51fc4ae2a4659880c0585dda32

See more details on using hashes here.

Provenance

The following attestation bundles were made for self_encryption-0.30.265-cp312-cp312-macosx_10_12_x86_64.whl:

Publisher: python-publish.yml on dirvine/self_encryption

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file self_encryption-0.30.265-cp311-none-win_amd64.whl.

File metadata

File hashes

Hashes for self_encryption-0.30.265-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 a92bcccfeba2de1d619117994be7550b4b9d4ab90b69e46e7c5c554b89315baa
MD5 7ee8d9096203be0204e548f78bcf708b
BLAKE2b-256 107f171b0b052f2dd2961c7774bf9eae990096b1a276f22fa222ff89d55628ca

See more details on using hashes here.

Provenance

The following attestation bundles were made for self_encryption-0.30.265-cp311-none-win_amd64.whl:

Publisher: python-publish.yml on dirvine/self_encryption

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file self_encryption-0.30.265-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for self_encryption-0.30.265-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 78bccb9ae643020ecbceb7736e01099bd96b80652178f3a1604131110610dbf1
MD5 53abdb24679c4b7bc532b12c5eeb78f2
BLAKE2b-256 dcf88bd2b1139c33131bac27343a35d1402d320806834ca6147197322b11faa7

See more details on using hashes here.

Provenance

The following attestation bundles were made for self_encryption-0.30.265-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: python-publish.yml on dirvine/self_encryption

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file self_encryption-0.30.265-cp311-cp311-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for self_encryption-0.30.265-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 93ac827a16c688de47d1d3bdc7ca9365bba7e84bf849285bf32b57fa1f2b1b07
MD5 17fa19ce3d5484763d6404e8c13ace2c
BLAKE2b-256 c32f4c3f7294ae407b8f9361af7c498daf24c5a7fff42b0ea4397f1b18a9f39d

See more details on using hashes here.

Provenance

The following attestation bundles were made for self_encryption-0.30.265-cp311-cp311-macosx_10_12_x86_64.whl:

Publisher: python-publish.yml on dirvine/self_encryption

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file self_encryption-0.30.265-cp310-none-win_amd64.whl.

File metadata

File hashes

Hashes for self_encryption-0.30.265-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 88f1ba8b9bded0d303f5dfb6083f8b6f3c7afc7c6347e7160902fb73b43bd857
MD5 9b90f371046520996dc2c97834f1c1c0
BLAKE2b-256 ed8ece979386ce239f2734935964bb543c6b5c8f5234bee60226830d53326142

See more details on using hashes here.

Provenance

The following attestation bundles were made for self_encryption-0.30.265-cp310-none-win_amd64.whl:

Publisher: python-publish.yml on dirvine/self_encryption

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file self_encryption-0.30.265-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for self_encryption-0.30.265-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 343c96a1bc03e1e0290277a892084c82deca9d5cbd958a69a7bd111af219b434
MD5 e711fce718e3ed57633f8d80627b0008
BLAKE2b-256 4fe8ead3e1d12314928bd6fd879ea5493719e9e48aed8b9c52b12aff44b4bd1e

See more details on using hashes here.

Provenance

The following attestation bundles were made for self_encryption-0.30.265-cp310-cp310-macosx_11_0_arm64.whl:

Publisher: python-publish.yml on dirvine/self_encryption

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file self_encryption-0.30.265-cp310-cp310-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for self_encryption-0.30.265-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 b80fe3316c1a4022a319d65d971747608cd82f83a322f156251635437aeeb660
MD5 1d58a019acac28a04f0f69c3a53e9f58
BLAKE2b-256 4909a2a96dcca853e783476757e34d8e9a6c473d252eb5393601778a5e9ae11b

See more details on using hashes here.

Provenance

The following attestation bundles were made for self_encryption-0.30.265-cp310-cp310-macosx_10_12_x86_64.whl:

Publisher: python-publish.yml on dirvine/self_encryption

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file self_encryption-0.30.265-cp39-none-win_amd64.whl.

File metadata

File hashes

Hashes for self_encryption-0.30.265-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 4e8d8e222bba4ff19fb4041ba9bc02311377f3e2401578400df96fbc0c41549b
MD5 cf93e83b7f890883f4ad265786721411
BLAKE2b-256 e91cdbcae886ba27c1ca0bda3032148109bb45d28023ee52d004edcea89bf314

See more details on using hashes here.

Provenance

The following attestation bundles were made for self_encryption-0.30.265-cp39-none-win_amd64.whl:

Publisher: python-publish.yml on dirvine/self_encryption

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file self_encryption-0.30.265-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for self_encryption-0.30.265-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0e03598f9dbd930997baed3122ff9dc46fbad96654d72c26485cdc10608d0791
MD5 2294ea24d00a890c482e4dca90cbe468
BLAKE2b-256 7bdfea9a4d33c37af052f09d52ecf851b4bf6cce255d5abc5653a57d081cfd1c

See more details on using hashes here.

Provenance

The following attestation bundles were made for self_encryption-0.30.265-cp39-cp39-macosx_11_0_arm64.whl:

Publisher: python-publish.yml on dirvine/self_encryption

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file self_encryption-0.30.265-cp39-cp39-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for self_encryption-0.30.265-cp39-cp39-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 44e9077af4604dd0a90d4a8c72ce76377017d4b5b4b40cbf39393923172acb47
MD5 9649f6588437c5ceb647226e51812e3b
BLAKE2b-256 0cd49d861f9611f70abf3c4dc8c95873572df046390489cecf0ab18e9b4091e3

See more details on using hashes here.

Provenance

The following attestation bundles were made for self_encryption-0.30.265-cp39-cp39-macosx_10_12_x86_64.whl:

Publisher: python-publish.yml on dirvine/self_encryption

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file self_encryption-0.30.265-cp38-none-win_amd64.whl.

File metadata

File hashes

Hashes for self_encryption-0.30.265-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 bb918456702ce3c192734e9dbea7142706df4a7b9f47076750b74789cac0d92f
MD5 26f20f357474f6fd5593c667c6dffd39
BLAKE2b-256 3f61118ca9e898fc0b949f57e3a7db648ff494536bd9769eb7fc39052c100105

See more details on using hashes here.

Provenance

The following attestation bundles were made for self_encryption-0.30.265-cp38-none-win_amd64.whl:

Publisher: python-publish.yml on dirvine/self_encryption

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file self_encryption-0.30.265-cp38-cp38-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for self_encryption-0.30.265-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e337cd7e17aa99aca6f38df502556cae7d152d46f1c5dbcdb6b9412a871546c6
MD5 23b6932ec25e81eddf5506e82ffdc492
BLAKE2b-256 d850df8dceec83c1a41698e4632a636d49a3c83aa1f32aa3452c186b01e22583

See more details on using hashes here.

Provenance

The following attestation bundles were made for self_encryption-0.30.265-cp38-cp38-macosx_11_0_arm64.whl:

Publisher: python-publish.yml on dirvine/self_encryption

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file self_encryption-0.30.265-cp38-cp38-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for self_encryption-0.30.265-cp38-cp38-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 d566e0115811c83cfa43b4e7e86811387d57a4d7fe4f0cbd6373499121c9f23b
MD5 f2e2eca40dee79c7b56dd1bc0f38708a
BLAKE2b-256 29912c1b6082beb252192ee369eaad81cdbe9e7c80a08723fa3707e1dedd60fd

See more details on using hashes here.

Provenance

The following attestation bundles were made for self_encryption-0.30.265-cp38-cp38-macosx_10_12_x86_64.whl:

Publisher: python-publish.yml on dirvine/self_encryption

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file self_encryption-0.30.265-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for self_encryption-0.30.265-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bae9cac9d34bb03b1f569ae2f8f7b16bdcaa65b8612b0f279d118901e4438fc3
MD5 15215f88f886cee24a1b81a0b4e9eee7
BLAKE2b-256 d8e1cbe91dfde82410c607d7551e4b1448fb2eaa7dbdb60326a4f5907f4d9cce

See more details on using hashes here.

Provenance

The following attestation bundles were made for self_encryption-0.30.265-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: python-publish.yml on dirvine/self_encryption

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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