1 |
6daefa8c
|
Petr Lukašík
|
<?php
|
2 |
|
|
/* SVN FILE: $Id: security.php 4409 2007-02-02 13:20:59Z phpnut $ */
|
3 |
|
|
/**
|
4 |
|
|
* Security Class
|
5 |
|
|
*
|
6 |
|
|
* This class is a singleton class that contains
|
7 |
|
|
* functions for hasing and security.
|
8 |
|
|
*
|
9 |
|
|
* PHP versions 4 and 5
|
10 |
|
|
*
|
11 |
|
|
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
|
12 |
|
|
* Copyright 2005-2007, Cake Software Foundation, Inc.
|
13 |
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
14 |
|
|
* Las Vegas, Nevada 89104
|
15 |
|
|
*
|
16 |
|
|
* Licensed under The MIT License
|
17 |
|
|
* Redistributions of files must retain the above copyright notice.
|
18 |
|
|
*
|
19 |
|
|
* @filesource
|
20 |
|
|
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
|
21 |
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
22 |
|
|
* @package cake
|
23 |
|
|
* @subpackage cake.cake.libs
|
24 |
|
|
* @since CakePHP(tm) v .0.10.0.1233
|
25 |
|
|
* @version $Revision: 4409 $
|
26 |
|
|
* @modifiedby $LastChangedBy: phpnut $
|
27 |
|
|
* @lastmodified $Date: 2007-02-02 07:20:59 -0600 (Fri, 02 Feb 2007) $
|
28 |
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
29 |
|
|
*/
|
30 |
|
|
/**
|
31 |
|
|
* Security Class
|
32 |
|
|
*
|
33 |
|
|
* This class is a singleton class that contains functions for hasing and security.
|
34 |
|
|
*
|
35 |
|
|
* @package cake
|
36 |
|
|
* @subpackage cake.cake.libs
|
37 |
|
|
*/
|
38 |
|
|
class Security extends Object {
|
39 |
|
|
/**
|
40 |
|
|
* Singleton method to retrieve the instance of the Security class
|
41 |
|
|
*
|
42 |
|
|
* @return object Security
|
43 |
|
|
* @access public
|
44 |
|
|
*/
|
45 |
|
|
function &getInstance() {
|
46 |
|
|
static $instance = array();
|
47 |
|
|
if (!$instance) {
|
48 |
|
|
$instance[0] = &new Security;
|
49 |
|
|
}
|
50 |
|
|
return $instance[0];
|
51 |
|
|
}
|
52 |
|
|
/**
|
53 |
|
|
* Returns inactive minutes constant based on cake the security level
|
54 |
|
|
*
|
55 |
|
|
* @return integer
|
56 |
|
|
* @access public
|
57 |
|
|
*/
|
58 |
|
|
function inactiveMins() {
|
59 |
|
|
switch(CAKE_SECURITY) {
|
60 |
|
|
case 'high':
|
61 |
|
|
return 10;
|
62 |
|
|
break;
|
63 |
|
|
case 'medium':
|
64 |
|
|
return 100;
|
65 |
|
|
break;
|
66 |
|
|
case 'low':
|
67 |
|
|
default:
|
68 |
|
|
return 300;
|
69 |
|
|
break;
|
70 |
|
|
}
|
71 |
|
|
}
|
72 |
|
|
/**
|
73 |
|
|
* Generates a unique authkey
|
74 |
|
|
*
|
75 |
|
|
* @return mixed
|
76 |
|
|
* @access public
|
77 |
|
|
*/
|
78 |
|
|
function generateAuthKey() {
|
79 |
|
|
$_this =& Security::getInstance();
|
80 |
|
|
return $_this->hash(uniqid(rand(), true));
|
81 |
|
|
}
|
82 |
|
|
/**
|
83 |
|
|
* Validates the authkey
|
84 |
|
|
*
|
85 |
|
|
* @param mixed $authKey
|
86 |
|
|
* @return boolean
|
87 |
|
|
* @access public
|
88 |
|
|
*/
|
89 |
|
|
function validateAuthKey($authKey) {
|
90 |
|
|
return true;
|
91 |
|
|
}
|
92 |
|
|
/**
|
93 |
|
|
* Generates a hash of a string using a php built in hashing function
|
94 |
|
|
*
|
95 |
|
|
* @param string $string The string to be hashed
|
96 |
|
|
* @param string $type The hashing algorithm
|
97 |
|
|
* @return string
|
98 |
|
|
* @access public
|
99 |
|
|
*/
|
100 |
|
|
function hash($string, $type = 'sha1') {
|
101 |
|
|
$type = strtolower($type);
|
102 |
|
|
if ($type == 'sha1') {
|
103 |
|
|
if (function_exists('sha1')) {
|
104 |
|
|
$return = sha1($string);
|
105 |
|
|
return $return;
|
106 |
|
|
} else {
|
107 |
|
|
$type = 'sha256';
|
108 |
|
|
}
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
if ($type == 'sha256') {
|
112 |
|
|
if (function_exists('mhash')) {
|
113 |
|
|
$return = bin2hex(mhash(MHASH_SHA256, $string));
|
114 |
|
|
return $return;
|
115 |
|
|
} else {
|
116 |
|
|
$type = 'md5';
|
117 |
|
|
}
|
118 |
|
|
}
|
119 |
|
|
|
120 |
|
|
if ($type == 'md5') {
|
121 |
|
|
$return = md5($string);
|
122 |
|
|
return $return;
|
123 |
|
|
}
|
124 |
|
|
}
|
125 |
|
|
/**
|
126 |
|
|
* Function that ciphers a text using a key
|
127 |
|
|
*
|
128 |
|
|
* @param string $text
|
129 |
|
|
* @param string $key
|
130 |
|
|
* @return string
|
131 |
|
|
* @access public
|
132 |
|
|
*/
|
133 |
|
|
function cipher($text, $key) {
|
134 |
|
|
if (!defined('CIPHER_SEED')) {
|
135 |
|
|
//This is temporary will change later
|
136 |
|
|
define('CIPHER_SEED', '76859309657453542496749683645');
|
137 |
|
|
}
|
138 |
|
|
srand (CIPHER_SEED);
|
139 |
|
|
$out = '';
|
140 |
|
|
|
141 |
|
|
for($i = 0; $i < strlen($text); $i++) {
|
142 |
|
|
for($j = 0; $j < ord(substr($key, $i % strlen($key), 1)); $j++) {
|
143 |
|
|
$toss = rand(0, 255);
|
144 |
|
|
}
|
145 |
|
|
$mask = rand(0, 255);
|
146 |
|
|
$out .= chr(ord(substr($text, $i, 1)) ^ $mask);
|
147 |
|
|
}
|
148 |
|
|
return $out;
|
149 |
|
|
}
|
150 |
|
|
}
|
151 |
|
|
?>
|