SensitiveWords 1.0.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package SensitiveWords --version 1.0.4                
NuGet\Install-Package SensitiveWords -Version 1.0.4                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SensitiveWords" Version="1.0.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SensitiveWords --version 1.0.4                
#r "nuget: SensitiveWords, 1.0.4"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SensitiveWords as a Cake Addin
#addin nuget:?package=SensitiveWords&version=1.0.4

// Install SensitiveWords as a Cake Tool
#tool nuget:?package=SensitiveWords&version=1.0.4                

SensitiveWords

字符串脱敏,输入输出多种处理模式,支持字符、拼音、简拼、同音字等替换模式,支持正则。

安装

Install-Package SensitiveWords

使用
// Web注入方式,根据配置自动处理输入输出
services.AddSensitiveWords(sensitiveWordsOptions =>
{
    sensitiveWordsOptions.Add(new SensitiveWordsOptions(HandleOptions.Input, ReplaceOptions.Character, "*", true, false, GroupReplaceOptions.GroupPriority)
        .Add("牛皮|反对"));
    sensitiveWordsOptions.Add(new SensitiveWordsOptions(HandleOptions.Output, ReplaceOptions.Character, "*", true, false, GroupReplaceOptions.GroupPriority)
        .Add(@"\w{4}(\w+(?:[-+.]\w+)*)@\w+(?:[-.]\w+)*\.\w+(?:[-.]\w+)*")
        .Add(@"(?:13[0-9]|14[0-14-9]|15[0-35-9]|16[25-7]|17[0-8]|18[0-9]|19[0-35-9])(\d{4})\d{4}")
    );
});

// 直接调用
SensitiveWordsResolver.Desensitize("...");
// 扩展方法
"...".Desensitize();

示例
using SensitiveWords;
...
{
	// 初始化配置
	SensitiveWordsResolver.Config(options =>
	{
		options.Add(new SensitiveWordsOptions(HandleOptions.Default, ReplaceOptions.Character, "*", true).Add("啊啊|zf|666"));
		options.Add(new SensitiveWordsOptions(HandleOptions.Default, ReplaceOptions.Character, "?", true).Add("操|文|NM"));
		options.Add(new SensitiveWordsOptions(HandleOptions.Default | HandleOptions.Output, ReplaceOptions.Character, "*", true, groupReplaceOptions: GroupReplaceOptions.GroupPriority)
			.Add(@"\w{4}(\w+(?:[-+.]\w+)*)@\w+(?:[-.]\w+)*\.\w+(?:[-.]\w+)*")
			.Add(@"(?:13[0-9]|14[0-14-9]|15[0-35-9]|16[25-7]|17[0-8]|18[0-9]|19[0-35-9])(\d{4})\d{4}")
		);
	});

	var text = "zf这是什么操作啊啊……666 nm";
	// 扩展方法调用
	text.Desensitize(); // Output: **这是什么?作**……*** ??
	// 方法调用
	SensitiveWordsResolver.Desensitize(text); // Output: **这是什么?作**……*** ??

	var mail = "test123@mail.com";
	var phone = "13623332333";

	// 脱敏默认
	mail.Desensitize(); // Output: test***@mail.com
	// 脱敏输入
	mail.DesensitizeInput(); // Output: test123@mail.com
	phone.DesensitizeInput(); // Output: 13623332333
	// 脱敏输出
	mail.DesensitizeOutput(); // Output: test***@mail.com
	phone.DesensitizeOutput(); // Output: 136****2333
	// 脱敏所有
	mail.DesensitizeAll(); // Output: test***@mail.com
	phone.DesensitizeAll(); // Output: 136****2333

	SensitiveWordsResolver.Config(options =>
		options.Add(new SensitiveWordsOptions(HandleOptions.Default, ReplaceOptions.Character, "*", true, groupReplaceOptions: GroupReplaceOptions.GroupPriority)
			.Add(@"政治(?!老师|家)")
		)
	);
	"一个政治老师在谈政治话题".Desensitize(); // Output: 一个政治老师在谈**话题

	// 支持拼音等替换模式
	SensitiveWordsResolver.Config(options =>
	{
		options.Add(new SensitiveWordsOptions(HandleOptions.Default, ReplaceOptions.PinYin, null, true, groupReplaceOptions: GroupReplaceOptions.GroupPriority).Add("尘埃|酒"));
		options.Add(new SensitiveWordsOptions(HandleOptions.Default, ReplaceOptions.JianPin, null, true, groupReplaceOptions: GroupReplaceOptions.GroupPriority).Add("菩提|本来"));
		options.Add(new SensitiveWordsOptions(HandleOptions.Default, ReplaceOptions.Homophone, null, true, groupReplaceOptions: GroupReplaceOptions.GroupPriority).Add("终身"));
	});

	"菩提本无树,明镜亦非台。本来无一物,何处惹尘埃!知足常乐,终身不辱,今朝有酒今朝醉,明日愁来明日愁。".Desensitize(); // Output: pt本无树,明镜亦非台。bl无一物,何处惹chenai!知足常乐,中伸不辱,今朝有jiu今朝醉,明日愁来明日愁。
}
其他

特性:

  • 忽略特性 IgnoreSensitiveWordsAttribute
  • 指定处理标签特性 SensitiveWordsAttribute 通过 new SensitiveWordsOptions().SetTag("Tag") 配置标签

SensitiveWordsOptions:敏感词配置选项

  • 属性
    • HandleOptions 处理选项
      • Default:默认
      • Input:输入
      • Output:输出
    • ReplaceOptions 替换选项
      • Character:字符
      • PinYin:拼音
      • JianPin:简拼
      • Homophone:同音字
    • Character 替换字符串 ReplaceOptions.Character 时生效
    • IgnoreCase 忽略大小写
    • ReplaceSingle 替换为单个不补位,默认 false 补位至敏感词长度
    • GroupReplaceOptions 组替换选项
      • Default:默认替换匹配到的值
      • GroupOnly:只替换正则组
      • GroupPriority:组优先,存在组就替换组不存在就默认替换
    • IsMaxMatch:优先匹配最大长度(正则下不生效)
    • WhiteSpaceOptions:空字符选项
      • Default:默认不处理
      • IgnoreWhiteSpace:忽略空字符
      • IgnoreNewLine:忽略换行符
    • Tag 标签,可通过特性 SensitiveWordsAttribute 指定处理
  • 方法
    • Add 添加敏感词,多个英文竖线|分隔,包含竖线需要转义,支持正则
    • AddFile 添加文件敏感词,文本内容规则一致
    • SetTag 设置标签
    • Remove 删除敏感词

多音字词组添加:SensitiveWordsResolver.RegisterHomophoneRegexWordGroup(...) 存在多音字时无法区分,可通过添加词组确定

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.9 123 5/3/2024
1.0.8 353 3/6/2023
1.0.7 263 2/14/2023
1.0.6 266 2/5/2023
1.0.5 272 2/3/2023
1.0.4 261 2/3/2023
1.0.3 273 2/3/2023
1.0.2 319 1/4/2023
1.0.1 289 1/3/2023
1.0.0 289 1/3/2023