Skip to main content

devlacruz_hashlib 🛡️

devlacruz_hashlib (v0.2.0) is an enterprise-grade AI Privacy Shield & TOON Compressor, engineered in Rust with PyO3. It serves as a Zero-Trust middleware for Python AI Agents, encrypting sensitive data locally before reaching commercial LLMs (OpenAI, Anthropic, DeepSeek) while slashing prompt token costs with tabular compression.


Table of Contents

  1. The Problem It Solves
  2. What's New in v0.2.0
  3. Features
  4. Installation
  5. Quick Start
  6. Streaming LLM Responses (FSM Decoder)
  7. Deterministic Encryption & Referential Integrity
  8. Real-World AI Agent Integration
  9. API Reference
  10. Performance
  11. Backwards Compatibility
  12. Contributing
  13. License & Author

The Problem It Solves

When developing AI Agents (AutoGen, LangChain, CrewAI, or custom tool-calling agents), sending PII (Personally Identifiable Information) or PHI (Protected Health Information) to external model APIs introduces critical regulatory risks (GDPR, HIPAA, SOC-2). Additionally, sending verbose JSON structures wastes tokens on repetitive keys and punctuation.

devlacruz_hashlib resolves both challenges seamlessly at native Rust speed:

  1. Locally Encrypts sensitive fields using ChaCha20-Poly1305 DAEAD.
  2. Compresses data into the compact TOON format (or standard JSON if needed).
  3. Preserves Referential Integrity: Identical entities produce identical tokens, enabling the LLM to group, sort, and reason consistently.
  4. Resilient Streaming: Decrypts responses in real time chunk-by-chunk with zero latency overhead.
  5. Anti-BPE Fuzzy Matcher: Automatically heals tokens affected by tokenizer splits, Markdown escapes (\<...\>), spaces, or Unicode zero-width characters.

What's New in v0.2.0

  • 🧠 DAEAD Deterministic Encryption (SIV-like): Preserves entity equivalence across prompts using HMAC-derived nonces and Authenticated Additional Data (AAD).
  • 🏷️ Semantic Typed Tokens: Tokens specify category tags (e.g. <VAULT:NAME:...>, <VAULT:CONDITION:...>), preventing context confusion.
  • ⚡ Streaming VaultStreamDecoder: Finite State Machine (FSM) for zero-latency, chunk-by-chunk decryption over WebSockets / Server-Sent Events (SSE).
  • 🛡️ BPE & Markdown Resilience: Fuzzy scanner transparently recovers corrupted tokens without failing decryption.
  • 🧹 Memory Protection with zeroize: Key material wrapped in SessionKey with automatic zeroization upon cleanup or context exit.
  • 🔄 Hybrid Output: Support for both tabular toon and native json format outputs.
  • 🔒 100% Backwards Compatible: Retains flawless compatibility with legacy v0.1.x code signatures and string keys.

Features

  • 🔒 Zero-Trust Local Cryptography: ChaCha20-Poly1305 DAEAD. Master keys reside strictly in local memory and are never transmitted.
  • 📉 Token Optimization: Slashes JSON token consumption by up to 60% with TOON compression.
  • ⚡ Rust Native Execution: Drops the Python GIL (py.allow_threads) during heavy operations.
  • 📦 Universal ABI3 Wheels: Works out of the box on Python 3.8 through 3.14+ across Linux, macOS, and Windows.

Installation

pip install devlacruz_hashlib

Quick Start

Basic Usage (v0.1.x Compatible)

import devlacruz_hashlib as shield
import json

data = [
    {"id": 1, "name": "Alejandro De La Cruz", "diagnosis": "Anxiety", "score": 85},
    {"id": 2, "name": "Sam Altman", "diagnosis": "Flu", "score": 92}
]

# Encrypt sensitive keys into TOON format
toon_payload, key = shield.prepare_for_ai(json.dumps(data), ["name", "diagnosis"])

print(toon_payload)
# data[2]{diagnosis,id,name,score}
# <VAULT_...>,1,<VAULT_...>,85
# <VAULT_...>,2,<VAULT_...>,92

# Decrypt LLM response
llm_reply = "Patient <VAULT_...> requires follow-up care."
restored = shield.restore_from_ai(llm_reply, key)
print(restored)
# Patient Alejandro De La Cruz requires follow-up care.

Streaming LLM Responses (FSM Decoder)

When consuming tokens in real time from OpenAI, Anthropic, or Ollama streams, tokens may arrive sliced across chunk boundaries. Use VaultStreamDecoder:

from devlacruz_hashlib import SessionKey, VaultStreamDecoder, prepare_for_ai
import json

data = [{"id": 1, "patient": "John Doe", "condition": "Migraine"}]
payload, session_key = prepare_for_ai(
    json.dumps(data),
    ["patient"],
    deterministic=True,
    semantic_types={"patient": "NAME"}
)

# Initialize stream decoder
decoder = VaultStreamDecoder(session_key)

# Simulated stream fragments
stream_chunks = [
    "Patient ",
    "<VAULT:NAME:ab",
    "cd12345",
    "...>",
    " reported mild pain."
]

for chunk in stream_chunks:
    output = decoder.decode_chunk(chunk)
    print(output, end="", flush=True)

# Flush any trailing buffer
print(decoder.flush(), end="")
# Output: "Patient John Doe reported mild pain."

Deterministic Encryption & Referential Integrity

In agentic loops, LLMs often need to compare entities across rows. Standard randomized nonces turn the same name into two different tokens, causing hallucinations. With deterministic=True:

import devlacruz_hashlib as shield
import json

records = [
    {"record_id": 101, "customer": "Alice Smith"},
    {"record_id": 102, "customer": "Alice Smith"},  # Same identity
    {"record_id": 103, "customer": "Bob Jones"}
]

json_str, key = shield.prepare_for_ai(
    json.dumps(records),
    ["customer"],
    format="json",
    deterministic=True,
    semantic_types={"customer": "NAME"}
)

data = json.loads(json_str)
# data[0]['customer'] == data[1]['customer'] -> True (<VAULT:NAME:same_hash>)

Secure Memory with SessionKey

Keys can be explicitly cleared from memory or managed via Python context managers:

from devlacruz_hashlib import SessionKey, prepare_for_ai
import json

with SessionKey() as key:
    payload, _ = prepare_for_ai(
        json.dumps([{"secret": "API_KEY_123"}]),
        ["secret"],
        deterministic=True
    )
    # Perform operations...

# 'key' is automatically zeroized from RAM after exiting the block
assert key.is_zeroized()

Real-World AI Agent Integration

import devlacruz_hashlib as shield
import json

INTERNAL_DB = {
    "Alejandro De La Cruz": {"balance": 5000, "status": "VIP"},
    "Sam Altman": {"balance": 1000, "status": "Standard"}
}

def secure_get_customer_data(encrypted_name: str, key: str) -> str:
    # 1. Decrypt locally
    real_name = shield.restore_from_ai(encrypted_name, key).strip()
    if real_name in INTERNAL_DB:
        return f"User {encrypted_name}: {json.dumps(INTERNAL_DB[real_name])}"
    return "User not found."

# Protect user prompt
masked_input, session_key = shield.prepare_for_ai(
    json.dumps([{"target": "Alejandro De La Cruz"}]),
    ["target"],
    deterministic=True
)

# Agent receives token and retrieves data securely
agent_response = "Summary for <VAULT:...>: balance 5000."
final_response = shield.restore_from_ai(agent_response, session_key)

API Reference

Functions

Function Signature Description
prepare_for_ai (json_str: str, sensitive_keys: list[str], *, format: str = "toon", deterministic: bool = False, semantic_types: dict[str, str] = None) -> tuple[str, SessionKey | str] Encrypts sensitive keys and formats output as TOON or JSON. Returns payload and session key. Drops GIL.
restore_from_ai (text: str, master_key: SessionKey | str) -> str Scans text for <VAULT...> tokens, applies fuzzy healing, and returns plaintext. Compatible with both legacy and typed tokens. Drops GIL.

Classes

  • SessionKey(): Wrapper for ChaCha20 master key with zeroize capabilities and context manager support (__enter__, __exit__).
  • VaultStreamDecoder(key: SessionKey | str): FSM decoder for processing fragmented token streams in real time.

Performance

Compiled natively in Rust with SIMD and ABI3 optimizations.

Benchmarking 10,000 JSON records (Parsing + Encryption + Compression):

  • Pure Python (cryptography + json): ~2.5 - 4.0s
  • devlacruz_hashlib (Rust + PyO3): ~0.08 - 0.15s (~25x faster)

Backwards Compatibility

devlacruz_hashlib v0.2.0 is fully backwards-compatible with earlier v0.1.x versions:

  1. Calls to prepare_for_ai(json_str, keys) default to format="toon" and return tuple[str, str].
  2. restore_from_ai seamlessly accepts legacy str keys and <VAULT_...> tokens.
  3. Passing raw str keys issues a gentle DeprecationWarning while executing successfully without crashing.

Contributing

Contributions are welcome!

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m "feat: enhance streaming decoder")
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License. See the LICENSE file for details.


Author

Alejandro De La Cruz
✉️ devlarood@gmail.com
GitHub: @DevLaCruz

Metadata

Release files for devlacruz-hashlib 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for devlacruz-hashlib 0.2.0
File Interpreter ABI Platform Actions
devlacruz_hashlib-0.2.0-cp38-abi3-win_amd64.whl CPython 3.8 abi3 Windows x86-64 Details for devlacruz_hashlib-0.2.0-cp38-abi3-win_amd64.whl
devlacruz_hashlib-0.2.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.8 abi3 Linux glibc 2.17+ x86-64 Details for devlacruz_hashlib-0.2.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
devlacruz_hashlib-0.2.0-cp38-abi3-macosx_11_0_arm64.whl CPython 3.8 abi3 macOS 11.0+ ARM64 Details for devlacruz_hashlib-0.2.0-cp38-abi3-macosx_11_0_arm64.whl

Total release size: 933.3 kB

Release files / devlacruz_hashlib-0.2.0-cp38-abi3-win_amd64.whl

Download URL devlacruz_hashlib-0.2.0-cp38-abi3-win_amd64.whl
Size 261.7 kB
Tags CPython 3.8 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
11cd8dcea67708eb6d14772d39911942535bc022300a4241dcf31a1ec6926f1b
BLAKE2b-256 checksum
How to use checksums
a5fffccc6bd9f6dbccbd80478615ff24893eee48135dd81a943d38f827165759
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / devlacruz_hashlib-0.2.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL devlacruz_hashlib-0.2.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 358.9 kB
Tags CPython 3.8 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
92e7c360aa03ba9c1e3c57447bb2b3ef79b7bfd98154aa54596d1bc8572281dd
BLAKE2b-256 checksum
How to use checksums
9daf56617afdcdb9a8c3df84dae26603da5ed4da33d3a7e81124b20debe6c8d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / devlacruz_hashlib-0.2.0-cp38-abi3-macosx_11_0_arm64.whl

Download URL devlacruz_hashlib-0.2.0-cp38-abi3-macosx_11_0_arm64.whl
Size 312.8 kB
Tags CPython 3.8 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
013a1171f78d5147a9dbb07b4510628f26799bad4cebd98e2536cf31e601aabc
BLAKE2b-256 checksum
How to use checksums
f46cadfee2c2528f9a0cd4be8fcbd175ef9fb62c05dc12a85d3a4d12aec87b95
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Security for devlacruz-hashlib 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page