Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
TinyToken is a class that generates unique and small tokens for use in your PHP proyects as UID for your database users, sessions, forms And returned token it is verifiable!
<?php
require "TinyToken.php";
// Generate new unique token
$token = TinyToken::get();
// Show it !
echo $token;
echo "<br>";
// Check if the given token is valid and if so, show token creation date-time
if($tokenDate = TinyToken::isValid($token ) ){
var_dump($tokenDate);
echo "<br>";
}
// Test for a not valid token
var_dump( TinyToken::isValid('5Q4NFQXXXXXXX') )
?>
<?
/* ************************************************************************
TinyToken is a class that generates a unique and small token 13 bytes
for use in your PHP proyects as UID for your database users, sessions,
forms ..
And it is verifiable !
//-Generate new token
TinyToken::get();
//- Check if a token is valid and return token creation datetime
TinyToken::isValid($token);
Twitter: @EnteroPositivo
https://gist.github.com/enteropositivo
************************************************************************ */
class TinyToken{
// Generate new token
public static function get(){
$mm=0;
$Y= date("y") ;
$M= date("m");
$D= date("d");
$H = date("H");
$min = date("i");
$ss = date("s");
list($usec, $secz) = explode(" ",microtime());
$micro=str_pad(substr($usec,3,4), 5,'0', STR_PAD_LEFT);
$micro+=($min+$ss);
$micro = str_pad($micro, 5,'0', STR_PAD_LEFT);
$addY = substr($micro,4,1);
$addY2 = substr($micro,3,1);
$ss+=$addY; //(9x4= 36 + 59seg < 99 )
$mm+=$addY2;
$ss = str_pad($ss, 2,'0', STR_PAD_LEFT);
$mm = str_pad($mm, 2,'0', STR_PAD_LEFT);
$s = substr($ss,1,1); //- last character or seconds
if($s>5)$s-=4; //- choose from flip array depending on seconds
$M+=$min;
$D+=$ss;
$M = str_pad($M, 2,'0', STR_PAD_LEFT);
$D = str_pad($D, 2,'0', STR_PAD_LEFT);
$Y+=$addY + ($addY2*5); //- max. Y=2055
$Y = str_pad($Y, 2,'0', STR_PAD_LEFT);
$PRI = $Y. $H .$min . $D. $M ;
$M_S = $ss.$min;
$PRI = $PRI+$micro;
$PRI = str_pad($PRI, 10,'0', STR_PAD_LEFT);
//- Encode swaping
$swaps = array(
array(1,3,8),
array(2,5,9),
array(1,4,6),
array(3,7,8),
array(0,2,9),
array(0,5,7)
);
$s_1=$swaps[$s][0];
$s_2=$swaps[$s][1];
$s_3=$swaps[$s][2];
$PRI = self::swap_digits($PRI, $s_1,$s_2,$s_3); // swap variable
$SEC = $M_S . $micro;
// fixed encoded to be able to decode
$SEC = self::swap_digits($SEC, 1,5,9);
// base 32 encode
$PRI32 = base_convert($PRI, 10,32);
$SEC32 = base_convert($SEC, 10,32);
$SEC32 = str_pad($SEC32, 6,'0', STR_PAD_LEFT);
$PRI32 = str_pad($PRI32, 7,'0', STR_PAD_LEFT);
$TOT = strtoupper($PRI32.$SEC32);
$TOT = self::swap_digits($TOT, 2,5,8);
$TOT = self::swap_digits($TOT, 'N','Z','D');
return $TOT;
}
// Check if a token is valid and return token creation datetime
public static function isValid($TOT){
if(strlen($TOT)!=13) return false;
$TOT = self::swap_digits($TOT, 'D','Z','N');
$TOT = self::swap_digits($TOT, 8,5,2);
$PRI32=substr($TOT, 0, 7);
$SEC32=substr($TOT, 7, 6);
$PRI = base_convert($PRI32, 32,10);
$SEC = base_convert($SEC32, 32,10);
$SEC = str_pad($SEC, 9,'0', STR_PAD_LEFT);
$PRI = str_pad($PRI, 10,'0', STR_PAD_LEFT);
$SEC = self::swap_digits($SEC, 9,5,1);
$ss=substr($SEC, 0,2);
$min=substr($SEC, 2,2);
$micro=substr($SEC, 4);
$addY = substr($micro,4,1);
$addY2 = substr($micro,3,1);
$s = substr($ss,1,1);
if($s>5)$s-=4;
$swaps = array(
array(1,3,8),
array(2,5,9),
array(1,4,6),
array(3,7,8),
array(0,2,9),
array(0,5,7)
);
$s_1=$swaps[$s][0];
$s_2=$swaps[$s][1];
$s_3=$swaps[$s][2];
$PRI = self::swap_digits($PRI, $s_3,$s_2,$s_1);
$PRI-=$micro;
$PRI = str_pad($PRI, 10,'0', STR_PAD_LEFT);
$Y=substr($PRI, 0,2);
$H=substr($PRI, 2,2);
$min1=substr($PRI, 4,2);
$D=substr($PRI, 6,2);
$M=substr($PRI, 8,2);
$micro-=($ss+$min);
$micro = str_pad($micro, 5,'0', STR_PAD_LEFT);
$Y=$Y-$addY - ($addY2*5);
$Y = str_pad($Y, 2,'0', STR_PAD_LEFT);
$M-=$min;
$D-=$ss;
$ss-=$addY;
$M = str_pad($M, 2,'0', STR_PAD_LEFT);
$D = str_pad($D, 2,'0', STR_PAD_LEFT);
//- validacion
if($min!=$min1) return false;
if($M>12 || $M<1) return false;
if($D>31) return false;
if($H>24) return false;
if($min>60) return false;
if($ss>60) return false;
return array(
'Y'=>$Y,
'm'=>$M,
'd'=>$D,
'h'=>$H,
'i'=>$min,
's'=>$ss,
'u'=>$micro
);
}
static function swap_digits($parent, $a, $b, $c){
$pre= str_replace( array($a, $b,$c), array('#', '@','$'), $parent ) ;
return str_replace( array('#', '@','$'), array($b, $c,$a) , $pre ) ;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment