Hushfang / Guide
Secret handling in Python
Choose the secret type when the application first receives the value. Register it before a client, retry wrapper or logger can see its unwrapped representation.
from hushfang import SecretKey, SecretModel, SecretRegistry, SecretURL, revealing, validate_model
class Settings(SecretModel):
endpoint: SecretURL
signing_key: SecretKey
registry = SecretRegistry()
settings = validate_model(Settings, config_input, registry)
with revealing(settings.signing_key, registry=registry) as (key,):
client.sign(payload, key)
Install a RedactingFormatter on each text handler as shown in the quick start. A guarded wrapper still contains usable sensitive material in memory. Any code able to call its explicit accessor can obtain the value.
| Boundary | Behavior |
|---|---|
| Printing and serialization | SecretText, SecretBinary, SecretKey and SecretMnemonic display a fixed mask. Pickle, instance dictionary inspection, length and mutation are disabled. Copying preserves the immutable wrapper. SecretURL displays a parsed URL origin and masks ambiguous URLs. |
| Pydantic validation | SecretModel validates field defaults and omits secret defaults and examples from generated JSON schemas. validate_model registers a model after successful validation. A validation failure carries a summary of trusted field names and error codes. It can be inspected without retaining Pydantic's input-bearing error chain. Regular Pydantic model validation retains its native behavior. |
| Explicit disclosure | revealing yields raw values for a client call. It sanitizes exceptions leaving the block using the supplied registry. reveal_structure creates a plain copy of standard containers, preserving aliases, cycles and named tuples for an explicit serialization boundary. |
| Exception handling | sanitize_exception(error, registry) returns the error to forward. It handles causes, contexts, notes, groups, bytes, nested fields and process-exit status. It replaces an unsafe diagnostic when it cannot clean it. Enable this boundary before forwarding the exception to a separate reporter. Captured frame locals require a policy in that reporter. |
| Registry lifetime | Registrations are atomic and scoped to the registry. Readers see an immutable matching snapshot. A POSIX child retains registered values and can register after a multithreaded fork. Call clear() or discard the registry when its scope ends. |
| Log emission | Values are matched on the final formatted text. Named-field rules also cover nested built-in containers in messages, arguments and extra fields. The formatter works on a copy of the record. Use redact_bytes on a binary channel. |
Text representations and cost
Registering text prepares literal and encoded forms, including XML text and common quoted XML/HTML attributes. A public string that only resembles a stored value should survive unchanged. The formatter selects between searching for the spellings and looking up candidate substrings by length. The fast path reuses the existing set and respects overlaps and short-word boundaries. Markup fixtures · Registry latency and memory
Matching keys and phrases
SecretKeyaccepts bytes or text. It recognizes raw and prefixed hexadecimal, checked WIF, raw Base58, standard BIP-32 xprv/tprv, Ed25519 seed-plus-public keypairs, JSON byte arrays and a complete PEM private-key envelope. WIF versions covered by the fixtures are80,ef,b0,9e,ccandf1in hexadecimal, with and without the compression marker. Invalid extended keys retain their literal mask. Public extended-key data does not supply private scalar aliases.- Key encodings include hexadecimal dumps, RFC 4648 encodings, byte representations and decimal integers. For key-sized inputs the matcher can locate internal fragments of at least 15 characters containing an indexed eight-character sequence with at least three distinct characters. Long keys retain full-value matching. Base58 and decimal conversion are bounded at 1,785 bytes. Decimal results also respect the interpreter's integer conversion limit.
SecretSeedmarks every byte of a seed as private. Use it for a 64-byte BIP-39 seed. A 64-byte Ed25519 keypair registered asSecretKeycan expose its public half intentionally.SecretMnemonicrecognizes sequences of at least three registered words across case changes, combining marks, punctuation, numeric word labels, JSON escapes and URL escapes. Single words remain public. The Han segmentation rule applies to a registered phrase made of single Han characters. For a derived seed or extended private key, register the actual output asSecretSeedorSecretKeyrespectively. A wrapper can also carry additional exact spellings viaaliases=(... ,). Passphrases, paths and networks affect what a wallet derives.- Use
SecretRegistry(token_mode="delimited")when the logging client emits truncated ASCII tokens as separate units. It can match a distinctive prefix or suffix of at least ten characters, and internal fragments of at least 15 characters containing a mixed-case or alphanumeric anchor. Short alphabetic strings of up to four characters require a word boundary on both sides. The defaultexactmode keeps full substring matching for ordinary secrets. The benchmark displays both configurations. Two identical strings with different sensitivity in the same field require context from the producer.
Hushfang