1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
19:
20: App::uses('String', 'Utility');
21:
22: 23: 24: 25: 26:
27: class Security {
28:
29: 30: 31: 32: 33:
34: public static $hashType = null;
35:
36: 37: 38: 39: 40:
41: public static function inactiveMins() {
42: switch (Configure::read('Security.level')) {
43: case 'high':
44: return 10;
45: case 'medium':
46: return 100;
47: case 'low':
48: default:
49: return 300;
50: }
51: }
52:
53: 54: 55: 56: 57:
58: public static function generateAuthKey() {
59: return Security::hash(String::uuid());
60: }
61:
62: 63: 64: 65: 66: 67:
68: public static function validateAuthKey($authKey) {
69: return true;
70: }
71:
72: 73: 74: 75: 76: 77: 78: 79: 80: 81:
82: public static function hash($string, $type = null, $salt = false) {
83: if ($salt) {
84: if (is_string($salt)) {
85: $string = $salt . $string;
86: } else {
87: $string = Configure::read('Security.salt') . $string;
88: }
89: }
90:
91: if (empty($type)) {
92: $type = self::$hashType;
93: }
94: $type = strtolower($type);
95:
96: if ($type == 'sha1' || $type == null) {
97: if (function_exists('sha1')) {
98: $return = sha1($string);
99: return $return;
100: }
101: $type = 'sha256';
102: }
103:
104: if ($type == 'sha256' && function_exists('mhash')) {
105: return bin2hex(mhash(MHASH_SHA256, $string));
106: }
107:
108: if (function_exists('hash')) {
109: return hash($type, $string);
110: }
111: return md5($string);
112: }
113:
114: 115: 116: 117: 118: 119: 120: 121:
122: public static function setHash($hash) {
123: self::$hashType = $hash;
124: }
125:
126: 127: 128: 129: 130: 131: 132:
133: public static function cipher($text, $key) {
134: if (empty($key)) {
135: trigger_error(__d('cake_dev', 'You cannot use an empty key for Security::cipher()'), E_USER_WARNING);
136: return '';
137: }
138:
139: srand(Configure::read('Security.cipherSeed'));
140: $out = '';
141: $keyLength = strlen($key);
142: for ($i = 0, $textLength = strlen($text); $i < $textLength; $i++) {
143: $j = ord(substr($key, $i % $keyLength, 1));
144: while ($j--) {
145: rand(0, 255);
146: }
147: $mask = rand(0, 255);
148: $out .= chr(ord(substr($text, $i, 1)) ^ $mask);
149: }
150: srand();
151: return $out;
152: }
153:
154: 155: 156: 157: 158: 159: 160: 161:
162: public static function rijndael($text, $key, $operation) {
163: if (empty($key)) {
164: trigger_error(__d('cake_dev', 'You cannot use an empty key for Security::rijndael()'), E_USER_WARNING);
165: return '';
166: }
167: if (empty($operation) || !in_array($operation, array('encrypt', 'decrypt'))) {
168: trigger_error(__d('cake_dev', 'You must specify the operation for Security::rijndael(), either encrypt or decrypt'), E_USER_WARNING);
169: return '';
170: }
171: if (strlen($key) < 32) {
172: trigger_error(__d('cake_dev', 'You must use a key larger than 32 bytes for Security::rijndael()'), E_USER_WARNING);
173: return '';
174: }
175: $algorithm = 'rijndael-256';
176: $mode = 'cbc';
177: $cryptKey = substr($key, 0, 32);
178: $iv = substr($key, strlen($key) - 32, 32);
179: $out = '';
180: if ($operation === 'encrypt') {
181: $out .= mcrypt_encrypt($algorithm, $cryptKey, $text, $mode, $iv);
182: } elseif ($operation === 'decrypt') {
183: $out .= rtrim(mcrypt_decrypt($algorithm, $cryptKey, $text, $mode, $iv), "\0");
184: }
185: return $out;
186: }
187:
188: }
189: