PII Detection & Format-Preserving Tokenization
Synthetic Test Data Sprint β Day 2
Detect sensitive personal identifiers in JSON payloads, generate valid Luhn check credit cards, and execute deterministic HMAC pseudonymization.
Copying real customer data into test databases violates GDPR, HIPAA, and PCI-DSS. However, naive masking (like replacing emails with dummy@dummy.com) breaks relational foreign keys and API validations. Today you build format-preserving tokens and deterministic HMAC pseudonymization.
The official reference codebase for Day 2 is hosted in the repository. Clone the repository and navigate to day-2-pii-tokenization to run the working solution.
git clone https://github.com/777abhi/synthetic-test-data-roadmap.git
cd synthetic-test-data-roadmap/day-2-pii-tokenizationInspect PII Tokenization
Review regex pattern scanning, Luhn checksum verification, and salted HMAC surrogate tokens.
cat synthlab/pii_tokenizer.py | head -50import unittest
from synthlab.pii_tokenizer import PIIScanner, PIITokenizer, LuhnValidator
class TestDay2PIITokenization(unittest.TestCase):
def setUp(self):
self.tokenizer = PIITokenizer(salt="qe-test-salt-2026")
def test_luhn_credit_card_generation_and_validation(self):
card = LuhnValidator.generate_valid_card(prefix="4111", length=16)
self.assertTrue(LuhnValidator.is_valid(card))
def test_deterministic_pseudonymization_preserves_referential_equality(self):
token_1 = self.tokenizer.tokenize_email("alice@example.com")
token_2 = self.tokenizer.tokenize_email("alice@example.com")
self.assertEqual(token_1, token_2)Execute Day 2 Tests
Run the PII and Luhn verification tests.
npm run test:day2- Implemented the Luhn algorithm (Mod 10) to generate mathematically valid test credit cards.
- Used salted HMAC-SHA256 for deterministic pseudonymization: same customer produces identical surrogate tokens.
- Deep masked nested dictionary payloads while preserving RFC-compliant email and SSN formats.
π§ Knowledge Check
Why is deterministic pseudonymization preferred over random string replacement for test fixtures?