security.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: security.php 7847 2008-11-08 02:54:07Z renan.saddam $ */
00003 /**
00004  * Short description for file.
00005  *
00006  * Long description for file
00007  *
00008  * PHP versions 4 and 5
00009  *
00010  * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
00011  * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
00012  *
00013  * Licensed under The MIT License
00014  * Redistributions of files must retain the above copyright notice.
00015  *
00016  * @filesource
00017  * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
00018  * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00019  * @package       cake
00020  * @subpackage    cake.cake.libs
00021  * @since         CakePHP(tm) v .0.10.0.1233
00022  * @version       $Revision: 7847 $
00023  * @modifiedby    $LastChangedBy: renan.saddam $
00024  * @lastmodified  $Date: 2008-11-07 21:54:07 -0500 (Fri, 07 Nov 2008) $
00025  * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
00026  */
00027 /**
00028  * Short description for file.
00029  *
00030  * Long description for file
00031  *
00032  * @package       cake
00033  * @subpackage    cake.cake.libs
00034  */
00035 class Security extends Object {
00036 /**
00037  * Default hash method
00038  *
00039  * @var string
00040  * @access public
00041  */
00042     var $hashType = null;
00043 /**
00044   * Singleton implementation to get object instance.
00045   *
00046   * @return object
00047   * @access public
00048   * @static
00049   */
00050     function &getInstance() {
00051         static $instance = array();
00052         if (!$instance) {
00053             $instance[0] =& new Security;
00054         }
00055         return $instance[0];
00056     }
00057 /**
00058   * Get allowed minutes of inactivity based on security level.
00059   *
00060   * @return integer Allowed inactivity in minutes
00061   * @access public
00062   * @static
00063   */
00064     function inactiveMins() {
00065         $_this =& Security::getInstance();
00066         switch (Configure::read('Security.level')) {
00067             case 'high':
00068                 return 10;
00069             break;
00070             case 'medium':
00071                 return 100;
00072             break;
00073             case 'low':
00074             default:
00075                 return 300;
00076                 break;
00077         }
00078     }
00079 /**
00080   * Generate authorization hash.
00081   *
00082   * @return string Hash
00083   * @access public
00084   * @static
00085   */
00086     function generateAuthKey() {
00087         if (!class_exists('String')) {
00088             App::import('Core', 'String');
00089         }
00090         return Security::hash(String::uuid());
00091     }
00092 /**
00093  * Validate authorization hash.
00094  *
00095  * @param string $authKey Authorization hash
00096  * @return boolean Success
00097  * @access public
00098  * @static
00099  * @todo Complete implementation
00100  */
00101     function validateAuthKey($authKey) {
00102         return true;
00103     }
00104 /**
00105  * Create a hash from string using given method.
00106  * Fallback on next available method.
00107  *
00108  * @param string $string String to hash
00109  * @param string $type Method to use (sha1/sha256/md5)
00110  * @param boolean $salt If true, automatically appends the application's salt
00111  *                value to $string (Security.salt)
00112  * @return string Hash
00113  * @access public
00114  * @static
00115  */
00116     function hash($string, $type = null, $salt = false) {
00117         $_this =& Security::getInstance();
00118 
00119         if ($salt) {
00120             if (is_string($salt)) {
00121                 $string = $salt . $string;
00122             } else {
00123                 $string = Configure::read('Security.salt') . $string;
00124             }
00125         }
00126 
00127         if (empty($type)) {
00128             $type = $_this->hashType;
00129         }
00130         $type = strtolower($type);
00131 
00132         if ($type == 'sha1' || $type == null) {
00133             if (function_exists('sha1')) {
00134                 $return = sha1($string);
00135                 return $return;
00136             }
00137             $type = 'sha256';
00138         }
00139 
00140         if ($type == 'sha256' && function_exists('mhash')) {
00141             return bin2hex(mhash(MHASH_SHA256, $string));
00142         }
00143 
00144         if (function_exists('hash')) {
00145             return hash($type, $string);
00146         }
00147         return md5($string);
00148     }
00149 /**
00150  * Sets the default hash method for the Security object.  This affects all objects using
00151  * Security::hash().
00152  *
00153  * @param string $hash Method to use (sha1/sha256/md5)
00154  * @access public
00155  * @return void
00156  * @static
00157  * @see Security::hash()
00158  */
00159     function setHash($hash) {
00160         $_this =& Security::getInstance();
00161         $_this->hashType = $hash;
00162     }
00163 /**
00164  * Encrypts/Decrypts a text using the given key.
00165  *
00166  * @param string $text Encrypted string to decrypt, normal string to encrypt
00167  * @param string $key Key to use
00168  * @return string Encrypted/Decrypted string
00169  * @access public
00170  * @static
00171  */
00172     function cipher($text, $key) {
00173         if (empty($key)) {
00174             trigger_error(__('You cannot use an empty key for Security::cipher()', true), E_USER_WARNING);
00175             return '';
00176         }
00177 
00178         $_this =& Security::getInstance();
00179         if (!defined('CIPHER_SEED')) {
00180             //This is temporary will change later
00181             define('CIPHER_SEED', '76859309657453542496749683645');
00182         }
00183         srand(CIPHER_SEED);
00184         $out = '';
00185 
00186         for ($i = 0; $i < strlen($text); $i++) {
00187             for ($j = 0; $j < ord(substr($key, $i % strlen($key), 1)); $j++) {
00188                 $toss = rand(0, 255);
00189             }
00190             $mask = rand(0, 255);
00191             $out .= chr(ord(substr($text, $i, 1)) ^ $mask);
00192         }
00193         return $out;
00194     }
00195 }
00196 ?>

Generated on Sun Nov 22 00:30:53 2009 for CakePHP 1.2.x.x (v1.2.4.8284) by doxygen 1.4.7