PHP Classes

PHP String Rotate: Rotate strings in different ways

Recommend this page to a friend!
  Info   View files Example   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 159 This week: 1All time: 8,976 This week: 560Up
Version License PHP version Categories
string-rotator 1.0.1Custom (specified...5Algorithms, PHP 5, Text processing
Description 

Author

This class can rotate strings in different ways.

It can take a given string and rotate the position of the characters that it has in using a method that if you rotate the string again, it will return the original string.

The class implement several methods of rotation like:

- Invert the position of two characters
- Invert the position of several characters
- Rotate a string based on the current rotation factor and dictionary
- Circular rotation
- Automatically rotate using a internal map and rotation factor

Innovation Award
PHP Programming Innovation award nominee
November 2018
Number 5
String rotation is a simple way to obfuscate strings and way that it is possible to retrieve the original string from the obfuscated version.

This class provides a solution for rotating strings that implements several methods of string rotation.

Manuel Lemos
Picture of zinsou A.A.E.Moïse
  Performance   Level  
Name: zinsou A.A.E.Moïse is available for providing paid consulting. Contact zinsou A.A.E.Moïse .
Classes: 50 packages by
Country: Benin Benin
Age: 34
All time rank: 6781 in Benin Benin
Week rank: 109 Up1 in Benin Benin Equal
Innovation award
Innovation award
Nominee: 23x

Winner: 2x

Example

<?php

require('str_rotator.class.php');
 
 echo
'<pre>';
$dictionary=implode('',range('a','z '));//str_rot13 basic

$newrotator=new str_rotator($dictionary);

var_dump($newrotator->rotate('dictionary contains duplicated characters')===str_rot13('dictionary contains duplicated characters'));
           
           
$dictionary=str_shuffle(implode('',range('a','z ')));//str_rot13 custom variant

$newrotator=new str_rotator($dictionary);

var_dump($newrotator->rotate('dictionary contains duplicated characters')===str_rot13('dictionary contains duplicated characters'));
 
 
$dictionary=implode('',array_merge(range('a','z'),range(0,9)));//str_rot18 basic
$newrotator=new str_rotator($dictionary);

var_dump($newrotator->rotate($newrotator->rotate('dictionary contains duplicated characters')));



$dictionary=str_shuffle(implode('',array_merge(range('a','z'),range(0,9))));//str_rot18 custom variant

$newrotator=new str_rotator($dictionary);

var_dump(
$newrotator->rotate('dictionary contains duplicated characters'),$newrotator->getRotationFactor(),
$newrotator::InvertTwoCharsPos($newrotator::c_rot('dictionary contains duplicated characters',rand(0,25))),
$newrotator::InvertTwoCharsPos($newrotator::InvertTwoCharsPos('dictionary contains duplicated characters')),
$newrotator::autoRotate('dictionary contains duplicated characters'),
$newrotator::c_Rot('dictionary contains duplicated characters',25)
);

 
$content="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed, dolor. Cras elementum ultrices diam. Maecenas ligula massa, varius a, semper congue, euismod non, mi. Proin porttitor, orci nec nonummy molestie, enim est eleifend mi, non fermentum diam nisl sit amet erat. Duis semper. Duis arcu massa, scelerisque vitae, consequat in, pretium a, enim. Pellentesque congue. Ut in risus volutpat libero pharetra tempor. Cras vestibulum bibendum augue. Praesent egestas leo in pede. Praesent blandit odio eu enim. Pellentesque sed dui ut augue blandit sodales. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aliquam nibh. Mauris ac mauris sed pede pellentesque fermentum. Maecenas adipiscing ante non diam sodales hendrerit.";
 
var_dump($encode=$newrotator::autorotate($content),$newrotator::invertCharsSlot($content,7),$newrotator::autorotate($encode));




Details

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 $newrotator=new str_rotator($dictionary); 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

  Files folder image Files  
File Role Description
Accessible without login Plain text file license.txt Lic. license file
Accessible without login Plain text file readme.txt Doc. readme
Accessible without login Plain text file RotatorTest.php Example example script
Plain text file str_rotator.class.php Class class source

 Version Control Unique User Downloads Download Rankings  
 0%
Total:159
This week:1
All time:8,976
This week:560Up