Automatically detect and mask secrets in strings and dicts.
Project description
philiprehberger-secret-mask
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:
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file philiprehberger_secret_mask-0.2.0.tar.gz.
File metadata
- Download URL: philiprehberger_secret_mask-0.2.0.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
56e8c47aebaff6e285b71a5b60774af46066fd0bf967779dc47f7a37306086e6
|
|
| MD5 |
a2778e67f609617aba5c1d956f3a98a8
|
|
| BLAKE2b-256 |
02b0f6ce14a1f7482760a2b94b5fee63cc87166f367e55a70debf8d39ca59f6d
|
File details
Details for the file philiprehberger_secret_mask-0.2.0-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_secret_mask-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0267413952273486984f86381e1cc40f838230dfcd1cc9e7c9bfadf55f105f14
|
|
| MD5 |
fd8bdf154982e7aa12b3d73e7f5033bf
|
|
| BLAKE2b-256 |
d6f21aee61ca3177c6f8bd8ce73f1880c14003802f0a1dcd9ca8268a3202437e
|