Keyed Pseudonymization
Keyed pseudonymization replaces an identifier, such as an email address or a user ID, with a value computed from the identifier and a secret key. The same input and key always give the same pseudonym, so records can still be joined or counted, but a person who sees only the pseudonym, with no access to the key or to a service that computes it, cannot test a guessed identifier against it. A plain hash lacks that property: when the possible inputs are few or predictable, an attacker hashes every candidate and compares. The general distinction between pseudonymization and anonymization is covered in Pseudonymization and Anonymization; this entry covers how the keyed construction is built and where it stops helping.
Use HMAC, not a hash with a key glued on
The standard construction is HMAC (RFC 2104), which computes the hash of the key combined with the message in two nested passes, written H(K xor opad, H(K xor ipad, text)). Python provides it as hmac.new(key, msg, digestmod). Hashing the concatenation of key and message directly is the pattern HMAC was designed to replace, so use the library function. Compare pseudonyms supplied from outside with hmac.compare_digest rather than == when a comparison is part of a verification step.
Scope, rotation, normalization, and length
Four design choices decide whether the pseudonyms are useful and safe. The script below derives one key per purpose from a master key, then shows the effect of each choice on one fabricated email address.
import hmac, hashlib
def pseud(key: bytes, value: str, n=12):
return hmac.new(key, value.encode(), hashlib.sha256).hexdigest()[:n]
master = b"lab-only-master-key"
def purpose_key(purpose: str) -> bytes: # one derived key per purpose
return hmac.new(master, b"purpose:" + purpose.encode(), hashlib.sha256).digest()
email = "ana@example.test"
print("traces :", pseud(purpose_key("traces"), email))
print("traces :", pseud(purpose_key("traces"), email), "(same key, same value)")
print("analytics:", pseud(purpose_key("analytics"), email), "(different purpose key)")
print("rotated :", pseud(purpose_key("traces-v2"), email), "(after key rotation)")
print("case/space variant:", pseud(purpose_key("traces"), " Ana@Example.test "),
pseud(purpose_key("traces"), " Ana@Example.test ".strip().lower()))
n, bits = 1_000_000, 48
print("collision chance, 1M users, 12 hex chars: %.4f" % (n*(n-1)/2/2**bits))
bits = 64; print("collision chance, 1M users, 16 hex chars: %.2e" % (n*(n-1)/2/2**bits))
traces : a7e8401e1342
traces : a7e8401e1342 (same key, same value)
analytics: af969f448012 (different purpose key)
rotated : 0cce84ff290c (after key rotation)
case/space variant: be9006e54c70 a7e8401e1342
collision chance, 1M users, 12 hex chars: 0.0018
collision chance, 1M users, 16 hex chars: 2.71e-08
- One key per purpose. The traces pseudonym and the analytics pseudonym of the same address differ, so a person who obtains both datasets cannot join them on the pseudonym. Use one shared key only when joining across systems is the goal.
- Rotation breaks continuity. After the key changes, the same address gets a new pseudonym, so records before and after the change no longer match. Rotation limits how long a leaked key can be used to pseudonymize new data, and the price is a gap in longitudinal analysis. It does not undo exposure that has already happened: someone who holds the old key and old pseudonyms can still check candidate identifiers against those old values. Decide the rotation period against both needs.
- Normalize before keying. The padded, mixed-case address produced a different pseudonym from the cleaned one. Apply one canonical form (trim, lowercase where the identifier is case-insensitive) before computing, or the same person becomes two.
- Length trades readability for collisions. A pseudonym truncated to 48 bits (12 hex characters) has about a 0.18 percent chance of at least one collision among a million users, by the birthday approximation \( n(n-1)/2 \cdot 2^{-b} \) with \( n \) users and \( b \) bits; 64 bits brings that near \( 3 \times 10^{-8} \). The numbers are for a uniformly random output and show the order of magnitude. Choose the length from the population size and how harmful a false match would be.
What it does not do
The mapping is deterministic, so a party who holds the key and a candidate identifier can confirm whether that person appears in the data. The key is therefore a secret with its own storage, access control, and rotation plan, and it should not sit next to the data it protects. The same applies to any service that computes pseudonyms on request: whoever can submit chosen identifiers to it can test guesses, so its input permissions, purpose, and rate limits need protection as well. A stable pseudonym also supports profiling: all of one person’s activity shares a label, and other fields in the record, such as rare locations or timestamps, can identify the person without the key. When linkage is needed only occasionally, for support or deletion, a restricted lookup table can replace the derivation. Keyed pseudonymization lowers the risk in shared data; it does not make the data anonymous.
References
Discover more from Insightful Data Lab
Subscribe to get the latest posts sent to your email.
