- Notifications
You must be signed in to change notification settings - Fork0
A fast, simple, zero allocation redacting library for .NET, with no non-Microsoft dependencies.
License
nikouu/ZeroRedact
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A fast, simple, zero allocation redacting library for .NET, with no non-Microsoft dependencies.
Fully or partially redact:
- Strings
- Email addresses
- Credit card numbers
- Dates
- Phone numbers
- IPV4 addresses
- IPV6 addresses
- MAC addresses
Read more in theofficial docs.
Install viaNuGet:
dotnet add package Nikouu.ZeroRedact
The following shows example redactions with varying configs. For more see theofficial docs.
varredactor=newRedactor();// returns "*************"varstringResult=redactor.RedactString("Hello, World!");// returns "*****@*******.***"varemailAddressResult=redactor.RedactEmailAddress("email@example.com");// returns "****-****-****-1111"varcreditCardOptions=newCreditCardRedactorOptions{RedactorType=CreditCardRedaction.ShowLastFour};varcreditCardResult=redactor.RedactCreditCard("4111-1111-1111-1111",creditCardOptions);// returns based on current culture// en-NZ: "**/06/2023"// en-US: "6/**/2023"// ja-JP: "2023/06/**"// InvariantCulture: "06/**/2023"vardateOptions=newDateRedactorOptions{RedactorType=DateRedaction.Day};vardateResult=redactor.RedactDate(newDateTime(2023,6,15),dateOptions);// returns "###-###-####"varphoneNumberOptions=newPhoneNumberRedactorOptions{RedactionCharacter='#'};varphoneNumberResult=redactor.RedactPhoneNumber("212-456-7890");// returns "@@@.@.@.146"varipv4AddressOptions=newIPv4AddressRedactorOptions{RedactionCharacter='@',RedactorType=IPv4AddressRedaction.ShowLastOctet};varipv4Result=redactor.RedactIPv4Address("192.0.2.146",ipv4AddressOptions);// returns "****:****:****:****:****:****:****:****"varipv6Result=redactor.RedactIPv6Address("2001:0000:130F:0000:0000:09C0:876A:130B");// "**:**:**:**:**:**"varmacResult=redactor.RedactMACAddress("00:B0:D0:63:C2:26");
Or in the case of a service registration withIServiceCollection
:
// Defaultbuilder.Services.AddZeroRedact();// Or to configurebuilder.Services.AddZeroRedact(newRedactorOptions{CreditCardRedactorOptions=newCreditCardRedactorOptions{RedactorType=CreditCardRedaction.ShowLastFour},EmailAddressRedactorOptions=newEmailAddressRedactorOptions{RedactorType=EmailAddressRedaction.ShowFirstCharacters},DateRedactorOptions=newDateRedactorOptions{RedactorType=DateRedaction.Day},PhoneNumberRedactorOptions=newPhoneNumberRedactorOptions{RedactorType=PhoneNumberRedaction.ShowLastFour}});
ZeroRedact can be configured in two ways: via constructor, and via a redaction method. Both using the appropriate redactor option object.
Configuration has three layers with least to most priority:
- Base defaults within the
Redactor
object. - Passed in options to the
Redactor
object constructor - Passed in options to a redaction method at the time of redaction
This example shows changing the base defaults. This will apply to every redaction, whether it's a string or email address, etc.
// Example 1: Changing base defaultsvaroptions=newRedactorOptions{RedactionCharacter='X'};varredactor=newRedactor(options);// returns "XXXXXXXXXXXXX"varresult=redactor.RedactString("Hello, World!");
This example showsStringRedactorOptions
being passed into theRedactor
constructor. Only string redactions will now use 'A' as the redaction character, all others will continue to use the default character of '*'.
// Example 2: Changing specific redaction type optionsvaroptions=newRedactorOptions{StringRedactorOptions=newStringRedactorOptions{RedactionCharacter='A'}};varredactor=newRedactor(options);// returns "AAAAAAAAAAAAA"varresult=redactor.RedactString("Hello, World!");
This example shows passing inStringRedactorOptions
at the time of redaction, which takes precedence over the constructor options.
// Example 3: Changing redaction options at redaction timevaroptions=newRedactorOptions{StringRedactorOptions=newStringRedactorOptions{RedactionCharacter='A'}};varredactor=newRedactor(options);varspecificOptions=newStringRedactorOptions{RedactionCharacter='B'};// returns "BBBBBBBBBBBBB"varresult=redactor.RedactString("Hello, World!",specificOptions);
Users should have a fine grained customization experience which allows for an easy to reuse item that can setup default behaviours but when needed, can adapt to the need of the redaction call at the time.
Redacting will not throw exceptions - that is, the string manipulation logic. Any exception during the redacting process will return a fixed length redaction with a default redacting character. This is to avoid disrupting the important real work happening in a user's codebase while still preventing PII from being exposed.
However exceptions can occur by passing invalid options to the redactor, whether that's via the constructor, or via a redaction method.
Keeps project light, easier to maintain, easier to get approved by having Microsoft-only packages for less supply chain issues. Read more from Microsoft viaBest practices for a secure software supply chain.
ZeroRedact can be integrated intoMicrosoft.Extensions.Compliance.Redaction to provide full and partial redacting.
SeeCompliance.Redaction-with-ZeroRedact for an example project.
Seebenchmarks.
Currently not looking for contributions, however issues for problems or feature suggestions are welcome for consideration.
About
A fast, simple, zero allocation redacting library for .NET, with no non-Microsoft dependencies.