hutool.core.util.desensitized¶
Module Contents¶
Classes¶
数据脱敏工具类 |
API¶
- class hutool.core.util.desensitized.DesensitizedUtil¶
数据脱敏工具类
- static chinese_name(name: str) str¶
中文姓名脱敏:保留姓,名替换为*
示例:张三 -> 张*,欧阳娜娜 -> 欧***
- 参数:
name – 中文姓名
- 返回:
脱敏后的姓名
- static id_card(idcard: str, before: int = 3, after: int = 4) str¶
身份证脱敏
保留前before位和后after位,中间用*替换
- 参数:
idcard – 身份证号
before – 保留前N位,默认3
after – 保留后N位,默认4
- 返回:
脱敏后的身份证号
- static mobile_phone(phone: str) str¶
手机号脱敏:138****1234
- 参数:
phone – 手机号
- 返回:
脱敏后的手机号
- static fixed_phone(phone: str) str¶
固定电话脱敏
保留区号和后4位,中间用*替换
- 参数:
phone – 固定电话
- 返回:
脱敏后的固定电话
- static email(email: str) str¶
邮箱脱敏:t***@example.com
保留@前的首字母和@后的完整域名
- 参数:
email – 邮箱地址
- 返回:
脱敏后的邮箱
- static address(address: str, sensitive_size: int = 6) str¶
地址脱敏
从敏感_size位之前开始替换为*
- 参数:
address – 地址
sensitive_size – 需要脱敏的字符数,默认6
- 返回:
脱敏后的地址
- static bank_card(card: str) str¶
银行卡脱敏
保留前4位和后4位,中间用*替换
- 参数:
card – 银行卡号
- 返回:
脱敏后的银行卡号
- static password(password: str) str¶
密码脱敏:全部替换为*
- 参数:
password – 密码
- 返回:
脱敏后的密码(全为*)
- static car_license(license_no: str) str¶
车牌号脱敏
保留前2位和后1位,中间用*替换
- 参数:
license_no – 车牌号
- 返回:
脱敏后的车牌号
- static ipv4(ipv4: str) str¶
IPv4脱敏
保留前两段,后两段替换为*
- 参数:
ipv4 – IPv4地址
- 返回:
脱敏后的IPv4地址
- static license_plate(plate: str) str¶
车牌号脱敏
保留省份简称和地区代码,后面替换为*
- 参数:
plate – 车牌号
- 返回:
脱敏后的车牌号
- static first_mask(str_val: str, mask_char: str = '*', mask_len: int = 4) str¶
对字符串前 N 个字符进行脱敏。
- 参数:
str_val – 原始字符串
mask_char – 替换字符,默认
"*"mask_len – 脱敏长度,默认 4
- 返回:
脱敏后的字符串
- static ipv6(ipv6: str) str¶
IPv6 地址脱敏,保留前两段,后两段替换为
*。- 参数:
ipv6 – IPv6 地址
- 返回:
脱敏后的 IPv6 地址
- static passport(passport: str) str¶
护照号脱敏,保留首位和末位,中间替换为
*。- 参数:
passport – 护照号
- 返回:
脱敏后的护照号
- static credit_code(code: str) str¶
统一社会信用代码脱敏,保留前 6 位和后 4 位。
- 参数:
code – 统一社会信用代码
- 返回:
脱敏后的代码
- static clear_mask(s: str, replacement: str = '*') str¶
清空字符串,全部替换为指定字符。
- 参数:
s – 原始字符串
replacement – 替换字符,默认 ‘*’
- 返回:
替换后的字符串
- static clear_to_null(s: str)¶
清空字符串,返回 None。
- 参数:
s – 原始字符串
- 返回:
None
- static desensitized(s: str, start: int, end: int, replacement: str = '*') str¶
通用脱敏:将指定区间内的字符替换为指定字符。
- 参数:
s – 原始字符串
start – 开始位置(包含)
end – 结束位置(不包含)
replacement – 替换字符,默认 ‘*’
- 返回:
脱敏后的字符串
- static user_id(user_id_val) str¶
用户 ID 脱敏。
- 参数:
user_id_val – 用户 ID
- 返回:
脱敏后的用户 ID