Skip to main content

pxa-security

Purpose-specific credential and user-info encryption utilities.

pip install pxa-security

The distribution name is pxa-security; the import name is pxa_security.

Credential API

Use a secret master key that is supplied separately from the configuration file, normally through an environment variable or a secret manager. Tokens use scrypt with a per-token random salt and AES-GCM authenticated encryption.

import os

from pxa_security import encrypt_credential, decrypt_credential

master_key = os.environ["PXA_CREDENTIAL_MASTER_KEY"]

# The token is safe to store in a config file; the master key is not.
token = encrypt_credential(master_key, "s3cret!")

# Decrypt with the separately supplied master key.
password = decrypt_credential(master_key, token)
assert password == "s3cret!"

A wrong master key, damaged token, or modified token raises ValueError:

decrypt_credential("wrong-master-key", token)  # ValueError

Use at least 32 random bytes for the master key and never write it beside the encrypted token. A username, application name, or other public identifier is not a master key. One suitable value can be generated with secrets.token_urlsafe(32) and then provisioned through your deployment's secret store.

A master key shorter than 32 bytes is accepted — every token carries a random scrypt salt, so short keys still encrypt and decrypt correctly — but encrypt_credential emits a UserWarning to keep the recommendation visible. Decryption never warns.

User-info API

AES-128-CBC in a wire format compatible with the legacy Node.js implementation: ciphertext is a hex string, the IV equals the key, and the plaintext is a JSON object whose string values are wrapped in MIME B-encoding (=?UTF-8?B?<base64>?=).

from pxa_security import encrypt_userinfo, decrypt_userinfo

key = "0123456789abcdef"  # must be exactly 16 bytes (AES-128)

# Pass a dict: values are MIME-encoded and JSON-serialized automatically
data = encrypt_userinfo({"name": "홍길동", "dept": "IT"}, key)

userinfo = decrypt_userinfo(data, key)
assert userinfo == {"name": "홍길동", "dept": "IT"}

encrypt_userinfo also accepts a ready-made plaintext string (legacy usage), and decrypt_userinfo decrypts data produced by the original Node.js code as-is.

API summary

Function Description
encrypt_credential(master_key: str | bytes, credential: str) -> str Encrypts a credential with a separately managed secret and returns an ASCII token.
decrypt_credential(master_key: str | bytes, encrypted_credential: str | bytes) -> str Authenticates and decrypts a token produced by encrypt_credential.
encrypt_userinfo(userinfo: str | dict, key: str) -> str Encrypts user info with AES-128-CBC (legacy format); returns a hex string.
decrypt_userinfo(encrypted_userinfo: str, key: str) -> dict Decrypts the hex string and returns the user-info dict with MIME-encoded values decoded. Raises ValueError on failure.

Security notes

  • Credential API: AES-GCM ensures ciphertext modification and wrong keys are detected. scrypt makes offline guessing more expensive, but it cannot compensate for a weak or exposed master key. Keep the master key out of source control and configuration files.
  • User-info API: the legacy format reuses the key as the IV, which makes encryption deterministic (identical plaintexts produce identical ciphertexts) and is kept only for interoperability with existing data. Prefer the credential API for new data.

License

Apache License 2.0 — see LICENSE.

Download files

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

Source Distribution

pxa_security-1.0.0.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pxa_security-1.0.0-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file pxa_security-1.0.0.tar.gz.

File metadata

  • Download URL: pxa_security-1.0.0.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.13

File hashes

Hashes for pxa_security-1.0.0.tar.gz
Algorithm Hash digest
SHA256 415200af79b9a4384b6d0ba1ca9dfb6fabe99430b19b5393d4e0bf06708241ec
MD5 3f05d8937191cf513d1ade0b083474c2
BLAKE2b-256 8af39e976188a037baf0eaffc2c1c332f8e2808d3e21031ae87b49b1c32641d4

See more details on using hashes here.

File details

Details for the file pxa_security-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pxa_security-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.13

File hashes

Hashes for pxa_security-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 54cbc9390c72da2cc24e9d183b09278735294f3877e8fed36ddc3ce03509b53c
MD5 e61e288e62bc762aeb4eb5bd44b7259f
BLAKE2b-256 052eeab15f95b39863be582ed2cdfe9f631c11444c90f330ab166fd8267959c5

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 files

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