Enhance dol_encode/dol_decode function

This commit is contained in:
Laurent Destailleur 2018-04-19 12:03:42 +02:00
parent 41be1c3d3d
commit 0f16ccbc4a
2 changed files with 54 additions and 18 deletions

View File

@ -27,44 +27,75 @@
/** /**
* Encode a string with base 64 algorithm + specific change * Encode a string with base 64 algorithm + specific delta change.
* Code of this function is useless and we should use base64_encode only instead
* *
* @param string $chain string to encode * @param string $chain string to encode
* @param string $key rule to use for delta ('0', '1' or 'myownkey')
* @return string encoded string * @return string encoded string
* @see dol_decode
*/ */
function dol_encode($chain) function dol_encode($chain, $key='1')
{ {
$strlength=dol_strlen($chain); if (is_numeric($key) && $key == '1') // rule 1 is offset of 17 for char
for ($i=0; $i < $strlength; $i++)
{ {
$output_tab[$i] = chr(ord(substr($chain,$i,1))+17); $strlength=dol_strlen($chain);
for ($i=0; $i < $strlength; $i++)
{
$output_tab[$i] = chr(ord(substr($chain,$i,1))+17);
}
$chain = implode("",$output_tab);
}
elseif ($key)
{
$result='';
$strlength=dol_strlen($chain);
for ($i=0; $i < $strlength; $i++)
{
$keychar = substr($key, ($i % strlen($key))-1, 1);
$result.= chr(ord(substr($chain,$i,1))+(ord($keychar)-65));
}
$chain=$result;
} }
$string_coded = base64_encode(implode("",$output_tab)); return base64_encode($chain);
return $string_coded;
} }
/** /**
* Decode a base 64 encoded + specific string. * Decode a base 64 encoded + specific delta change.
* This function is called by filefunc.inc.php at each page call. * This function is called by filefunc.inc.php at each page call.
* Code of this function is useless and we should use base64_decode only instead
* *
* @param string $chain string to decode * @param string $chain string to decode
* @param string $key rule to use for delta ('0', '1' or 'myownkey')
* @return string decoded string * @return string decoded string
* @see dol_encode
*/ */
function dol_decode($chain) function dol_decode($chain, $key='1')
{ {
$chain = base64_decode($chain); $chain = base64_decode($chain);
$strlength=dol_strlen($chain); if (is_numeric($key) && $key == '1') // rule 1 is offset of 17 for char
for($i=0; $i < $strlength;$i++)
{ {
$output_tab[$i] = chr(ord(substr($chain,$i,1))-17); $strlength=dol_strlen($chain);
for ($i=0; $i < $strlength;$i++)
{
$output_tab[$i] = chr(ord(substr($chain,$i,1))-17);
}
$chain = implode("",$output_tab);
}
elseif ($key)
{
$result='';
$strlength=dol_strlen($chain);
for ($i=0; $i < $strlength; $i++)
{
$keychar = substr($key, ($i % strlen($key))-1, 1);
$result.= chr(ord(substr($chain, $i, 1))-(ord($keychar)-65));
}
$chain=$result;
} }
$string_decoded = implode("",$output_tab); return $chain;
return $string_decoded;
} }

View File

@ -234,12 +234,17 @@ class SecurityTest extends PHPUnit_Framework_TestCase
*/ */
public function testEncodeDecode() public function testEncodeDecode()
{ {
$stringtotest="This is a string to test encode/decode"; $stringtotest="This is a string to test encode/decode. This is a string to test encode/decode. This is a string to test encode/decode.";
$encodedstring=dol_encode($stringtotest); $encodedstring=dol_encode($stringtotest);
$decodedstring=dol_decode($encodedstring); $decodedstring=dol_decode($encodedstring);
print __METHOD__." encodedstring=".$encodedstring." ".base64_encode($stringtotest)."\n"; print __METHOD__." encodedstring=".$encodedstring." ".base64_encode($stringtotest)."\n";
$this->assertEquals($stringtotest,$decodedstring); $this->assertEquals($stringtotest,$decodedstring, 'Use dol_encode/decode with no parameter');
$encodedstring=dol_encode($stringtotest, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
$decodedstring=dol_decode($encodedstring, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
print __METHOD__." encodedstring=".$encodedstring." ".base64_encode($stringtotest)."\n";
$this->assertEquals($stringtotest,$decodedstring, 'Use dol_encode/decode with a key parameter');
return 0; return 0;
} }