Class ValueMaskers
ValueMasker.-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends ValueMasker>
TProvides information about theValueMaskerimplementation.static ValueMasker.StringMaskereachCharacterWith(String value) Masks all characters of a target string value with a static string value.static ValueMasker.NumberMaskereachDigitWith(int digit) Masks all digits of a target numeric value with a static digit.static ValueMasker.StringMaskerMasks a target string value (containing an email) while keeping some number of the prefix or suffix characters and the ability to keep the domain unmasked.static ValueMasker.AnyValueMaskernoop()Does not mask a target value (no-operation).static ValueMasker.AnyValueMaskerwith(boolean value) Masks a target value with a static boolean value.static ValueMasker.AnyValueMaskerwith(int value) Masks a target value with a static integer value.static ValueMasker.AnyValueMaskerMasks a target value with a static string value.static ValueMasker.AnyValueMaskerwithNull()Masks a target value withnull.static ValueMasker.AnyValueMaskerwithRawValueFunction(Function<String, String> masker) Masks a target value with the providedFunction.
-
Method Details
-
describe
Provides information about theValueMaskerimplementation. Which is useful for debugging and testing purposes.- See Also:
-
DescriptiveValueMasker
-
with
Masks a target value with a static string value.For example, "maskMe": "secret" -> "maskMe": "***".
-
with
Masks a target value with a static integer value.For example, "maskMe": 12345 -> "maskMe": 0.
-
with
Masks a target value with a static boolean value.For example, "maskMe": true -> "maskMe": false.
-
withNull
Masks a target value withnull. -
eachCharacterWith
Masks all characters of a target string value with a static string value.For example, "maskMe": "secret" -> "maskMe": "******".
Note: this implementation only replaces visible characters with a mask, meaning that JSON escape character ('\') will not count towards the length of the masked value and the unicode characters ('
\u1000'), including 4-byte UTF-8 characters ('\uD83D\uDCA9'), will only count as a single character in the masked value. -
eachDigitWith
Masks all digits of a target numeric value with a static digit.For example, "maskMe": 12345 -> "maskMe": 88888.
-
noop
Does not mask a target value (no-operation). Can be used if certain JSON value types do not need to be masked, for example, not masking booleans or numbers. -
email
public static ValueMasker.StringMasker email(int keepPrefixLength, int keepSuffixLength, boolean keepDomain, String mask) Masks a target string value (containing an email) while keeping some number of the prefix or suffix characters and the ability to keep the domain unmasked.For example:
- "maskMe": "agavlyukovskiy@gmail.com" -> "maskMe": "***@gmail.com"
- "maskMe": "agavlyukovskiy@gmail.com" -> "maskMe": "ag***"
- "maskMe": "agavlyukovskiy@gmail.com" -> "maskMe": "ag***@gmail.com"
- "maskMe": "agavlyukovskiy@gmail.com" -> "maskMe": "ag***iy@gmail.com"
- Parameters:
keepPrefixLength- amount of prefix characters to keep unmaskedkeepDomain- if true - the email domain will remain unmaskedmask- the static mask applied to the rest of the value
-
withRawValueFunction
Masks a target value with the providedFunction. The target value (as raw JSON literal) is passed into the function as a string regardless of the JSON type (string, numeric or a boolean). In case the target value is a JSON string the value the function will receive a JSON encoded value with the quotes as it appears in the JSON with line breaks encoded as \n, special characters like " or \ escaped with a backslash (\).Consequently, the return value of the provided function must be a valid JSON encoded literal (of any JSON type), otherwise the masking will result in an invalid JSON.
The table below contains a couple examples for the masking
Examples of using withRawValueFunction Input JSON Function Masked JSON { "maskMe": "a secret" }value -> value.replaceAll("secret", "***"){ "maskMe": "a ***" }{ "maskMe": 12345 }value -> value.startsWith(123) ? "0" : value{ "maskMe": 0 }{ "maskMe": "secret" }value -> "***"{ "maskMe": *** }(invalid JSON){ "maskMe": "secret" }value -> "\"***\""{ "maskMe": "***" }(valid JSON){ "maskMe": "secret value" }value -> value.substring(0, 3) + "***"{ "maskMe": "se*** }(invalid JSON{ "maskMe": "secret value" }value -> value.startsWith("\"") ? value.substring(0, 4) + "***\"" : value{ "maskMe": "sec***" }(valid JSON){ "maskMe": "Andrii \"Juice\" Pilshchykov" }value -> value.replaceAll("\"", "(quote)"){ "maskMe": "Andrii \(quote)Juice\(quote) Pilshchykov" }(invalid JSON){ "maskMe": "Andrii \"Juice\" Pilshchykov" }value -> value.replaceAll("\\\"", "(quote)"){ "maskMe": "Andrii (quote)Juice(quote) Pilshchykov" }(valid JSON)Note: usually the
ValueMaskeroperates on a byte level without parsing JSON values into intermediate objects. This implementation, however, needs to allocate aStringbefore passing it into the function and then turn it back into a byte array for the replacement, which introduces some performance overhead.
-