DEV Community

William Rodriguez
William Rodriguez

Posted on

Encryption on the fly: Direct encrypt() & decrypt() with auto-JSON.

Encrypting payloads for message queues shouldn't require manual JSON serialization and byte formatting. wauth's on-the-fly encrypt() and decrypt() handle strings, lists, and dicts seamlessly.

Here is the exact production implementation for Direct encrypt() & decrypt() with Auto-JSON:

from wauth import WAuth

auth = WAuth()

# 1. Direct String Encryption
token = auth.encrypt("Sensitive payload for Kafka topic")
original_text = auth.decrypt(token)

# 2. Direct Dictionary (Auto-JSON) Encryption
session = {
    "user_id": 9941,
    "role": "superadmin",
    "permissions": ["read", "write", "audit"]
}

# Automatically serialized to JSON before AES encryption
encrypted_token = auth.encrypt(session)

# Automatically deserialized back into a real Python dictionary!
restored_session = auth.decrypt(encrypted_token)
print(f"Restored role: {restored_session['role']}")
Enter fullscreen mode Exit fullscreen mode

Why this changes developer velocity:

  • Direct Cryptographic Primitives: auth.encrypt(payload) and auth.decrypt(token) without database writes.
  • Auto-JSON Serialization: Automatically handles dict and list serialization and deserialization.
  • Ready for Event Streaming: Ideal for sealing Kafka events, Redis payloads, and HTTP webhooks.

Zero Pain:

  • Needing to encrypt JSON payloads in-transit for message queues without saving them into a database
  • Manually juggling json.dumps() and byte conversions before running Fernet cryptography
  • Corrupted dictionary structures caused by custom encoding and decoding mistakes

Explore the verified open-source repository on GitHub or install it via:

pip install wauth
Enter fullscreen mode Exit fullscreen mode

Author: William Steve Rodríguez Villamizar (Wisrovi)

Top comments (0)