00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Security extends Object {
00036
00037
00038
00039
00040
00041
00042 var $hashType = null;
00043
00044
00045
00046
00047
00048
00049
00050 function &getInstance() {
00051 static $instance = array();
00052 if (!$instance) {
00053 $instance[0] =& new Security;
00054 }
00055 return $instance[0];
00056 }
00057
00058
00059
00060
00061
00062
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
00081
00082
00083
00084
00085
00086 function generateAuthKey() {
00087 if (!class_exists('String')) {
00088 App::import('Core', 'String');
00089 }
00090 return Security::hash(String::uuid());
00091 }
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 function validateAuthKey($authKey) {
00102 return true;
00103 }
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
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
00151
00152
00153
00154
00155
00156
00157
00158
00159 function setHash($hash) {
00160 $_this =& Security::getInstance();
00161 $_this->hashType = $hash;
00162 }
00163
00164
00165
00166
00167
00168
00169
00170
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
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 ?>