Skip to main content

Privacy protection library for detecting and masking PII in text, logs, and files.

Project description

DataCloak 🔒

Privacy protection for Python applications — detect and mask PII in text, logs, files, and data pipelines.

PyPI version Python License: MIT Tests


DataCloak is a production-ready Python library for automatically detecting and masking Personally Identifiable Information (PII) — built for India-first compliance use cases (Aadhaar, PAN, UPI) while covering universal types like email and credit cards.

✨ Features

Capability Description
8 built-in detectors Aadhaar, PAN, Mobile, Email, UPI ID, Credit Card (Luhn), IFSC, IPv4/IPv6
3 masking modes partial (keep trailing chars), full (redaction tags), hash (SHA-256)
File scanning .txt, .log, .csv — extensible to PDF, DOCX, and more
Structured scan Returns findings dict without modifying original text
JSON reports Risk-level classified reports with per-type counts
CLI datacloak scan / mask / report commands
Pluggable detectors Subclass BaseDetector to add your own PII types

🚀 Installation

pip install datacloak

Requires Python 3.11+.


⚡ Quick Start

from datacloak import mask, scan

text = """
Aadhaar: 2345 6789 0123
PAN: ABCPE1234F
Email: alice@example.com
Phone: 9876543210
"""

# Partial masking (default) — keeps trailing characters visible
print(mask(text))

Output:

Aadhaar: XXXX XXXX 0123
PAN: XXXXX1234F
Email: a***@example.com
Phone: ******3210

📖 Usage Guide

1. Masking Modes

from datacloak import mask

text = "Contact: alice@example.com | Phone: 9876543210"

# Partial — show trailing characters (default)
mask(text, mode="partial")
# → 'Contact: a***@example.com | Phone: ******3210'

# Full — replace with descriptive redaction tags
mask(text, mode="full")
# → 'Contact: [EMAIL_REDACTED] | Phone: [PHONE_REDACTED]'

# Hash — SHA-256 digest (deterministic and irreversible)
mask(text, mode="hash")
# → 'Contact: [HASH:142d78e466cacab3] | Phone: [HASH:7619ee8cea49187f]'

2. Scan Without Masking

from datacloak import scan

findings = scan("Send invoice to billing@acme.com, call 9876543210")
print(findings)
# {
#   "email": ["billing@acme.com"],
#   "phone": ["9876543210"]
# }

3. File Scanning

from datacloak import scan_file

# Scan a plain text or log file
result = scan_file("application.log")

# Scan a CSV (each cell is scanned individually)
result = scan_file("customers.csv")

print(result.summary)
# {"email": 142, "phone": 38, "aadhaar": 5}

print(result.findings[0])
# FileFinding(email='alice@example.com' @customers.csv:line=2)

4. Report Generation

from datacloak import report

r = report(text, source_label="user_input")
print(r.to_json())
{
  "generated_at": "2026-06-01T10:23:00+00:00",
  "source": "user_input",
  "total_findings": 4,
  "summary": {
    "aadhaar": 1,
    "email": 1,
    "phone": 1,
    "pan": 1
  },
  "details": { ... },
  "risk_level": "MEDIUM"
}

Save to disk:

r.save("pii_report.json")

🖥️ Command-Line Interface

DataCloak ships a full CLI via datacloak:

# Scan a file and display findings as a table
datacloak scan customers.txt

# Scan with JSON output
datacloak scan --format json customers.txt

# Mask a file (writes customers.masked.txt by default)
datacloak mask customers.txt

# Mask with full-redaction mode, specify output file
datacloak mask customers.txt --mode full --output clean.txt

# Print masked output to stdout (pipe-friendly)
datacloak mask customers.txt --stdout | grep "REDACTED"

# Generate a JSON report
datacloak report customers.txt

# Save report to file
datacloak report customers.txt --output report.json

# Verbose logging
datacloak -v scan customers.txt

🔌 Writing a Custom Detector

DataCloak's detector framework is designed for extension. Subclass BaseDetector, set _pattern, and optionally override _validate():

import re
from datacloak.detectors import BaseDetector, Detection
from datacloak import scan

class PassportDetector(BaseDetector):
    name = "indian_passport"
    description = "Indian Passport Number (A-Z followed by 7 digits)"
    _pattern = re.compile(r"\b[A-Z]\d{7}\b")

# Use alongside built-in detectors
from datacloak.detectors import DEFAULT_DETECTORS

my_detectors = DEFAULT_DETECTORS + [PassportDetector()]
findings = scan("Passport: A1234567", detectors=my_detectors)
# {"indian_passport": ["A1234567"]}

Adding a new file format

from datacloak.file_scanner import FileHandler, register_handler
from pathlib import Path

class PDFHandler(FileHandler):
    extensions = (".pdf",)

    def extract_chunks(self, path: Path):
        # Use any PDF library (pdfplumber, PyMuPDF, etc.)
        import pdfplumber
        with pdfplumber.open(path) as pdf:
            for page_num, page in enumerate(pdf.pages, start=1):
                text = page.extract_text() or ""
                yield page_num, None, text

register_handler(PDFHandler())

# Now scan_file("document.pdf") works automatically

🕵️ Supported PII Types

Detector Example Validation
aadhaar 2345 6789 0123 12 digits, starts 2-9, space/hyphen/plain
pan ABCPE1234F AAAAA9999A format, valid entity code
phone 9876543210, +91 9876543210 10 digits, starts 6-9, optional country code
email alice@example.com RFC-5321 compliant
upi_id user@okaxis VPA format, non-email handles only
credit_card 4111 1111 1111 1111 13-19 digits, Luhn algorithm validated
ifsc HDFC0001234 4-alpha + 0 + 6-alphanumeric
ip_address 192.168.1.1, ::1 IPv4 (range-validated) and IPv6

🧪 Running Tests

# Clone the repo
git clone https://github.com/AtharvaUbhe46/DataCloak
cd datacloak

# Install dev dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Run with coverage
pytest --cov=datacloak --cov-report=term-missing

Target: ≥ 85% coverage.


📄 License

MIT License — Copyright © 2026 DataCloak Contributors.


🤝 Contributing

Contributions, issues, and feature requests are welcome! Please read the contributing guide and open a pull request.

  1. Fork the repository
  2. Create a feature branch: git checkout -b feat/my-detector
  3. Write your code and tests
  4. Run pytest and ensure coverage stays ≥ 85%
  5. Open a pull request

DataCloak — because privacy is not optional.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

datacloak-0.2.0.tar.gz (23.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

datacloak-0.2.0-py3-none-any.whl (25.8 kB view details)

Uploaded Python 3

File details

Details for the file datacloak-0.2.0.tar.gz.

File metadata

  • Download URL: datacloak-0.2.0.tar.gz
  • Upload date:
  • Size: 23.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.6

File hashes

Hashes for datacloak-0.2.0.tar.gz
Algorithm Hash digest
SHA256 ab547593a9e9ca5848abf5dd7e0984f95717a869fdf71e3df331e8559b073aed
MD5 dc39dae4409ce980450b32e9e3ff123d
BLAKE2b-256 2124f01313d31f2e6294791e33dcecc02d634294d13920cec492c6e0b0ab6e53

See more details on using hashes here.

File details

Details for the file datacloak-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: datacloak-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 25.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.6

File hashes

Hashes for datacloak-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c6f95d4f9f26bb30e9d8b30f0db3b35c82974e92a70b756e02ec7fd214ab0c04
MD5 9690abb67327414796dfe979737c5190
BLAKE2b-256 bfae4639a3742b0ce665788dfe37106e27046480b7afbc12860f841b3ea44ed8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page