Skip to content

Replace fields in json, replacing by something, don't care if property is in depth objects. Very useful to replace passwords credit card number, etc.

License

Notifications You must be signed in to change notification settings

ThiagoBarradas/jsonmasking

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status Quality Gate Status Coverage NuGet Downloads NuGet Version

Json Masking

Replace fields in json, replacing by something, don't care if property is in depth objects. Very useful to replace passwords, credit card number, etc.

This library matching insensitive values with field namespaces. You can use wildcard * to allow any char in pattern;

Sample

- Completely masking some of the properties

var example = new 
{
	SomeValue = "Demo",
	Password = "SomePasswordHere",
	DepthObject = new 
	{
		Password = "SomePasswordHere2",
		Card = new 
		{
			Number = "555500022223333"
		}
	},
	CreditCardNumber = "5555000011112222",
	Card = new 
	{
		Number = "555500022223333"
	}
};

var exampleAsString = JsonConvert.Serialize(example); // value must be a json string to masked

// note that password is only replaced when is in root path
var blacklist = new string[] { "password", "card.number", "*.card.number" "creditcardnumber" };
var mask = "******";

var maskedExampleAsString = exampleAsString.MaskFields(blacklist, mask);

Console.WriteLine(maskedExampleAsString);

Output

{
	"SomeValue" : "Demo",
	"Password" : "******",
	"DepthObject" : {
		"Password" : "SomePasswordHere2",
		"Card" : {
			"Number" : "******"
		}
	},
	"CreditCardNumber" : "******",
	"Card" : {
		"Number" : "******"
	}
}
- Partially masking some of the properties

var example = new 
{
	SomeValue = "Demo",
	Password = "SomePasswordHere",
	DepthObject = new 
	{
		Password = "SomePasswordHere2",
		Card = new 
		{
			Number = "555500022223333"
		}
	},
	CreditCardNumber = "5555000011112222",
	Card = new 
	{
		Number = "555500022223333"
	}
};

var blacklistPartial = new Dictionary<string, Func<string, string>>(StringComparer.OrdinalIgnoreCase) // The key is the property to be partially masked and the value is the function to be applied.
{
    { "*card.number", text =>  // Note that the property "*card.number" is also in the blacklist. If the property only exists in the blacklistPartial, it will not be masked.
        Regex.Replace(
            text,
            @"(\d{4,5})[ -|]?(\d{3,6})[ -|]?(\d{3,5})[ -|]?(\d{3,4})",
            match => $"{match.Value.Substring(0, 6)}*****{match.Value.Substring(match.Value.Length - 4, 4)}")
    }
};

var exampleAsString = JsonConvert.Serialize(example); // The value must be a JSON string to be masked.

// Note that the password is only replaced when it is in the root path.
var blacklist = new string[] { "password", "*card.number", "creditcardnumber" };
var mask = "******";

var maskedExampleAsString = exampleAsString.MaskFields(blacklist, mask, blacklistPartial); // The blacklistPartial is optional. If provided, it will apply the mask only if the property is also in the blacklist.

Console.WriteLine(maskedExampleAsString);

Output

{
	"SomeValue" : "Demo",
	"Password" : "******",
	"DepthObject" : {
		"Password" : "SomePasswordHere2",
		"Card" : {
			"Number" : "555500*****3333"
		}
	},
	"CreditCardNumber" : "******",
	"Card" : {
		"Number" : "555500*****3333"
	}
}

Install via NuGet

PM> Install-Package JsonMasking

How can I contribute?

Please, refer to CONTRIBUTING

Found something strange or need a new feature?

Open a new Issue following our issue template ISSUE_TEMPLATE

Changelog

See in nuget version history

Did you like it? Please, make a donate :)

if you liked this project, please make a contribution and help to keep this and other initiatives, send me some Satochis.

BTC Wallet: 1G535x1rYdMo9CNdTGK3eG6XJddBHdaqfX

1G535x1rYdMo9CNdTGK3eG6XJddBHdaqfX

About

Replace fields in json, replacing by something, don't care if property is in depth objects. Very useful to replace passwords credit card number, etc.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages