Microsoft Security Utilities Secret Masker

1.0.0b4 · active · verified Thu Apr 09

Microsoft Security Utilities - Secret Masker (version 1.0.0b4) is a Python library designed for the detection and masking of sensitive data. It provides built-in JSON-formatted detection rules, enabling users to identify and redact secrets using simple symbols or SHA256 hashes. This tool is part of Microsoft's internal security utilities and focuses on preventing secret exposure. It was last released on March 10, 2025, and is actively maintained in a beta state.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the SecretMasker with built-in detection rules, then use it to detect and mask secrets in an input string. It also shows an option for SHA256 hashing for masking.

from microsoft_security_utilities_secret_masker import SecretMasker, load_regex_patterns_from_json_file

# Load built-in detection rules
precisely_classified_regex_patterns = load_regex_patterns_from_json_file('PreciselyClassifiedSecurityKeys.json')
unclassified_regex_patterns = load_regex_patterns_from_json_file('UnclassifiedPotentialSecurityKeys.json')

# Combine patterns
regex_patterns = precisely_classified_regex_patterns.union(unclassified_regex_patterns)

# Construct secret masker with chosen patterns
secret_masker = SecretMasker(regex_patterns)

# Example usage
input_text = "My API key is sk-1234567890abcdef1234567890abcdef and my email is test@example.com"

detected_secrets = secret_masker.detect_secrets(input_text)
print(f"Detected secrets: {detected_secrets}")

processed_input = secret_masker.mask_secrets(input_text)
print(f"Masked input: {processed_input}")

# Example with custom masking character (e.g., SHA256)
processed_input_sha256 = secret_masker.mask_secrets(input_text, mask_with_sha256=True)
print(f"Masked with SHA256: {processed_input_sha256}")

view raw JSON →