Skip to main content

Automatically detect and mask secrets in strings and dicts.

Project description

philiprehberger-secret-mask

Tests PyPI version Last updated

Automatically detect and mask secrets in strings and dicts.

Installation

pip install philiprehberger-secret-mask

Usage

from philiprehberger_secret_mask import mask_secrets

data = {"username": "alice", "password": "hunter2", "api_key": "sk-abc123def456ghi789jk"}
safe = mask_secrets(data)
# {"username": "alice", "password": "***ter2", "api_key": "******************89jk"}

Masking Strings

from philiprehberger_secret_mask import mask_secrets

log_line = "Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkw"
safe_log = mask_secrets(log_line)
# Bearer token is replaced with asterisks, last 4 chars visible

Checking Key Names

from philiprehberger_secret_mask import is_secret_key

is_secret_key("db_password")   # True
is_secret_key("api_key")       # True
is_secret_key("username")      # False

Custom Patterns

from philiprehberger_secret_mask import register_pattern, register_key, mask_secrets, is_secret_key

# Register a custom regex pattern for string scanning
register_pattern(r"CUSTOM-[A-Za-z0-9]{8,}")
mask_secrets("key is CUSTOM-abcdefghij")
# "key is **************ghij"

# Register a custom key name for dict masking
register_key("ssn")
is_secret_key("ssn")        # True
mask_secrets({"ssn": "123-45-6789"})
# {"ssn": "*******6789"}

Custom Masking

from philiprehberger_secret_mask import mask_value

mask_value("supersecret", mask_char="#", reveal_last=2)
# "#########et"

API

Function / Constant Description
mask_secrets(data, *, mask_char, reveal_last) Detect and mask secrets in a string, dict, or list
mask_value(value, *, mask_char, reveal_last) Mask a single string value
is_secret_key(key) Check if a key name matches known secret patterns
register_pattern(pattern) Register a custom regex pattern for secret detection
register_key(key) Register a custom key name for secret key detection
_SECRET_PATTERNS Compiled regexes for secret patterns in strings
_SECRET_KEY_NAMES Set of key name substrings that indicate secrets

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

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

philiprehberger_secret_mask-0.2.0.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

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

philiprehberger_secret_mask-0.2.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_secret_mask-0.2.0.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_secret_mask-0.2.0.tar.gz
Algorithm Hash digest
SHA256 56e8c47aebaff6e285b71a5b60774af46066fd0bf967779dc47f7a37306086e6
MD5 a2778e67f609617aba5c1d956f3a98a8
BLAKE2b-256 02b0f6ce14a1f7482760a2b94b5fee63cc87166f367e55a70debf8d39ca59f6d

See more details on using hashes here.

File details

Details for the file philiprehberger_secret_mask-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_secret_mask-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0267413952273486984f86381e1cc40f838230dfcd1cc9e7c9bfadf55f105f14
MD5 fd8bdf154982e7aa12b3d73e7f5033bf
BLAKE2b-256 d6f21aee61ca3177c6f8bd8ce73f1880c14003802f0a1dcd9ca8268a3202437e

See more details on using hashes here.

Supported by

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