Files
GestioneSpiaggia.it/library/App/Service/CodificaPiva.php
T
2021-09-17 12:41:45 +02:00

82 lines
2.3 KiB
PHP

<?php
class App_Service_CodificaPiva
{
public static function cripta($stringa,$maxPos = 11)
{
$pos = rand(1,$maxPos);
$strCript = str_pad(($pos * 17), 4, '0', STR_PAD_LEFT);
for($i = 0;$i < strlen($stringa); $i++){
$charpos = $i+1;
if($charpos < $pos){
$strCript .= str_pad((intval(substr($stringa,$i,1)) * $charpos * 5 + 17), 4, '0', STR_PAD_LEFT);
}else{
$strCript .= str_pad((intval(substr($stringa,$i,1)) * $charpos * 3 + 177), 4, '0', STR_PAD_LEFT);
}
}
return $strCript;
}
public static function decripta($stringa)
{
$packs = str_split($stringa, 4);
$pos = $packs[0] / 17;
unset($packs[0]);
$strDecript = "";
for($i = 1;$i <= count($packs); $i++){
if($i < $pos){
$strDecript .= ($packs[$i] - 17) / $i / 5;
}else{
$strDecript .= ($packs[$i] - 177) / $i / 3;
}
}
return $strDecript;
}
public static function criptaHex($stringa,$maxPos = 11)
{
$pos = rand(1,$maxPos);
$strCript = str_pad(($pos * 17), 4, '0', STR_PAD_LEFT);
for($i = 0;$i < strlen($stringa); $i++){
$charpos = $i+1;
if($charpos < $pos){
$strCript .= str_pad((ord(substr($stringa,$i,1)) + $charpos + 17), 3, '0', STR_PAD_LEFT);
}else{
$strCript .= str_pad((ord(substr($stringa,$i,1)) + $charpos + 177), 3, '0', STR_PAD_LEFT);
}
}
return base64_encode($strCript);
}
public static function decriptaHex($stringa)
{
$stringa = base64_decode($stringa);
$pos = substr($stringa,0,4) / 17;
$packs = str_split(substr($stringa,4), 3);
$strDecript = "";
for($i = 0;$i < count($packs); $i++){
$charpos = $i+1;
if($charpos < $pos){
//echo "<br>".intval($packs[$i]) . "-$charpos-17=$x";
$strDecript .= chr(intval($packs[$i]) - $charpos - 17);
}else{
//echo "<br>".intval($packs[$i]) . "-$charpos-177=$x";
$strDecript .= chr(intval($packs[$i]) - $charpos - 177);
}
}
return $strDecript;
}
}