DEV Community

丁久
丁久

Posted on • Originally published at dingjiu1989-hue.github.io

Data Masking and Redaction

This article was originally published on AI Study Room. For the full version with working code examples and related articles, visit the original post.

Data Masking and Redaction

Data Masking and Redaction

Data Masking and Redaction

Data Masking and Redaction

Data Masking and Redaction

Data Masking and Redaction

Data Masking and Redaction

Data Masking and Redaction

Data Masking and Redaction

Data Masking and Redaction

Data Masking and Redaction

Introduction

Data masking protects sensitive information by replacing it with realistic but fictional data. Unlike encryption, masked data is permanently de-identified — it cannot be reversed to recover the original value. Organizations use masking for development, testing, analytics, and compliance with privacy regulations like GDPR and CCPA.

Static Data Masking

Static masking creates a sanitized copy of a production database for non-production use.

import hashlib

import random

import string

class StaticDataMasker:

def init(self, seed=42):

self.seed = seed

self.rng = random.Random(seed)

def mask_email(self, email):

"""Generate a consistent fake email from the real one."""

local, domain = email.split('@')

hash_obj = hashlib.sha256(email.encode())

fake_local = hash_obj.hexdigest()[:12]

return f"{fake_local}@masked-domain.com"

def mask_phone(self, phone):

"""Mask phone number, keeping format but replacing digits."""

masked = []

for char in phone:

if char.isdigit():

masked.append(str(self.rng.randint(0, 9)))

else:

masked.append(char)

return ''.join(masked)

def mask_credit_card(self, cc_number):

"""Mask all but last 4 digits."""

clean = cc_number.replace(' ', '').replace('-', '')

if len(clean) >= 4:

masked = '*' * (len(clean) - 4) + clean[-4:]

else:

masked = clean

return masked

def mask_name(self, name):

"""Replace name with a fake name."""

first_names = ['John', 'Jane', 'Alex', 'Sarah', '


Read the full article on AI Study Room for complete code examples, comparison tables, and related resources.

Found this useful? Check out more developer guides and tool comparisons on AI Study Room.

Top comments (0)