<!-- Copyright (c) 2026 defconxt. All rights reserved. -->
<!-- Licensed under AGPL-3.0 — see LICENSE file for details. -->
---
name: implementing-data-anonymization
description: >-
  Implement data anonymization techniques including k-anonymity, l-diversity,
  t-closeness, generalization, suppression, and pseudonymization to protect
  personal data while maintaining analytical utility.
domain: cybersecurity
subdomain: privacy-engineering
tags:
  - anonymization
  - k-anonymity
  - l-diversity
  - t-closeness
  - pseudonymization
  - de-identification
  - gdpr
version: "1.0"
author: defconxt
license: AGPL-3.0
metadata:
  mitre-attack: []
  mitre-attack: []
---

# Implementing Data Anonymization

## Overview

Data anonymization transforms personal data so individuals can no longer be identified
directly or indirectly, removing it from GDPR scope (Recital 26). This skill covers
formal anonymization models (k-anonymity, l-diversity, t-closeness), practical
techniques (generalization, suppression, masking, pseudonymization), and verification
of anonymization effectiveness against re-identification attacks.

Mode: `[MODE: PRIVACY]` — Data protection and de-identification.

## Prerequisites

- Understanding of quasi-identifiers vs. direct identifiers vs. sensitive attributes
- Python 3.10+ with pandas, hashlib
- Dataset with classified columns (identifiers, quasi-identifiers, sensitive)
- Knowledge of GDPR Recital 26 (anonymization vs. pseudonymization distinction)

## Key Concepts

### Anonymization Models

```text
k-Anonymity: Each record is indistinguishable from at least k-1 other records
             on quasi-identifier attributes. Prevents singling out.

l-Diversity: Each equivalence class has at least l well-represented values
             for sensitive attributes. Prevents attribute disclosure.

t-Closeness: Distribution of sensitive attributes in each equivalence class
             differs by at most t from the global distribution. Prevents
             skewness and similarity attacks.
```

### Techniques

| Technique | Description | Reversible |
|-----------|-------------|------------|
| Generalization | Replace specific values with ranges (age 27 → 25-30) | No |
| Suppression | Remove identifying values entirely | No |
| Masking | Replace characters (john@x.com → j***@x.com) | No |
| Pseudonymization | Replace with consistent tokens (GDPR Art. 4(5)) | Yes |
| Perturbation | Add random noise to numeric values | No |
| Swapping | Exchange values between records | No |
| Aggregation | Replace individual records with group statistics | No |

### Generalization Hierarchies

```python
AGE_HIERARCHY = {
    0: lambda x: x,           # Level 0: exact age
    1: lambda x: f"{(x // 5) * 5}-{(x // 5) * 5 + 4}",  # Level 1: 5-year range
    2: lambda x: f"{(x // 10) * 10}-{(x // 10) * 10 + 9}",  # Level 2: decade
    3: lambda x: "*",          # Level 3: suppressed
}

ZIP_HIERARCHY = {
    0: lambda x: x,           # 90210
    1: lambda x: x[:4] + "*",  # 9021*
    2: lambda x: x[:3] + "**", # 902**
    3: lambda x: x[:2] + "***", # 90***
    4: lambda x: "*****",       # suppressed
}
```

### Pseudonymization with HMAC

```bash
# Pseudonymize a CSV column using HMAC-SHA256
node scripts/agent.js --action pseudonymize \
  --input data.csv --column email --secret "HMAC_SECRET_KEY" \
  --output /tmp/pseudonymized.csv
```

## Workflow

### Step 1: Classify Columns

```bash
node scripts/agent.js --action classify --input dataset.csv \
  --output /tmp/classification.json
```

### Step 2: Apply k-Anonymity

```bash
node scripts/agent.js --action k-anonymity --input dataset.csv \
  --quasi-identifiers "age,zipcode,gender" --k 5 \
  --output /tmp/anonymized.json
```

### Step 3: Verify Anonymization

```bash
node scripts/agent.js --action verify --input /tmp/anonymized.csv \
  --quasi-identifiers "age,zipcode,gender" --k 5
```

### Step 4: Assess Re-Identification Risk

```bash
node scripts/agent.js --action risk --input /tmp/anonymized.csv \
  --quasi-identifiers "age,zipcode,gender"
```

## Verification

- [ ] Direct identifiers removed or pseudonymized
- [ ] Quasi-identifiers generalized to achieve k ≥ 5
- [ ] Sensitive attributes satisfy l-diversity (l ≥ 3)
- [ ] Re-identification risk below acceptable threshold
- [ ] Pseudonymization keys stored separately with access controls
- [ ] Anonymization documented — GDPR Recital 26 applies only if truly irreversible
- [ ] Utility assessment confirms data remains fit for intended purpose

## References

- [GDPR Recital 26 — Anonymisation](https://gdpr-info.eu/recitals/no-26/)
- [Article 29 WP Opinion 05/2014 on Anonymisation Techniques](https://ec.europa.eu/justice/article-29/documentation/opinion-recommendation/files/2014/wp216_en.pdf)
- [NIST De-Identification of Personal Information (SP 800-188)](https://csrc.nist.gov/publications/detail/sp/800-188/final)
- [ARX Data Anonymization Tool](https://arx.deidentifier.org/)
- [HIPAA Safe Harbor — 18 Identifiers](https://www.hhs.gov/hipaa/for-professionals/privacy/special-topics/de-identification/)
