Source for file Encrypt.class.php

Documentation is available at Encrypt.class.php

  1. <?php
  2. /**
  3.  * Gumbo Library Framework
  4.  *
  5.  * LICENSE
  6.  * This library is being released under the terms of the New BSD License.  A
  7.  * copy of the license is packaged with the software (LICENSE.txt).  If no
  8.  * copy is found, a copy of the license template can be found at:
  9.  * http://www.opensource.org/licenses/bsd-license.php
  10.  * 
  11.  * @category Gumbo
  12.  * @package Encryption
  13.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  14.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  15.  * @author Michael Luster <mluster79@yahoo.com>
  16.  * @link http://sourceforge.net/projects/phpgumbo
  17.  * @version 0.0.1
  18.  */
  19.  
  20. /**
  21.  * Encrypt Class
  22.  *
  23.  * @category Gumbo
  24.  * @package Encryption
  25.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  26.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  27.  * @author Michael Luster <mluster79@yahoo.com>
  28.  * @link http://sourceforge.net/projects/phpgumbo
  29.  * @desc Encrypt Class
  30.  * @version 0.0.1
  31.  */
  32.  
  33. gumbo_load ("Encryption");
  34.  
  35. class Gumbo_Encrypt extends Gumbo_Encryption {
  36.     
  37.     /**
  38.      * Constructor
  39.      * @param string $data 
  40.      * @param string $key 
  41.      * @param int $cipher 
  42.      * @param int $mode 
  43.      * @param bool $encode 
  44.      */
  45.     public function __construct ($data=null$key=null$cipher=null$mode=null$encode=null{
  46.         if (!is_null ($data)) $this->setData ($data)}
  47.         $this->setKey ($key);
  48.         $this->setCipher ($cipher);
  49.         $this->setMode ($mode);
  50.         $this->setEncode ($encode);
  51.     }
  52.     
  53.     
  54.     
  55.     /** ACTION METHODS **/
  56.     /**
  57.      * Parses the data by encryption
  58.      * @return string 
  59.      * @throws Gumbo_Exception
  60.      */
  61.     public function parse ({
  62.         try {
  63.             // verify precondition
  64.             if (!$this->getData ()) {
  65.                 throw new Gumbo_Exception ("No Data to ENCRYPT");
  66.             }
  67.             
  68.             if ($this->isRaw ()) {
  69.                 // set IV
  70.                 $iv_size mcrypt_get_iv_size ($this->getCipher ()$this->getMode ());
  71.                 $iv null;
  72.                 for ($x 0;$x $iv_size;$x++$iv .= "a"}
  73.                 
  74.                 $this->setData (mcrypt_encrypt ($this->getCipher ()$this->getKey ()$this->getData ()$this->getMode ()$iv));
  75.                 
  76.                 if ($this->toEncode ()) {
  77.                     $data $this->getData ();
  78.                     
  79.                     $hexcode false;
  80.                     for ($x 0;$x strlen ($data);$x++{
  81.                         $tmp = (string) dechex (ord ($data {$x}));
  82.                         if (strlen ($tmp== 1$tmp "0" $tmp}
  83.                         $hexcode .= $tmp;
  84.                     }
  85.                     $this->setData ($hexcode);
  86.                 }
  87.                 $this->setRaw (false);
  88.             }
  89.         catch (Gumbo_Exception $e{
  90.             $e->setFunction (__METHOD__);
  91.             gumbo_trigger ($e);
  92.         }
  93.         return $this->getData ();
  94.     }
  95.     
  96.     
  97.     
  98.     /** ACCESSOR METHODS **/
  99.     /**
  100.      * Returns an md5 encrypted string
  101.      * @param string $data 
  102.      * @param bool $raw_output parameter in the md5 function
  103.      * @return string 
  104.      * @throws Gumbo_Exception
  105.      */
  106.     public function md5 ($data=null$raw_output=false{
  107.         try {
  108.             // verify precondition
  109.             try {
  110.                 if (!is_bool ($raw_output)) {
  111.                     throw new Gumbo_Exception ("Invalid Argument 'raw_output:bool' => {$raw_output}:gettype ($raw_output));
  112.                 }
  113.             catch (Gumbo_Exception $e{
  114.                 $e->setFunction (__METHOD__);
  115.                 gumbo_trigger ($e);
  116.                 $raw_output false;
  117.             }
  118.             
  119.             if (!is_null ($data&& !is_string ($data)) $data null}
  120.             if (is_null ($data)) $data $this->getData ()}
  121.             if (!$data{
  122.                 throw new Gumbo_Exception ("No Data to ENCRYPT");
  123.             }
  124.             
  125.             return md5 ($data$raw_output);
  126.         catch (Gumbo_Exception $e{
  127.             $e->setFunction (__METHOD__);
  128.             gumbo_trigger ($e);
  129.         }
  130.     }
  131.     
  132.     /**
  133.      * Returns an sha1 encrypted string
  134.      * @param string $data 
  135.      * @param bool $raw_output parameter in the md5 function
  136.      * @return string 
  137.      * @throws Gumbo_Exception
  138.      */
  139.     public function sha1 ($data=null$raw_output=false{
  140.         try {
  141.             // verify precondition
  142.             try {
  143.                 if (!is_bool ($raw_output)) {
  144.                     throw new Gumbo_Exception ("Invalid Argument 'raw_output:bool' => {$raw_output}:gettype ($raw_output));
  145.                 }
  146.             catch (Gumbo_Exception $e{
  147.                 $e->setFunction (__METHOD__);
  148.                 gumbo_trigger ($e);
  149.                 $raw_output false;
  150.             }
  151.             
  152.             if (!is_null ($data&& !is_string ($data)) $data null}
  153.             if (is_null ($data)) $data $this->getData ()}
  154.             if (!$data{
  155.                 throw new Gumbo_Exception ("No Data to ENCRYPT");
  156.             }
  157.             
  158.             return sha1 ($data$raw_output);
  159.         catch (Gumbo_Exception $e{
  160.             $e->setFunction (__METHOD__);
  161.             gumbo_trigger ($e);
  162.         }
  163.         return null;
  164.     }
  165.     
  166. }
  167.  
  168. ?>