Why the usual replacements fail#
| Replacement | Same user stays recognizable | Can be reversed by guessing |
|---|---|---|
[removed] for each | No | No |
| A random label per line | No | No |
| A plain SHA-256 of the ID | Yes | Yes, when IDs are short or guessable |
| HMAC-SHA-256 with a secret key | Yes | Not without the key |
With the first two, "user A's request hit user B's cache" looks the same as two requests from one user. That difference is often the bug.
A plain hash looks safe and is weak for phone numbers, sequential IDs and email addresses. Hashing every seven-digit number up to 5557712 in a single Python process took 4 seconds to find the one that matched. HMAC adds a key that an attacker does not have.
A label that keeps equality#
import hmac, hashlib, re, secrets, sys
key = secrets.token_bytes(32) # keep it only as long as the handoff
def label(value):
return "user_" + hmac.new(key, value.encode(), hashlib.sha256).hexdigest()[:8]
pattern = re.compile(r"[\w.+-]+@[\w-]+(?:\.[\w-]+)+")
for line in sys.stdin:
sys.stdout.write(pattern.sub(lambda m: label(m.group().lower()), line))
Normalize before you hash. Without .lower(), ana@example.com and Ana@example.com became user_ee715d10 and user_d0cb8a6a in a run with a fixed key, so one person looked like two. The pattern stops before a sentence's final period for the same reason. If the letter case is the suspected bug, skip that step and say so.
Eight hex digits are 32 bits. With a few thousand users a collision is unlikely: for 5,000 it is about 0.3%. With a million, expect collisions and use more digits.
Run every file of one handoff through the same key in one pass. Two runs of this script produce unrelated labels.
What remains identifying#
The result is pseudonymous data. A person can still be recognized from what is left:
- Times, when they match a visit somebody knows of.
- Rare values: one account in a country, one very large order.
- Free text, such as names in a message body or a search query.
- IDs in other shapes: a path like
/users/4411, a token, an IP address. - Identifiers inside encoded values, such as a JWT's payload.
The pattern in the script handles email addresses in plain form and nothing else. List the identifier shapes your log has, write a pattern for each, and then search the output for known values from a test account that you put there on purpose.
Decide whether to send lines at all#
If the receiver has access to the log system, send a query, the absolute time window and the request IDs. Nothing is copied. A pseudonymized extract is for a receiver who should not have that access, and the approval to share it belongs to whoever is responsible for the data.
Worked example#
This example is fictional. A vendor needs to see why some sessions receive another user's cart. The first extract replaces every email with [removed], and the vendor sees a normal sequence. The second uses labels, and shows user_3fa1 logging in and user_9c02's cart returned on the same connection. The vendor's connection reuse is the suspect, and no address was sent.
For a recording instead of a log, check a cleaned HAR or trace. Return to handing over evidence.