| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="Redactor.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System.Text.RegularExpressions; |
| | | 8 | | |
| | | 9 | | namespace MyNet.Text.Redaction; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Provides common text redaction transforms. |
| | | 13 | | /// </summary> |
| | | 14 | | public static partial class Redactor |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Gets a generic redactor that preserves the first and last two characters. |
| | | 18 | | /// </summary> |
| | 3 | 19 | | public static ITextRedactorTransform Generic { get; } = new FixedMaskTextRedactor(new() { ShowStart = 2, ShowEnd = 2 |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Gets an email redactor that preserves first character and domain suffix. |
| | | 23 | | /// </summary> |
| | 3 | 24 | | public static ITextRedactorTransform Email { get; } = new RegexTextRedactor(EmailRegex()); |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Gets a phone redactor that masks all digits except the last two. |
| | | 28 | | /// </summary> |
| | 3 | 29 | | public static ITextRedactorTransform Phone { get; } = new RegexTextRedactor(PhoneRegex(), "*"); |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Gets a card number redactor that masks all digits except the last four. |
| | | 33 | | /// </summary> |
| | 3 | 34 | | public static ITextRedactorTransform CardNumber { get; } = new RegexTextRedactor(CardNumberRegex(), "*"); |
| | | 35 | | |
| | | 36 | | [GeneratedRegex("(?<=.).(?=[^@]*?@)|(?<=@.).(?=[^.]*\\.)", RegexOptions.Compiled)] |
| | | 37 | | private static partial Regex EmailRegex(); |
| | | 38 | | |
| | | 39 | | [GeneratedRegex(@"\d(?=(?:\D*\d){2})", RegexOptions.Compiled)] |
| | | 40 | | private static partial Regex PhoneRegex(); |
| | | 41 | | |
| | | 42 | | [GeneratedRegex(@"\d(?=(?:\D*\d){4})", RegexOptions.Compiled)] |
| | | 43 | | private static partial Regex CardNumberRegex(); |
| | | 44 | | } |
| | | 45 | | |