Files

34 lines
1.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""敏感信息脱敏工具(工作台统一出口,避免各接口遗漏导致合规风险)。
两层脱敏(PRD §4.2.2):
- 常规敏感(手机号中 4 位、姓名/证件常规掩码):默认掩码展示;投顾查看完整值时,
调用方负责写审计 action=view_sensitive;
- 强敏感(完整身份证号、银行卡号):V1.0 一律只返回掩码,不提供完整值。
"""
from __future__ import annotations
def mask_phone(phone: str | None) -> str:
"""手机号:保留前 3 后 4,中间打码(138****1234)。"""
if not phone:
return ""
if len(phone) >= 7:
return phone[:3] + "****" + phone[-4:]
return "****"
def mask_idcard(idcard: str | None) -> str:
"""身份证:保留前 4 后 4,中间打码。"""
if not idcard:
return ""
if len(idcard) < 8:
return "****"
return idcard[:4] + "**********" + idcard[-4:]
def mask_name(name: str | None) -> str:
"""姓名:仅保留首字符(姓氏),其余打码。"""
if not name:
return ""
return name[0] + "*" * (len(name) - 1) if len(name) > 1 else name