once someone needed to know how to implement str_rot18 function with ASCII code.
My suggestion was:
"it is not impossible but it may be difficult; and disrespectful of the spirit of str_rot13 which encode and also decode
I suggest to use either custom internal map to do it or implement a str_rot128 if he however prefer to use the ASCII code".
I don't know what he finally opt for , but this give me the idea to create a class which may use dynamic map and thus dynamic rotation factor.So this package is born.
It actually allow to implement many string rotation functionalities including the basic str_rot13 and many variants of str_rot13 and of all other allowed rotations.Of course apply rotation twice on the same string bring back the string existing before rotations.
Finally as bonus four static functions exist to achieve useful transformations. Only the rot method disrespect the encoding-decoding function spirit.
little explanation of why the class is useful :
Sometimes we want to implement really simple encoding function to make some text unintelligible for simple human while making the decoding process fast and as easy as the encoding process.
Str_rot13 is a native PHP function which achieve this kindly.But it is also a really easy challenge to solve.
However solve this is really easy because the alphabet used is known by advance.Let's us now extend, with this class ,the
basic str_rot13 algorithm:
$dictionary=str_shuffle(implode('',range('a','z ')));//create a str_rot13 custom alphabet order
var_dump($newrotator->rotate("dictionary length must be even number and greater than 0"));// completely different from the native output
As you may see str_rot13 is actually absolutely not the most secure way to obfuscate text but this class
already make it a little more difficult for an human to break easily without knowing the exact dictionary used.
Using the same alphabet [a-z] as the native function you can thus produce 26!(factorial(26)) (about 403 291 461 126 605 635 584 000 000) numbers of alphabet and thus the same number of variants for the str_rot13.
This is a really basic explanation because the complexity of a solution can drastically been reduced
or augmented depending of many factors and specially if we use [a-z] or not.
You can also note that using this package a clever way could lead to really simple but relatively secure string "obfuscator"
implementation as only you, know your dictionary length,characters order and thus the rotation factor.
list of methods:
public function __construct(string $dictionary) throws new InvalidDictionaryException when invalid dictionary is supplied
array public function getMap() return the internal map
int public function getRotationFactor() return the current rotation factor
int public function getDictionaryLength() return the current dictionary length
void private function init_map(array $dictionary) initialize the internal map
string|false public static function invertTwoCharsPos(string $string) invert characters position with something like a bubble sort except that it doesn't sort anything and that one position only participate once.
string|false public static function invertCharsSlot(string $string,integer $slotLength=1) invert lots of characters position with something like a bubble sort except that it doesn't sort anything and that one slot only participate once
string|false public function rotate(string $string) rotate any given string string based on the current rotation factor and the given dictionary
string|false public static function c_rot(string $string,integer $rotation) apply a kind of circular rotation on the given string
string|false public static function autoRotate(string $string) create internal map based on the characters used in the string, choose a rotation factor based on this internal map and rotate the string according to it