7 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
10 * Licensed under The MIT License
11 * Redistributions of files must retain the above copyright notice.
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14 * @link http://cakephp.org CakePHP(tm) Project
16 * @subpackage cake.cake.libs
17 * @since CakePHP(tm) v .0.10.0.1233
18 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
22 * Security Library contains utility methods related to security
25 * @subpackage cake.cake.libs
27 class Security
extends Object {
38 * Singleton implementation to get object instance.
44 function &getInstance() {
45 static $instance = array();
47 $instance[0] =& new Security
;
53 * Get allowed minutes of inactivity based on security level.
55 * @return integer Allowed inactivity in minutes
59 function inactiveMins() {
60 switch (Configure
::read('Security.level')) {
75 * Generate authorization hash.
81 function generateAuthKey() {
82 if (!class_exists('String')) {
83 App
::import('Core', 'String');
85 return Security
::hash(String::uuid());
89 * Validate authorization hash.
91 * @param string $authKey Authorization hash
92 * @return boolean Success
95 * @todo Complete implementation
97 function validateAuthKey($authKey) {
102 * Create a hash from string using given method.
103 * Fallback on next available method.
105 * @param string $string String to hash
106 * @param string $type Method to use (sha1/sha256/md5)
107 * @param boolean $salt If true, automatically appends the application's salt
108 * value to $string (Security.salt)
109 * @return string Hash
113 function hash($string, $type = null, $salt = false) {
114 $_this =& Security
::getInstance();
117 if (is_string($salt)) {
118 $string = $salt . $string;
120 $string = Configure
::read('Security.salt') . $string;
125 $type = $_this->hashType
;
127 $type = strtolower($type);
129 if ($type == 'sha1' ||
$type == null) {
130 if (function_exists('sha1')) {
131 $return = sha1($string);
137 if ($type == 'sha256' && function_exists('mhash')) {
138 return bin2hex(mhash(MHASH_SHA256
, $string));
141 if (function_exists('hash')) {
142 return hash($type, $string);
148 * Sets the default hash method for the Security object. This affects all objects using
151 * @param string $hash Method to use (sha1/sha256/md5)
155 * @see Security::hash()
157 function setHash($hash) {
158 $_this =& Security
::getInstance();
159 $_this->hashType
= $hash;
163 * Encrypts/Decrypts a text using the given key.
165 * @param string $text Encrypted string to decrypt, normal string to encrypt
166 * @param string $key Key to use
167 * @return string Encrypted/Decrypted string
171 function cipher($text, $key) {
173 trigger_error(__('You cannot use an empty key for Security::cipher()', true), E_USER_WARNING
);
177 srand(Configure
::read('Security.cipherSeed'));
179 $keyLength = strlen($key);
180 for ($i = 0, $textLength = strlen($text); $i < $textLength; $i++
) {
181 $j = ord(substr($key, $i %
$keyLength, 1));
185 $mask = rand(0, 255);
186 $out .= chr(ord(substr($text, $i, 1)) ^
$mask);