{"id":28287873,"url":"https://github.com/li-xiaoyao/sensitivewords","last_synced_at":"2025-10-29T03:13:06.726Z","repository":{"id":65153883,"uuid":"584610752","full_name":"LI-XIAOYAO/SensitiveWords","owner":"LI-XIAOYAO","description":"字符串脱敏，输入输出多种处理模式，支持字符、拼音、简拼、同音字等替换模式，支持正则。","archived":false,"fork":false,"pushed_at":"2024-08-25T07:48:26.000Z","size":373,"stargazers_count":7,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-21T22:14:33.231Z","etag":null,"topics":["csharp","dotnet","dotnetcore","netcore","sensitivewords","string","words"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LI-XIAOYAO.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-01-03T03:49:23.000Z","updated_at":"2024-10-16T06:09:40.000Z","dependencies_parsed_at":"2024-05-03T10:47:08.299Z","dependency_job_id":"9d79f127-1d98-4a7b-a62f-5654cb5aa3d2","html_url":"https://github.com/LI-XIAOYAO/SensitiveWords","commit_stats":{"total_commits":16,"total_committers":1,"mean_commits":16.0,"dds":0.0,"last_synced_commit":"5a3564f94d329516c081132c360d207f92b47c4d"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/LI-XIAOYAO/SensitiveWords","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LI-XIAOYAO%2FSensitiveWords","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LI-XIAOYAO%2FSensitiveWords/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LI-XIAOYAO%2FSensitiveWords/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LI-XIAOYAO%2FSensitiveWords/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LI-XIAOYAO","download_url":"https://codeload.github.com/LI-XIAOYAO/SensitiveWords/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LI-XIAOYAO%2FSensitiveWords/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260494394,"owners_count":23017694,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","dotnet","dotnetcore","netcore","sensitivewords","string","words"],"created_at":"2025-05-21T22:14:33.377Z","updated_at":"2025-10-29T03:13:06.712Z","avatar_url":"https://github.com/LI-XIAOYAO.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SensitiveWords\n字符串脱敏，输入输出多种处理模式，支持字符、拼音、简拼、同音字等替换模式，支持正则。\n\u003c!--TOC--\u003e\n- [安装](#安装)\n- [使用](#使用)\n- [示例](#示例)\n- [其他](#其他)\n\u003c!--/TOC--\u003e\n\n#### 安装\n\u003e Install-Package SensitiveWords\n\n#### 使用\n```c#\n// Web注入方式，根据配置自动处理输入输出\nservices.AddSensitiveWords(sensitiveWordsOptions =\u003e\n{\n    sensitiveWordsOptions.Add(new SensitiveWordsOptions(HandleOptions.Input, ReplaceOptions.Character, \"*\", true, false, GroupReplaceOptions.GroupPriority)\n        .Add(\"牛皮|反对\").Build());\n    sensitiveWordsOptions.Add(new SensitiveWordsOptions(HandleOptions.Output, ReplaceOptions.Character, \"*\", true, false, GroupReplaceOptions.GroupPriority)\n        .AddRegex(@\"\\w{4}(\\w+(?:[-+.]\\w+)*)@\\w+(?:[-.]\\w+)*\\.\\w+(?:[-.]\\w+)*\")\n        .AddRegex(@\"(?: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}\")\n\t\t.Build()\n    );\n});\n\n// 直接调用\nSensitiveWordsResolver.Desensitize(\"...\");\n// 扩展方法\n\"...\".Desensitize();\n\n```\n\n#### 示例\n```c#\nusing SensitiveWords;\n...\n{\n\t// 初始化配置\n\tSensitiveWordsResolver.Config(options =\u003e\n\t{\n\t\toptions.Add(new SensitiveWordsOptions(HandleOptions.Default, ReplaceOptions.Character, \"*\", true).Add(\"啊啊|zf|666\").Build());\n\t\toptions.Add(new SensitiveWordsOptions(HandleOptions.Default, ReplaceOptions.Character, \"?\", true).Add(\"操|文|NM\").Build());\n\t\toptions.Add(new SensitiveWordsOptions(HandleOptions.Default | HandleOptions.Output, ReplaceOptions.Character, \"*\", true, groupReplaceOptions: GroupReplaceOptions.GroupPriority)\n\t\t\t.AddRegex(@\"\\w{4}(\\w+(?:[-+.]\\w+)*)@\\w+(?:[-.]\\w+)*\\.\\w+(?:[-.]\\w+)*\")\n\t\t\t.AddRegex(@\"(?: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}\")\n\t\t\t.Build()\n\t\t);\n\t});\n\n\tvar text = \"zf这是什么操作啊啊……666 nm\";\n\t// 扩展方法调用\n\ttext.Desensitize(); // Output: **这是什么?作**……*** ??\n\t// 方法调用\n\tSensitiveWordsResolver.Desensitize(text); // Output: **这是什么?作**……*** ??\n\n\tvar mail = \"test123@mail.com\";\n\tvar phone = \"13623332333\";\n\n\t// 脱敏默认\n\tmail.Desensitize(); // Output: test***@mail.com\n\t// 脱敏输入\n\tmail.DesensitizeInput(); // Output: test123@mail.com\n\tphone.DesensitizeInput(); // Output: 13623332333\n\t// 脱敏输出\n\tmail.DesensitizeOutput(); // Output: test***@mail.com\n\tphone.DesensitizeOutput(); // Output: 136****2333\n\t// 脱敏所有\n\tmail.DesensitizeAll(); // Output: test***@mail.com\n\tphone.DesensitizeAll(); // Output: 136****2333\n\n\tSensitiveWordsResolver.Config(options =\u003e\n\t\toptions.Add(new SensitiveWordsOptions(HandleOptions.Default, ReplaceOptions.Character, \"*\", true, groupReplaceOptions: GroupReplaceOptions.GroupPriority)\n\t\t\t.AddRegex(@\"政治(?!老师|家)\").Build()\n\t\t)\n\t);\n\t\"一个政治老师在谈政治话题\".Desensitize(); // Output: 一个政治老师在谈**话题\n\n\t// 支持拼音等替换模式\n\tSensitiveWordsResolver.Config(options =\u003e\n\t{\n\t\toptions.Add(new SensitiveWordsOptions(HandleOptions.Default, ReplaceOptions.PinYin, null, true, groupReplaceOptions: GroupReplaceOptions.GroupPriority).Add(\"尘埃|酒\").Build());\n\t\toptions.Add(new SensitiveWordsOptions(HandleOptions.Default, ReplaceOptions.JianPin, null, true, groupReplaceOptions: GroupReplaceOptions.GroupPriority).Add(\"菩提|本来\").Build());\n\t\toptions.Add(new SensitiveWordsOptions(HandleOptions.Default, ReplaceOptions.Homophone, null, true, groupReplaceOptions: GroupReplaceOptions.GroupPriority).Add(\"终身\").Build());\n\t});\n\n\t\"菩提本无树，明镜亦非台。本来无一物，何处惹尘埃！知足常乐，终身不辱，今朝有酒今朝醉，明日愁来明日愁。\".Desensitize(); // Output: pt本无树，明镜亦非台。bl无一物，何处惹chenai！知足常乐，中伸不辱，今朝有jiu今朝醉，明日愁来明日愁。\n}\n```\n\n#### 其他\n特性：\n- 忽略特性 `IgnoreSensitiveWordsAttribute`\n- 指定处理标签特性 `SensitiveWordsAttribute` 通过 `new SensitiveWordsOptions().SetTag(\"Tag\")` 配置标签\n\n`SensitiveWordsOptions`：敏感词配置选项\n- 属性\n\t- `HandleOptions` 处理选项\n\t\t- `Default`：默认\n\t\t- `Input`：输入\n\t\t- `Output`：输出\n\t- `ReplaceOptions` 替换选项\n\t\t- `Character`：字符\n\t\t- `PinYin`：拼音\n\t\t- `JianPin`：简拼\n\t\t- `Homophone`：同音字\n\t- `Character` 替换字符串 `ReplaceOptions.Character` 时生效\n\t- `IgnoreCase` 忽略大小写\n\t- `ReplaceSingle` 替换为单个不补位，默认 `false` 补位至敏感词长度\n\t- `GroupReplaceOptions` 组替换选项\n\t\t- `Default`：默认替换匹配到的值\n\t\t- `GroupOnly`：只替换正则组\n\t\t- `GroupPriority`：组优先，存在组就替换组不存在就默认替换\n\t- `IsMaxMatch`：优先匹配最大长度（正则下不生效）\n\t- `WhiteSpaceOptions`：空字符选项\n    \t- `Default`：默认不处理\n    \t- `IgnoreWhiteSpace`：忽略空字符\n    \t- `IgnoreNewLine`：忽略换行符\n\t- `Tag` 标签，可通过特性 `SensitiveWordsAttribute` 指定处理\n- 方法\n\t- `Add` 添加敏感词，多个英文竖线|分隔，包含竖线需要转义\n\t- `AddRegex` 添加正则敏感词，多个英文竖线|分隔，包含竖线需要转义\n\t- `AddFile` 添加文件敏感词，同文本规则\n\t- `AddRegexFile` 添加正则文件敏感词\n\t- `SetTag` 设置标签\n\t- `Remove` 删除敏感词\n\t- `Build` 构建敏感词，添加删除敏感词后调用生效\n\n多音字词组添加：`SensitiveWordsResolver.RegisterHomophoneRegexWordGroup(...)` 存在多音字时无法区分，可通过添加词组确定\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fli-xiaoyao%2Fsensitivewords","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fli-xiaoyao%2Fsensitivewords","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fli-xiaoyao%2Fsensitivewords/lists"}