Pseudonymise customers with a keyed hash, and see who can reverse it

applied · 50 min · Objective 5.3

Task

Replace every customer id in the orders with a token made by a keyed hash, show that analysis still works on the tokens, and then show that whoever holds the key can reverse every one of them.

Steps

  1. Create a random key with python3 -c "import secrets; print(secrets.token_hex(32))" and save it in lab/mask/key.txt.
  2. A customer's token is the first 16 hex characters of HMAC-SHA256 of the customer id, keyed with that key.
  3. Write lab/mask/orders-pseudo.csv with header order_id,order_date,customer_token,product_id,quantity,discount_pct, every order with its customer id replaced by the token.
  4. Using only the key and data/customers.csv, calculate the token of every customer, match them against the tokens in the file, and record re-identified with the key: N in lab/mask/reverse.txt.

Verify

These checks run in a POSIX shell with Python 3: Terminal on macOS or Linux, and on Windows Git Bash (it comes with Git for Windows) or WSL. On Windows, python3 is often a Microsoft Store placeholder that prints “Python was not found”; run alias python3=py in Git Bash first.

python3 -c "import csv, collections; r = list(csv.DictReader(open('lab/mask/orders-pseudo.csv'))); c = collections.Counter(x['customer_token'] for x in r); print('rows:', len(r)); print('tokens:', len(c)); print('busiest token orders:', max(c.values())); print('raw ids left:', sum(1 for x in r for v in x.values() if v.startswith('C0')))"
cat lab/mask/reverse.txt

2000 rows, 300 tokens, the busiest token has 16 orders -- the same as the busiest customer, so counting and joining still work -- and no raw ids are left. Then all 300 are re-identified with the key. Pseudonymised data is still personal data: the key decides who can reverse it, so it is stored apart from the data and guarded like it.

This is an independent study companion for CompTIA Data+ DA0-002 and is not produced by or endorsed by CompTIA.