security.php
Go to the documentation of this file.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
00036
00037 class Security extends Object {
00038
00039
00040
00041
00042
00043
00044
00045 var $hashType = null;
00046
00047
00048
00049
00050
00051
00052
00053 function &getInstance() {
00054 static $instance = array();
00055 if (!$instance) {
00056 $instance[0] =& new Security;
00057 }
00058 return $instance[0];
00059 }
00060
00061
00062
00063
00064
00065
00066
00067 function inactiveMins() {
00068 $_this =& Security::getInstance();
00069 switch(Configure::read('Security.level')) {
00070 case 'high':
00071 return 10;
00072 break;
00073 case 'medium':
00074 return 100;
00075 break;
00076 case 'low':
00077 default:
00078 return 300;
00079 break;
00080 }
00081 }
00082
00083
00084
00085
00086
00087
00088
00089 function generateAuthKey() {
00090 $_this =& Security::getInstance();
00091 if(!class_exists('String')) {
00092 App::import('Core', 'String');
00093 }
00094 return $_this->hash(String::uuid());
00095 }
00096
00097
00098
00099
00100
00101
00102
00103
00104 function validateAuthKey($authKey) {
00105 $_this =& Security::getInstance();
00106 return true;
00107 }
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119 function hash($string, $type = null, $salt = false) {
00120 $_this =& Security::getInstance();
00121
00122 if ($salt) {
00123 $string = Configure::read('Security.salt') . $string;
00124 }
00125 if (empty($type)) {
00126 $type = $_this->hashType;
00127 }
00128 $type = strtolower($type);
00129
00130 if ($type == 'sha1' || $type == null) {
00131 if (function_exists('sha1')) {
00132 $return = sha1($string);
00133 return $return;
00134 } else {
00135 $type = 'sha256';
00136 }
00137 }
00138
00139 if ($type == 'sha256') {
00140 if (function_exists('mhash')) {
00141 $return = bin2hex(mhash(MHASH_SHA256, $string));
00142 return $return;
00143 } else {
00144 $type = 'md5';
00145 }
00146 }
00147
00148 if ($type == 'md5') {
00149 $return = md5($string);
00150 return $return;
00151 }
00152 }
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162 function setHash($hash) {
00163 $_this =& Security::getInstance();
00164 $_this->hashType = $hash;
00165 }
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175 function cipher($text, $key) {
00176 if (empty($key)) {
00177 trigger_error(__('You cannot use an empty key for Security::cipher()', true), E_USER_WARNING);
00178 return '';
00179 }
00180
00181 $_this =& Security::getInstance();
00182 if (!defined('CIPHER_SEED')) {
00183
00184 define('CIPHER_SEED', '76859309657453542496749683645');
00185 }
00186 srand (CIPHER_SEED);
00187 $out = '';
00188
00189 for ($i = 0; $i < strlen($text); $i++) {
00190 for ($j = 0; $j < ord(substr($key, $i % strlen($key), 1)); $j++) {
00191 $toss = rand(0, 255);
00192 }
00193 $mask = rand(0, 255);
00194 $out .= chr(ord(substr($text, $i, 1)) ^ $mask);
00195 }
00196 return $out;
00197 }
00198 }
00199 ?>