Download the PHP package robtimus/obfuscation without Composer
On this page you can find all versions of the php package robtimus/obfuscation. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download robtimus/obfuscation
More information about robtimus/obfuscation
Files in robtimus/obfuscation
Package obfuscation
Short Description Provides functionality for obfuscating text
License Apache-2.0
Homepage https://github.com/robtimus/php-obfuscation
Informations about the package obfuscation
Obfuscation
Provides functionality for obfuscating text. This can be useful for logging information that contains sensitive information.
Obfuscating strings
Pre-defined obfuscators
The following pre-defined obfuscators are provided that all return an immutable obfuscator.
Obfuscate::all
Replaces all characters with a mask character that defaults to *.
Note: using this obfuscator still leaks out information about the length of the original text. One of the following two is more secure.
Obfuscate::fixedLength
Replaces the entire text with a fixed number of the given mask character that defaults to *.
Obfuscate::fixedValue
Replaces the entire text with a fixed value.
Obfuscate::portion
While the above examples are simple, they are not very flexible. Using Obfuscate::portion you can build obfuscators that obfuscate only specific portions of text. Some examples:
Obfuscating all but the last 4 characters
Useful for obfuscating values like credit card numbers.
It’s advised to use atLeastFromStart, to make sure that values of fewer than 16 characters are still obfuscated properly:
Obfuscating only the last 2 characters
Useful for obfuscating values like zip codes, where the first part is not as sensitive as the full zip code:
Here, the keepAtStart instructs the obfuscator to keep everything; however, atLeastFromEnd overrides that partly to ensure that the last two characters are obfuscated regardless of the value specified by keepAtStart.
Using a fixed length
Similar to using Obfuscate::all, by default an obfuscator built using Obfuscate::portion leaks out the length of the original text. If your text has a variable length, you should consider specifying a fixed total length for the result. The length of the result will then be the same no matter how long the input is:
Note that if keepAtStart and keepAtEnd are both specified, parts of the input may be repeated in the result if the input’s length is less than the combined number of characters to keep. This makes it harder to find the original input. For example, if in the example foo would be obfuscated into fo***o instead, it would be clear that the input was foo. Instead, it can now be anything that starts with fo and ends with oo.
Obfuscate::none
Does not perform any obfuscation at all. It can be used as default to prevent checks. For instance:
Obfuscate::exploded
Explodes the text to an array, obfuscates each element, then implodes the array again.
Combining obfuscators
Sometimes the obfucators in this library alone cannot perform the obfuscation you need. For instance, if you want to obfuscate credit cards, but keep the first and last 4 characters. If the credit cards are all fixed length, Obfuscate::portion can do just that:
However, if you attempt to use such an obfuscator on only a part of a credit card, you could end up leaking parts of the credit card that you wanted to obfuscate:
To overcome this issue, it’s possible to combine obfuscators. The form is as follows:
- Specify the first obfuscator, and the input length to which it should be used.
- Specify any other obfuscators, and the input lengths to which they should be used. Note that each input length should be larger than the previous input length.
- Specify the obfuscator that will be used for the remainder.
For instance, for credit card numbers of exactly 16 characters, the above can also be written like this:
With this chaining, it’s now possible to keep the first and last 4 characters, but with at least 8 characters in between:
Splitting text during obfuscation
To make it easier to create obfuscators for structured text like email addresses, use a SplitPoint. Three implementations are provided :
atFirst(s)splits at the first occurrence of strings.atLast(s)splits at the last occurrence of strings.atNth(s, occurrence)splits at the zero-based specified occurrence of strings.
For instance:
To obfuscate the domain except for the TLD, use a nested SplitPoint:
Custom obfuscators
To create a custom obfuscator, create a sub class of Obfuscator, override its constructor and implements its obfuscateText method. For instance:
Obfuscating object properties
Use PropertyObfuscator::builder to start creating objects that can obfuscate single object properties as well as recursively all properties in an object or array.
The simplest form provides an obfucator for each property to obfuscate:
This matches property names case sensitively, and for any nested objects or arrays will obfuscate any scalar property with the object's or array's obfuscator. This behaviour can be changed in two ways:
-
Per property.
withPropertyreturns an object that can be used to further configure the property: - Using global options:
In both cases, forObjects and forArrays can take the following values:
PropertyObfuscationMode::SKIPto skip obfuscating object or array values and all nested properties.PropertyObfuscationMode::EXCLUDEto not match properties with object or array values; nested properties will be matched separately.PropertyObfuscationMode::INHERITto obfuscate each nested scalar property value or array element using the given obfuscator.PropertyObfuscationMode::INHERIT_OVERRIDABLEto obfuscate each nested scalar property value or array element using the given obfuscator; however, if a nested property has its own obfuscator defined this will be used instead.
Obfuscating HTTP headers
Use HeaderObfuscator::builder to start creating objects that can obfuscate single HTTP headers (as strings and string arrays) and associative arrays containing multiple headers. It's much like PropertyObfuscator, but like HTTP headers it's always case insensitive. Unlike PropertyObfuscator, it doesn't support nested objects, and for nested arrays each element is obfuscated separately. It also does not support skipping obfuscation.
All versions of obfuscation with dependencies
ext-mbstring Version *