[ Index ]
 

Code source de Horde 3.1.3

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/Horde/ -> Secret.php (source)

   1  <?php
   2  /**
   3   * The Secret:: class provides an API for encrypting and decrypting
   4   * small pieces of data with the use of a shared key.
   5   *
   6   * The Secret:: functions use the Horde Cipher:: class if mcrypt is not
   7   * available.
   8   *
   9   * $Horde: framework/Secret/Secret.php,v 1.45.10.7 2006/03/02 05:25:10 slusarz Exp $
  10   *
  11   * Copyright 1999-2006 Chuck Hagenbuch <chuck@horde.org>
  12   *
  13   * See the enclosed file COPYING for license information (LGPL). If you
  14   * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  15   *
  16   * @author  Chuck Hagenbuch <chuck@horde.org>
  17   * @since   Horde 1.3
  18   * @package Horde_Secret
  19   */
  20  class Secret {
  21  
  22      /**
  23       * Take a small piece of data and encrypt it with a key.
  24       *
  25       * @param string $key      The key to use for encryption.
  26       * @param string $message  The plaintext message.
  27       *
  28       * @return string  The ciphertext message.
  29       */
  30      function write($key, $message)
  31      {
  32          if (Util::extensionExists('mcrypt')) {
  33              $td = @mcrypt_module_open(MCRYPT_GOST, '', MCRYPT_MODE_ECB, '');
  34              if ($td) {
  35                  $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  36                  @mcrypt_generic_init($td, $key, $iv);
  37                  $encrypted_data = @mcrypt_generic($td, $message);
  38                  @mcrypt_generic_deinit($td);
  39  
  40                  return $encrypted_data;
  41              }
  42          }
  43  
  44          static $cipherCache = array();
  45          $cacheIdx = md5($key);
  46  
  47          if (!isset($cipherCache[$cacheIdx])) {
  48              require_once 'Horde/Cipher.php';
  49  
  50              $cipherCache[$cacheIdx] = &Horde_Cipher::factory('blowfish');
  51              $cipherCache[$cacheIdx]->setBlockMode('ofb64');
  52              $cipherCache[$cacheIdx]->setKey($key);
  53          }
  54  
  55          return $cipherCache[$cacheIdx]->encrypt($message);
  56      }
  57  
  58      /**
  59       * Decrypt a message encrypted with Secret::write().
  60       *
  61       * @param string $key      The key to use for decryption.
  62       * @param string $message  The ciphertext message.
  63       *
  64       * @return string  The plaintext message.
  65       */
  66      function read($key, $ciphertext)
  67      {
  68          if (Util::extensionExists('mcrypt')) {
  69              $td = @mcrypt_module_open(MCRYPT_GOST, '', MCRYPT_MODE_ECB, '');
  70              if ($td) {
  71                  $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  72                  @mcrypt_generic_init($td, $key, $iv);
  73                  $decrypted_data = @mdecrypt_generic($td, $ciphertext);
  74                  @mcrypt_generic_deinit($td);
  75  
  76                  // Strip padding characters.
  77                  return rtrim($decrypted_data, "\0");
  78              }
  79          }
  80  
  81          static $cipherCache;
  82          $cacheIdx = md5($key);
  83  
  84          if (!is_array($cipherCache) || !isset($cipherCache[$cacheIdx])) {
  85              require_once 'Horde/Cipher.php';
  86  
  87              $cipherCache[$cacheIdx] = &Horde_Cipher::factory('blowfish');
  88              $cipherCache[$cacheIdx]->setBlockMode('ofb64');
  89              $cipherCache[$cacheIdx]->setKey($key);
  90          }
  91  
  92          return $cipherCache[$cacheIdx]->decrypt($ciphertext);
  93      }
  94  
  95      /**
  96       * Generate a secret key (for encryption), either using a random
  97       * md5 string and storing it in a cookie if the user has cookies
  98       * enabled, or munging some known values if they don't.
  99       *
 100       * @param string $keyname  The name of the key to set.
 101       *
 102       * @return string  The secret key that has been generated.
 103       */
 104      function setKey($keyname = 'generic')
 105      {
 106          global $conf;
 107  
 108          $timeout = $conf['session']['timeout'] ? time() + $conf['session']['timeout'] : 0;
 109  
 110          if (isset($_COOKIE[$conf['session']['name']])) {
 111              if (isset($_COOKIE[$keyname . '_key'])) {
 112                  $key = $_COOKIE[$keyname . '_key'];
 113              } else {
 114                  $key = md5(mt_rand());
 115                  $_COOKIE[$keyname . '_key'] = $key;
 116                  @setcookie($keyname . '_key', $key, $timeout, $conf['cookie']['path'],
 117                             $conf['cookie']['domain'], $conf['use_ssl'] == 1 ? 1 : 0);
 118              }
 119          } else {
 120              $key = session_id();
 121              @setcookie($keyname . '_key', $key, $timeout, $conf['cookie']['path'],
 122                         $conf['cookie']['domain'], $conf['use_ssl'] == 1 ? 1 : 0);
 123          }
 124  
 125          return $key;
 126      }
 127  
 128      /**
 129       * Return a secret key, either from a cookie, or if the cookie
 130       * isn't there, assume we are using a munged version of a known
 131       * base value.
 132       *
 133       * @param string $keyname  The name of the key to get.
 134       *
 135       * @return string  The secret key.
 136       */
 137      function getKey($keyname = 'generic')
 138      {
 139          static $keycache = array();
 140  
 141          if (!isset($keycache[$keyname])) {
 142              if (isset($_COOKIE[$keyname . '_key'])) {
 143                  $keycache[$keyname] = $_COOKIE[$keyname . '_key'];
 144              } else {
 145                  global $conf;
 146                  $keycache[$keyname] = session_id();
 147                  @setcookie($keyname . '_key', $keycache[$keyname],
 148                             $conf['session']['timeout'] ? time() + $conf['session']['timeout'] : 0,
 149                             $conf['cookie']['path'], $conf['cookie']['domain'], $conf['use_ssl'] == 1 ? 1 : 0);
 150              }
 151          }
 152  
 153          return $keycache[$keyname];
 154      }
 155  
 156      /**
 157       * Clears a secret key entry from the current cookie.
 158       *
 159       * @param string $keyname  The name of the key to clear.
 160       *
 161       * @return boolean  True if key existed, false if not.
 162       */
 163      function clearKey($keyname = 'generic')
 164      {
 165          if (isset($_COOKIE[$GLOBALS['conf']['session']['name']]) &&
 166              isset($_COOKIE[$keyname . '_key'])) {
 167              unset($_COOKIE[$keyname . '_key']);
 168              return true;
 169          }
 170          return false;
 171      }
 172  
 173  }


Généré le : Sun Feb 25 18:01:28 2007 par Balluche grâce à PHPXref 0.7