phpDocumentor general
[ class tree: general ] [ index: general ] [ all elements ]

Source for file c_crypter.inc

Documentation is available at c_crypter.inc

  1. <?php
  2. /**
  3.  * Classe di encrypt / decrypt
  4.  *  
  5.  * @package general
  6.  * @author Ubik <emiliano.leporati@gmail.com>
  7.  */
  8.  
  9. /**
  10.  * Crittazione e decrittazione di valori
  11.  *  
  12.  * @package general
  13.  */
  14. class CRYPTER 
  15. {
  16.     /**
  17.      * Tabella di crittazione
  18.      * @var resource 
  19.      */
  20.     private $td;
  21.     /**
  22.      * Chiave di crittazione
  23.      * @var string 
  24.      */
  25.     private $key;
  26.     /**
  27.      * Vettore di inizializzazione
  28.      * @var string 
  29.      */
  30.     private $iv;
  31.  
  32.     /**
  33.      * Inizializza le variabili private, solleva eccezione se manca il modulo Mcrypt
  34.      */
  35.     public function __construct($key)
  36.     {
  37.         if (function_exists("mcrypt_module_open")) {
  38.             srand();
  39.             $this->td = mcrypt_module_open(MCRYPT_RIJNDAEL_128'''ecb''');
  40.             $this->key = substr($key0mcrypt_enc_get_key_size($this->td));
  41.             $iv_size mcrypt_enc_get_iv_size($this->td);
  42.             $this->iv = mcrypt_create_iv($iv_sizeMCRYPT_RAND);
  43.         else {
  44.             throw new FatalException("Modulo Mcrypt non installato, impossibile utilizzare campi crittati.");
  45.         }
  46.     }
  47.  
  48.  
  49.     /**
  50.      * Chiude il modulo di crittazione
  51.      */
  52.     public function __destruct()
  53.     {
  54.         mcrypt_module_close($this->td);
  55.     }
  56.  
  57.     /**
  58.      * Esegue una codifica reversibile della stringa
  59.      * @param string $string La stringa da crittare
  60.      * @return string La stringa crittata
  61.      */
  62.     public function encrypt($string)
  63.     {
  64.         if (!$stringreturn $string;
  65.  
  66.         mcrypt_generic_init($this->td$this->key$this->iv);
  67.         $result bin2hex(mcrypt_generic($this->td$string));
  68.         mcrypt_generic_deinit($this->td);
  69.  
  70.         return $result;
  71.     }
  72.  
  73.     /**
  74.      * Decodifica una stringa crittata con {@link encrypt}
  75.      * @param string $string La stringa crittata
  76.      * @return string La stringa de-crittata
  77.      */
  78.     public function decrypt($string)
  79.     {
  80.         if (!$stringreturn $string;
  81.  
  82.         mcrypt_generic_init($this->td$this->key$this->iv);
  83.         $result rtrim(mdecrypt_generic($this->tdhex2bin($string)));
  84.         mcrypt_generic_deinit($this->td);
  85.  
  86.         return $result;
  87.     }
  88.  
  89.     /**
  90.      * Esegue una codifica irreversibile di una stringa; solleva eccezione se manca il modulo Mcrypt
  91.      * @static
  92.      * @param string $string La stringa da crittare
  93.      * @return string La stringa crittata
  94.      */
  95.     public static function hash($string)
  96.     {
  97.         if (function_exists("mhash")) {
  98.             return bin2hex(mhash(MHASH_SHA1$string));
  99.         else {
  100.             throw new FatalException("Modulo Mhash non installato, impossibile utilizzare campi crittati.");
  101.         }
  102.     }
  103.  
  104. }
  105.  
  106.  
  107. ?>

Documentation generated on Thu, 25 Sep 2008 23:29:05 +0200 by phpDocumentor 1.4.0