Skip to main content

Module crypto

Module crypto 

Source
Expand description

Content Hash Key (CHK) encryption for HashTree

⚠️ EXPERIMENTAL: Encryption API is unstable and may change.

Uses convergent encryption where the key is derived from the content itself. This enables deduplication: same content → same ciphertext.

Algorithm:

  1. content_hash = SHA256(plaintext)
  2. key = HKDF-SHA256(content_hash, salt=“hashtree-chk”, info=“encryption-key”)
  3. ciphertext = AES-256-GCM(key, zero_nonce, plaintext)

Zero nonce is safe because CHK guarantees same key = same content.

Format: [ciphertext][16-byte auth tag]

The content_hash acts as the “decryption key” - store it securely.

Enums§

CryptoError
Encryption error

Functions§

content_hash
Compute content hash (SHA256) - this becomes the decryption key for CHK
could_be_encrypted
Check if data could be encrypted (based on minimum size for non-CHK)
decrypt
Decrypt with a provided key (non-CHK)
decrypt_chk
CHK decrypt: derive key from content_hash, decrypt with zero nonce
encrypt
Encrypt with a provided key (non-CHK, random nonce)
encrypt_chk
CHK encrypt: derive key from content, encrypt with zero nonce
encrypted_size
Calculate encrypted size for given plaintext size (non-CHK with nonce prefix)
encrypted_size_chk
Calculate encrypted size for CHK (no nonce prefix)
generate_key
Generate a random 32-byte key (for non-CHK encryption)
key_from_hex
Convert hex string to key
key_to_hex
Convert key to hex string
plaintext_size
Calculate plaintext size from encrypted size (non-CHK)

Type Aliases§

EncryptionKey
32-byte encryption key (256 bits) - this is the content hash