Anonymizer icon indicating copy to clipboard operation
Anonymizer copied to clipboard

feat(replace): optional secret key (HMAC) for the Hash strategy

Open vamsibvv opened this issue 1 month ago • 0 comments

Priority Level

High (Major improvement)

Is your feature request related to a problem?

Hash computes hashlib.new(algorithm, text.encode()).hexdigest()[:digest_length] with no secret, so anyone who has the output can get the original value back by hashing candidates. That's fast for the values people usually hash: dates of birth, phone numbers, SSNs, ZIP codes, member IDs. The label in the token (<HASH_SSN_...>) also tells them which format to try.

With the default Hash(), a date_of_birth token went back to 04/11/1987 in under a second locally, and an SSN takes minutes on one core. The docs currently suggest Hash for "re-joining records by identifier without keeping the identifier", which suggests the identifier can't be recovered.

Describe the solution you'd like

An optional secret on Hash, e.g. Hash(key=...) (pydantic SecretStr, also readable from an env var), that switches the digest to HMAC-SHA256. Same key, same token, so joins across records and runs still work, but tokens can't be reversed without the key. When no key is set, keep today's output so existing results stay comparable, and say in the docs that unkeyed Hash is reversible for low-entropy values.

Describe alternatives you've considered

  • A random per-run key by default: safer, but tokens would no longer match across runs, which is the main reason people pick Hash.
  • Only documenting the limitation: cheap, but users who read "without keeping the identifier" would still be exposed.

Additional context

This is the same per-run HMAC approach the measurement collector already uses for record identity (docs/development/observability.md). It touches the public Hash config, so I'm happy to write the plan under plans/<issue>/ first and then open the PR once this is triaged.

vamsibvv avatar Oct 29 '26 16:10 vamsibvv