Crypt_Blowfish
[ class tree: Crypt_Blowfish ] [ index: Crypt_Blowfish ] [ all elements ]

Source for file Blowfish.php

Documentation is available at Blowfish.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * Crypt_Blowfish allows for encryption and decryption on the fly using
  6.  * the Blowfish algorithm. Crypt_Blowfish does not require the MCrypt
  7.  * PHP extension, but uses it if available, otherwise it uses only PHP.
  8.  * Crypt_Blowfish supports encryption/decryption with or without a secret key.
  9.  *
  10.  * PHP versions 4 and 5
  11.  *
  12.  * @category   Encryption
  13.  * @package    Crypt_Blowfish
  14.  * @author     Matthew Fonda <mfonda@php.net>
  15.  * @copyright  2005-2008 Matthew Fonda
  16.  * @license    http://www.opensource.net/licenses/bsd-license.php New BSD
  17.  * @version    CVS: $Id: Blowfish.php,v 1.86 2008/08/30 21:53:50 jausions Exp $
  18.  * @link       http://pear.php.net/package/Crypt_Blowfish
  19.  */
  20.  
  21. /**
  22.  * Required PEAR package(s)
  23.  */
  24. require_once 'PEAR.php';
  25.  
  26. /**
  27.  * Engine choice constants
  28.  */
  29. /**
  30.  * To let the Crypt_Blowfish package decide which engine to use
  31.  * @since 1.1.0
  32.  */
  33. define('CRYPT_BLOWFISH_AUTO',   1);
  34. /**
  35.  * To use the MCrypt PHP extension.
  36.  * @since 1.1.0
  37.  */
  38. define('CRYPT_BLOWFISH_MCRYPT'2);
  39. /**
  40.  * To use the PHP-only engine.
  41.  * @since 1.1.0
  42.  */
  43. define('CRYPT_BLOWFISH_PHP',    3);
  44.  
  45.  
  46. /**
  47.  * Example using the factory method in CBC mode
  48.  * 
  49.  * <code>
  50.  * $bf =& Crypt_Blowfish::factory('cbc');
  51.  * if (PEAR::isError($bf)) {
  52.  *     echo $bf->getMessage();
  53.  *     exit;
  54.  * }
  55.  * $iv = 'abc123+=';
  56.  * $key = 'My secret key';
  57.  * $bf->setKey($key, $iv);
  58.  * $encrypted = $bf->encrypt('this is some example plain text');
  59.  * $bf->setKey($key, $iv);
  60.  * $plaintext = $bf->decrypt($encrypted);
  61.  * if (PEAR::isError($plaintext)) {
  62.  *     echo $plaintext->getMessage();
  63.  *     exit;
  64.  * }
  65.  * // Encrypted text is padded prior to encryption
  66.  * // so you may need to trim the decrypted result.
  67.  * echo 'plain text: ' . trim($plaintext);
  68.  * </code>
  69.  *
  70.  * To disable using the mcrypt library, define the CRYPT_BLOWFISH_NOMCRYPT
  71.  * constant. This is useful for instance on Windows platform with a buggy
  72.  * mdecrypt_generic() function.
  73.  * <code>
  74.  * define('CRYPT_BLOWFISH_NOMCRYPT', true);
  75.  * </code>
  76.  *
  77.  * @category   Encryption
  78.  * @package    Crypt_Blowfish
  79.  * @author     Matthew Fonda <mfonda@php.net>
  80.  * @author     Philippe Jausions <jausions@php.net>
  81.  * @copyright  2005-2008 Matthew Fonda
  82.  * @license    http://www.opensource.net/licenses/bsd-license.php New BSD
  83.  * @link       http://pear.php.net/package/Crypt_Blowfish
  84.  * @version    1.1.0RC2
  85.  * @access     public
  86.  */
  87. {
  88.     /**
  89.      * Implementation-specific Crypt_Blowfish object
  90.      *
  91.      * @var object 
  92.      * @access private
  93.      */
  94.     var $_crypt = null;
  95.  
  96.     /**
  97.      * Initialization vector
  98.      *
  99.      * @var string 
  100.      * @access protected
  101.      */
  102.     var $_iv = null;
  103.  
  104.     /**
  105.      * Holds block size
  106.      *
  107.      * @var integer 
  108.      * @access protected
  109.      */
  110.     var $_block_size = 8;
  111.  
  112.     /**
  113.      * Holds IV size
  114.      *
  115.      * @var integer 
  116.      * @access protected
  117.      */
  118.     var $_iv_size = 8;
  119.  
  120.     /**
  121.      * Holds max key size
  122.      *
  123.      * @var integer 
  124.      * @access protected
  125.      */
  126.     var $_key_size = 56;
  127.  
  128.     /**
  129.      * Crypt_Blowfish Constructor
  130.      * Initializes the Crypt_Blowfish object (in EBC mode), and sets
  131.      * the secret key
  132.      *
  133.      * @param string $key 
  134.      * @access public
  135.      * @deprecated Since 1.1.0
  136.      * @see Crypt_Blowfish::factory()
  137.      */
  138.     function Crypt_Blowfish($key)
  139.     {
  140.         $this->_crypt =Crypt_Blowfish::factory('ecb'$key);
  141.         if (!PEAR::isError($this->_crypt)) {
  142.             $this->_crypt->setKey($key);
  143.         }
  144.     }
  145.  
  146.     /**
  147.      * Crypt_Blowfish object factory
  148.      *
  149.      * This is the recommended method to create a Crypt_Blowfish instance.
  150.      *
  151.      * When using CRYPT_BLOWFISH_AUTO, you can force the package to ignore
  152.      * the MCrypt extension, by defining CRYPT_BLOWFISH_NOMCRYPT.
  153.      *
  154.      * @param string $mode operating mode 'ecb' or 'cbc' (case insensitive)
  155.      * @param string $key 
  156.      * @param string $iv initialization vector (must be provided for CBC mode)
  157.      * @param integer $engine one of CRYPT_BLOWFISH_AUTO, CRYPT_BLOWFISH_PHP
  158.      *                 or CRYPT_BLOWFISH_MCRYPT
  159.      * @return object Crypt_Blowfish object or PEAR_Error object on error
  160.      * @access public
  161.      * @static
  162.      * @since 1.1.0
  163.      */
  164.     function &factory($mode 'ecb'$key = null$iv = null$engine = CRYPT_BLOWFISH_AUTO)
  165.     {
  166.         switch ($engine{
  167.             case CRYPT_BLOWFISH_AUTO:
  168.                 if (!defined('CRYPT_BLOWFISH_NOMCRYPT')
  169.                     && extension_loaded('mcrypt')) {
  170.                     $engine CRYPT_BLOWFISH_MCRYPT;
  171.                 else {
  172.                     $engine CRYPT_BLOWFISH_PHP;
  173.                 }
  174.                 break;
  175.             case CRYPT_BLOWFISH_MCRYPT:
  176.                 if (!PEAR::loadExtension('mcrypt')) {
  177.                     return PEAR::raiseError('MCrypt extension is not available.');
  178.                 }
  179.                 break;
  180.         }
  181.  
  182.         switch ($engine{
  183.             case CRYPT_BLOWFISH_PHP:
  184.                 $mode strtoupper($mode);
  185.                 $class 'Crypt_Blowfish_' $mode;
  186.                 include_once 'Crypt/Blowfish/' $mode '.php';
  187.                 $crypt = new $class(null);
  188.                 break;
  189.  
  190.             case CRYPT_BLOWFISH_MCRYPT:
  191.                 include_once 'Crypt/Blowfish/MCrypt.php';
  192.                 $crypt = new Crypt_Blowfish_MCrypt(null$mode);
  193.                 break;
  194.         }
  195.  
  196.         if (!is_null($key|| !is_null($iv)) {
  197.             $result $crypt->setKey($key$iv);
  198.             if (PEAR::isError($result)) {
  199.                 return $result;
  200.             }
  201.         }
  202.  
  203.         return $crypt;
  204.     }
  205.  
  206.     /**
  207.      * Returns the algorithm's block size
  208.      *
  209.      * @return integer 
  210.      * @access public
  211.      * @since 1.1.0
  212.      */
  213.     function getBlockSize()
  214.     {
  215.         return $this->_block_size;
  216.     }
  217.  
  218.     /**
  219.      * Returns the algorithm's IV size
  220.      *
  221.      * @return integer 
  222.      * @access public
  223.      * @since 1.1.0
  224.      */
  225.     function getIVSize()
  226.     {
  227.         return $this->_iv_size;
  228.     }
  229.  
  230.     /**
  231.      * Returns the algorithm's maximum key size
  232.      *
  233.      * @return integer 
  234.      * @access public
  235.      * @since 1.1.0
  236.      */
  237.     function getMaxKeySize()
  238.     {
  239.         return $this->_key_size;
  240.     }
  241.  
  242.     /**
  243.      * Deprecated isReady method
  244.      *
  245.      * @return bool 
  246.      * @access public
  247.      * @deprecated
  248.      */
  249.     function isReady()
  250.     {
  251.         return true;
  252.     }
  253.  
  254.     /**
  255.      * Deprecated init method - init is now a private
  256.      * method and has been replaced with _init
  257.      *
  258.      * @return bool 
  259.      * @access public
  260.      * @deprecated
  261.      */
  262.     function init()
  263.     {
  264.         return $this->_crypt->init();
  265.     }
  266.  
  267.     /**
  268.      * Encrypts a string
  269.      *
  270.      * Value is padded with NUL characters prior to encryption. You may
  271.      * need to trim or cast the type when you decrypt.
  272.      *
  273.      * @param string $plainText the string of characters/bytes to encrypt
  274.      * @return string|PEAR_ErrorReturns cipher text on success, PEAR_Error on failure
  275.      * @access public
  276.      */
  277.     function encrypt($plainText)
  278.     {
  279.         return $this->_crypt->encrypt($plainText);
  280.     }
  281.  
  282.  
  283.     /**
  284.      * Decrypts an encrypted string
  285.      *
  286.      * The value was padded with NUL characters when encrypted. You may
  287.      * need to trim the result or cast its type.
  288.      *
  289.      * @param string $cipherText the binary string to decrypt
  290.      * @return string|PEAR_ErrorReturns plain text on success, PEAR_Error on failure
  291.      * @access public
  292.      */
  293.     function decrypt($cipherText)
  294.     {
  295.         return $this->_crypt->decrypt($cipherText);
  296.     }
  297.  
  298.     /**
  299.      * Sets the secret key
  300.      * The key must be non-zero, and less than or equal to
  301.      * 56 characters (bytes) in length.
  302.      *
  303.      * If you are making use of the PHP MCrypt extension, you must call this
  304.      * method before each encrypt() and decrypt() call.
  305.      *
  306.      * @param string $key 
  307.      * @return boolean|PEAR_Error Returns TRUE on success, PEAR_Error on failure
  308.      * @access public
  309.      */
  310.     function setKey($key)
  311.     {
  312.         return $this->_crypt->setKey($key);
  313.     }
  314. }
  315.  
  316. ?>

Documentation generated on Sat, 30 Aug 2008 18:00:05 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.