Protecting sensitive user information during development and QA testing is a critical compliance standard. Data breaches frequently occur in staging environments due to the use of raw production databases. This guide covers how to implement data masking, hashing, and obfuscation to secure your databases.
Data Masking vs. Hashing vs. Encryption
Before implementing protection rules, it is essential to understand the difference between the three primary columns protection techniques:
- Encryption: Two-way operation. Data is transformed using an algorithm and key, and can be decrypted back to its original form. Required for active production storage.
- Hashing: One-way operation. Input values are converted into a fixed-length string (fingerprint). Ideal for passwords or match validations.
- Data Masking: Obfuscating data by replacing characters or columns with fake synthetic equivalents. It is irreversible and leaves the data structure intact for testing.
How to implement Data Masking in Python
Here is how you can mask sensitive email and telephone numbers using regex replacement patterns in Python:
import re
def mask_email(email):
# Mask email username keeping domain name visible
if "@" not in email:
return "******"
username, domain = email.split("@", 1)
if len(username) <= 2:
masked_username = "**"
else:
masked_username = username[0] + "*" * (len(username) - 2) + username[-1]
return f"{masked_username}@{domain}"
def mask_phone(phone):
# Keep country dial code, replace trailing numbers
clean_phone = re.sub(r'[^\d+]', '', phone)
if len(clean_phone) > 4:
return clean_phone[:-4] + "XXXX"
return "XXXX"
print(mask_email("john.doe@example.com")) # j******e@example.com
print(mask_phone("+1-555-019-2834")) # +1555019XXXX
Applying Data Masking rules in SQL queries
You can execute data masking on SQL datasets during staging migrations. Below is an example matching standard PostgreSQL query syntax:
-- Mask user names and credit card tails
SELECT
id,
CONCAT(LEFT(first_name, 1), '*****') AS masked_first_name,
CONCAT('XXXX-XXXX-XXXX-', RIGHT(credit_card_number, 4)) AS masked_card,
email
FROM staging_users;
Using local tools for secure mask execution
If you don't want to write custom regex scripts, you can use Sylvaera's interactive PII Anonymizer or Data Masking tools. Since all transformations run local in-memory inside your browser, no data gets uploaded to our servers, ensuring GDPR compliance.