Privacy & Compliance
Security protects data from unauthorised access. Privacy is about whether data about people is collected and used fairly and lawfully in the first place, even by authorised staff. A perfectly secure system can still violate privacy by collecting too much, keeping it forever, or using it for purposes nobody agreed to. Laws such as the EU's GDPR turn these principles into legal obligations, and compliance frameworks turn security good practice into auditable requirements.
Note: This lesson is an overview for developers, not legal advice. Real projects should involve their privacy or legal team.
GDPR in brief
The General Data Protection Regulation applies to organisations that process personal data (any information relating to an identifiable person: names, emails, IP addresses, location, cookie IDs) of people in the EU, wherever the organisation is based. Its core principles (Article 5):
| Principle | What it means for developers |
|---|---|
| Lawfulness, fairness, transparency | Have a legal basis (consent, contract, legal obligation, legitimate interest …) and tell users clearly |
| Purpose limitation | Collect for a specific purpose; don't reuse for unrelated ones |
| Data minimisation | Collect only what you need for that purpose |
| Accuracy | Let users correct their data |
| Storage limitation | Delete or anonymise when no longer needed |
| Integrity & confidentiality | Appropriate security (encryption, access control) |
| Accountability | Be able to demonstrate all of the above |
People have rights: to access their data, correct it, delete it ("right to be forgotten"), restrict processing, take it elsewhere (portability) and object. A personal-data breach usually has to be reported to the supervisory authority within 72 hours, and to affected people when the risk to them is high. Fines can reach 4% of worldwide annual turnover.
Other laws follow similar ideas: the UK GDPR, California's CCPA/CPRA, Brazil's LGPD, Vietnam's Decree 13/2023 and Personal Data Protection Law, and sector rules such as HIPAA for US health data.
Data minimisation in code
Privacy by design means building these principles into systems from the start. A practical first step is a schema review: for every field, what is it for, and do we need it in this form?
signup_form = {
"email": "lan.nguyen@example.com",
"password": "(hashed separately)",
"full_name": "Nguyen Thi Lan",
"date_of_birth": "1998-04-17",
"phone": "+84 912 345 678",
"gender": "female",
"national_id": "001198012345",
}
PURPOSE = { # what the shop actually needs, and in what form
"email": "keep: login and order emails",
"password": "keep: authentication",
"full_name": "keep: shipping label",
"date_of_birth": "reduce: only need 'is 18 or older'",
"phone": "optional: delivery updates, ask separately",
}
kept, reduced, dropped = [], [], []
for field in signup_form:
rule = PURPOSE.get(field)
if rule is None:
dropped.append(field)
elif rule.startswith("reduce"):
reduced.append(field)
else:
kept.append(field)
print("keep: ", kept)
print("reduce: ", reduced)
print("drop: ", dropped)keep: ['email', 'password', 'full_name', 'phone']
reduce: ['date_of_birth']
drop: ['gender', 'national_id']
Every field you don't collect is a field that can't leak. Storing is_adult = True instead of a full birth date answers the business question with far less risk.
Pseudonymisation and anonymisation
- Pseudonymised data has identifiers replaced by tokens, but can be re-linked with extra information (a key or lookup table). It is still personal data under GDPR, but it lowers risk.
- Anonymised data can no longer be linked to a person by any reasonable means; it falls outside GDPR. True anonymisation is harder than it looks: combinations of postcode, birth date and gender identify most people.
A keyed HMAC is a good way to pseudonymise identifiers for analytics: the same user always maps to the same token, but without the key nobody can reverse it or test guesses (unlike a plain hash of an email address).
import hmac
import hashlib
PSEUDONYM_KEY = b"demo-key-kept-in-a-separate-vault" # DEMO ONLY
def pseudonym(email):
return hmac.new(PSEUDONYM_KEY, email.lower().encode(), hashlib.sha256).hexdigest()[:12]
def generalise_age(birth_year, current_year=2026):
age = current_year - birth_year
low = age // 10 * 10
return f"{low}-{low + 9}"
orders = [
{"email": "lan.nguyen@example.com", "birth_year": 1998, "city": "Hanoi", "total": 42.0},
{"email": "Lan.Nguyen@example.com", "birth_year": 1998, "city": "Hanoi", "total": 18.5},
{"email": "tom@example.com", "birth_year": 1971, "city": "Da Nang", "total": 99.9},
]
for o in orders:
print(pseudonym(o["email"]), generalise_age(o["birth_year"]), o["city"], o["total"])baf4bf1b41f0 20-29 Hanoi 42.0
baf4bf1b41f0 20-29 Hanoi 18.5
5a218bd8ba78 50-59 Da Nang 99.9
The two orders from the same person get the same pseudonym (after normalising the case), so analysts can still count repeat customers, while ages are generalised into bands.
Retention
Storage limitation means deleting data on a schedule. A retention job is simple and very effective:
from datetime import date
RETENTION_DAYS = {"access_logs": 90, "support_tickets": 730, "invoices": 3650}
today = date(2026, 6, 1)
records = [
("access_logs", date(2026, 1, 10)),
("access_logs", date(2026, 5, 20)),
("support_tickets", date(2023, 11, 2)),
("invoices", date(2019, 3, 1)),
]
for kind, created in records:
age = (today - created).days
action = "DELETE" if age > RETENTION_DAYS[kind] else "keep"
print(f"{kind:<16} {created} age {age:>4} d limit {RETENTION_DAYS[kind]:>4} d {action}")access_logs 2026-01-10 age 142 d limit 90 d DELETE
access_logs 2026-05-20 age 12 d limit 90 d keep
support_tickets 2023-11-02 age 942 d limit 730 d DELETE
invoices 2019-03-01 age 2649 d limit 3650 d keep
Note that invoices are kept for ten years here, because tax law often requires it. Retention periods come from law and business need, and should be written down.
Compliance frameworks
| Framework | What it is |
|---|---|
| ISO/IEC 27001 | International standard for an information security management system (ISMS); certifiable |
| SOC 2 | US audit report on security, availability, confidentiality, processing integrity and privacy controls |
| PCI DSS | Mandatory requirements for anyone storing, processing or transmitting payment card data |
| NIST CSF | Framework of functions: Govern, Identify, Protect, Detect, Respond, Recover |
| NIS2 / DORA | EU rules for essential services and the financial sector |
Compliance is a floor, not a ceiling: passing an audit shows that certain controls exist, not that the organisation is secure. Used well, frameworks provide structure and a shared language between engineers, management and customers.
Go further: UK and EU Law covers the Computer Misuse Act and NIS2, including its incident-reporting deadlines.
Test yourself
Which GDPR principle says you should collect only the data you need for a purpose?
- Accuracy
- Data minimisation
- Accountability
- Portability
Is pseudonymised data still personal data under GDPR?
- No, never
- Yes, because it can be re-linked with additional information
- Only if encrypted
- Only for health data
Under GDPR, how quickly must most personal-data breaches be reported to the supervisory authority?
- Within 24 hours
- Within 72 hours of becoming aware
- Within 30 days
- Only if data was encrypted