Source for file Encryption.class.php

Documentation is available at Encryption.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.  * Abstract Encryption Class
  22.  * 
  23.  * This class provides the foundation for creating encryption/decryption
  24.  * algorithms.  The Encryption system is based on two complimentary classes.
  25.  * The first will Encrypt, the second Decrypt.  Each class will define it's
  26.  * algorithm inside the 'parse' method.
  27.  * 
  28.  * The encoding option is responsible for formatting the data into a safe
  29.  * format, usually for saving inside a database.  Typically, with the mcrypt
  30.  * library, the encrypted characters are not safe to input into a database.
  31.  * The Encode option replaces the characters with database safe ones, but does
  32.  * not lose any information.
  33.  * 
  34.  * Raw data indicates data that is not encrypted.  An Encrypt class receives
  35.  * 'raw' data, a Decrypt class will generate 'raw' data.  The 'parse' method
  36.  * should define the state of the data after completion.
  37.  * 
  38.  * @category Gumbo
  39.  * @package Encryption
  40.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  41.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  42.  * @author Michael Luster <mluster79@yahoo.com>
  43.  * @link http://sourceforge.net/projects/phpgumbo
  44.  * @desc Abstract Encryption Class
  45.  * @version 0.0.1
  46.  */
  47.  
  48. abstract class Gumbo_Encryption {
  49.     
  50.     /** @var string $_data text to be encrypted/decrypted */
  51.     private $_data;
  52.     /** @var string $_cipher MCRYPT_* cipher */
  53.     private $_cipher = GUMBO_ENCRYPT_CIPHER;
  54.     /** @var string $_mode MCRYPT_MODE_* mode */
  55.     private $_mode = GUMBO_ENCRYPT_MODE;
  56.     /** @var string $_key encryption/decryption key */
  57.     private $_key = GUMBO_ENCRYPT_KEY;
  58.     /** @var bool $_encode if the data is to be encoded or decoded */
  59.     private $_encode = GUMBO_ENCRYPT_ENCODE;
  60.     
  61.     /** @var bool $_raw if the data is raw or encrypted/decrypted */
  62.     private $_raw = true;
  63.     
  64.     
  65.     
  66.     /** ABSTRACT METHODS **/
  67.     /**
  68.      * Parses the data into an encrypted/decrypted format
  69.      * @postcondition isRaw() => [true|false]
  70.      */
  71.     abstract public function parse ();
  72.     
  73.     
  74.     
  75.     /** MUTATOR METHODS **/
  76.     /**
  77.      * Sets if the data is Raw, or encrypted/decrypted
  78.      * 
  79.      * Raw data is the initial data sent into an Encryption class.  If using
  80.      * Encryption, this will be the data viewable to the user.  If using
  81.      * Decryption, this would be the encrypted string.
  82.      * @param bool $val 
  83.      * @throws Gumbo_Exception
  84.      */
  85.     protected function setRaw ($val{
  86.         try {
  87.             // verify precondition
  88.             if (!is_bool ($val)) {
  89.                 throw new Gumbo_Exception ("Invalid Argument 'val:bool' => {$val}:gettype ($val));
  90.             }
  91.             
  92.             $this->_raw = $val;
  93.         catch (Gumbo_Exception $e{
  94.             $e->setFunction (__METHOD__);
  95.             gumbo_trigger ($e);
  96.         }
  97.     }
  98.     
  99.     /**
  100.      * Sets the data to be encrypted/decrypted
  101.      * @postcondition setRaw(true)
  102.      * @param string $data 
  103.      * @throws Gumbo_Exception
  104.      */
  105.     public function setData ($data{
  106.         try {
  107.             // verify precondition
  108.             if (!is_string ($data)) {
  109.                 throw new Gumbo_Exception ("Invalid Argument 'data:str' => {$data}:gettype ($data));
  110.             }
  111.             
  112.             $this->_data = $data;
  113.             $this->setRaw (true);
  114.         catch (Gumbo_Exception $e{
  115.             $e->setFunction (__METHOD__);
  116.             gumbo_trigger ($e);
  117.         }
  118.     }
  119.     
  120.     /**
  121.      * Sets the key value
  122.      * @param string $key 
  123.      * @throws Gumbo_Exception
  124.      */
  125.     public function setKey ($key{
  126.         if (is_null ($key)) return}
  127.         try {
  128.             // verify precondition
  129.             if (!is_string ($key)) {
  130.                 throw new Gumbo_Exception ("Invalid Argument 'key:str' => {$key}:gettype ($key));
  131.             }
  132.             
  133.             $this->_key = $key;
  134.         catch (Gumbo_Exception $e{
  135.             $e->setFunction (__METHOD__);
  136.             gumbo_trigger ($e);
  137.         }
  138.     }
  139.     
  140.     /**
  141.      * Sets the cipher
  142.      * @precondition must be a MCRYPT_* constant
  143.      * @param string $cipher 
  144.      * @throws Gumbo_Exception
  145.      */
  146.     public function setCipher ($cipher{
  147.         if (is_null ($cipher)) return}
  148.         try {
  149.             // verify precondition
  150.             if (!is_string ($cipher)) {
  151.                 throw new Gumbo_Exception ("Invalid Argument 'cipher:str' => {$cipher}:gettype ($cipher));
  152.             }
  153.             
  154.             switch ($cipher{
  155.                 case MCRYPT_3DES break;
  156.                 case MCRYPT_BLOWFISH break;
  157.                 case MCRYPT_CAST_128 break;
  158.                 case MCRYPT_CAST_256 break;
  159.                 case MCRYPT_CRYPT break;
  160.                 case MCRYPT_DES break;
  161.                 case MCRYPT_GOST break;
  162.                 case MCRYPT_RC2 break;
  163.                 case MCRYPT_SAFER64 break;
  164.                 case MCRYPT_SAFER128 break;
  165.                 case MCRYPT_THREEWAY break;
  166.                 case MCRYPT_IDEA break;
  167.                 
  168.                 // 2.4.x above
  169.                 case MCRYPT_ARCFOUR_IV break;
  170.                 case MCRYPT_ARCFOUR break;
  171.                 // case MCRYPT_ENIGMA : break;
  172.                 case MCRYPT_LOKI97 break;
  173.                 case MCRYPT_MARS break;
  174.                 case MCRYPT_PANAMA break;
  175.                 case MCRYPT_RIJNDAEL_128 break;
  176.                 case MCRYPT_RIJNDAEL_192 break;
  177.                 case MCRYPT_RIJNDAEL_256 break;
  178.                 case MCRYPT_RC6 break;
  179.                 case MCRYPT_SAFERPLUS break;
  180.                 case MCRYPT_SERPENT break;
  181.                 case MCRYPT_SKIPJACK break;
  182.                 case MCRYPT_WAKE break;
  183.                 case MCRYPT_XTEA break;
  184.                 
  185.                 // 2.2.x only
  186.                 // case MCRYPT_DES_COMPAT : break;
  187.                 // case MCRYPT_RC4 : break;
  188.                 // case MCRYPT_RC6_128 : break;
  189.                 // case MCRYPT_RC6_192 : break;
  190.                 // case MCRYPT_RC6_256 : break;
  191.                 // case MCRYPT_SERPENT_128 : break;
  192.                 // case MCRYPT_SERPENT_192 : break;
  193.                 // case MCRYPT_SERPENT_256 : break;
  194.                 // case MCRYPT_TEAN : break;
  195.                 default : throw new Gumbo_Exception ("Encryption Cipher Does Not Exist: {$cipher}");
  196.             }
  197.             
  198.             $this->_cipher = $cipher;
  199.         catch (Gumbo_Exception $e{
  200.             $e->setFunction (__METHOD__);
  201.             gumbo_trigger ($e);
  202.         }
  203.     }
  204.     
  205.     /**
  206.      * Sets the mode of encryption
  207.      * @precondition must be a MCRYPT_MODE_* constant
  208.      * @param string $mode 
  209.      * @throws Gumbo_Exception
  210.      */
  211.     public function setMode ($mode{
  212.         if (is_null ($mode)) return}
  213.         try {
  214.             // verify precondition
  215.             if (!is_string ($mode)) {
  216.                 throw new Gumbo_Exception ("Invalid Argument 'mode:str' => {$mode}:gettype ($mode));
  217.             }
  218.             
  219.             switch ($mode{
  220.                 case MCRYPT_MODE_ECB break;
  221.                 case MCRYPT_MODE_CBC break;
  222.                 case MCRYPT_MODE_CFB break;
  223.                 case MCRYPT_MODE_OFB break;
  224.                 case MCRYPT_MODE_NOFB break;
  225.                 case MCRYPT_MODE_STREAM break;
  226.                 default : throw new Gumbo_Exception ("Encryption Mode Does Not Exist: {$mode}");
  227.             }
  228.             
  229.             $this->_mode = $mode;
  230.         catch (Gumbo_Exception $e{
  231.             $e->setFunction (__METHOD__);
  232.             gumbo_trigger ($e);
  233.         }
  234.     }
  235.     
  236.     /**
  237.      * Sets if the text should be encoded
  238.      * @param bool $val 
  239.      * @throws Gumbo_Exception
  240.      */
  241.     public function setEncode ($val{
  242.         if (is_null ($val)) return}
  243.         try {
  244.             // verify precondition
  245.             if (!is_bool ($val)) {
  246.                 throw new Gumbo_Exception ("Invalid Argument 'val:bool' => {$val}:gettype ($val));
  247.             }
  248.             
  249.             $this->_encode = $val;
  250.         catch (Gumbo_Exception $e{
  251.             $e->setFunction (__METHOD__);
  252.             gumbo_trigger ($e);
  253.         }
  254.     }
  255.     
  256.     
  257.     
  258.     /** ACCESSOR METHODS **/
  259.     /**
  260.      * Returns if the data is raw
  261.      * @return bool 
  262.      */
  263.     public function isRaw ({
  264.         return $this->_raw;
  265.     }
  266.     
  267.     /**
  268.      * Returns the encrypted/decrypted data
  269.      * @return string 
  270.      */
  271.     public function getData ({
  272.         return $this->_data;
  273.     }
  274.     
  275.     /**
  276.      * Returns the key
  277.      * @return string 
  278.      */
  279.     public function getKey ({
  280.         return $this->_key;
  281.     }
  282.     
  283.     /**
  284.      * Returns the cipher
  285.      * @return string 
  286.      */
  287.     public function getCipher ({
  288.         return $this->_cipher;
  289.     }
  290.     
  291.     /**
  292.      * Returns the encryption mode
  293.      * @return string 
  294.      */
  295.     public function getMode ({
  296.         return $this->_mode;
  297.     }
  298.     
  299.     /**
  300.      * Returns if the text should be encoded
  301.      * @return bool 
  302.      */
  303.     public function toEncode ({
  304.         return $this->_encode;
  305.     }
  306.     
  307. }
  308.  
  309. ?>