/cake/libs/security.php

https://github.com/tsep/tsep1 · PHP · 191 lines · 85 code · 15 blank · 91 comment · 19 complexity · 11a17d6834d312d3f777b4b317512c00 MD5 · raw file

  1. <?php
  2. /**
  3. * Core Security
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package cake
  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)
  19. */
  20. /**
  21. * Security Library contains utility methods related to security
  22. *
  23. * @package cake
  24. * @subpackage cake.cake.libs
  25. */
  26. class Security extends Object {
  27. /**
  28. * Default hash method
  29. *
  30. * @var string
  31. * @access public
  32. */
  33. var $hashType = null;
  34. /**
  35. * Singleton implementation to get object instance.
  36. *
  37. * @return object
  38. * @access public
  39. * @static
  40. */
  41. function &getInstance() {
  42. static $instance = array();
  43. if (!$instance) {
  44. $instance[0] =& new Security;
  45. }
  46. return $instance[0];
  47. }
  48. /**
  49. * Get allowed minutes of inactivity based on security level.
  50. *
  51. * @return integer Allowed inactivity in minutes
  52. * @access public
  53. * @static
  54. */
  55. function inactiveMins() {
  56. switch (Configure::read('Security.level')) {
  57. case 'high':
  58. return 10;
  59. break;
  60. case 'medium':
  61. return 100;
  62. break;
  63. case 'low':
  64. default:
  65. return 300;
  66. break;
  67. }
  68. }
  69. /**
  70. * Generate authorization hash.
  71. *
  72. * @return string Hash
  73. * @access public
  74. * @static
  75. */
  76. function generateAuthKey() {
  77. if (!class_exists('String')) {
  78. App::import('Core', 'String');
  79. }
  80. return Security::hash(String::uuid());
  81. }
  82. /**
  83. * Validate authorization hash.
  84. *
  85. * @param string $authKey Authorization hash
  86. * @return boolean Success
  87. * @access public
  88. * @static
  89. * @todo Complete implementation
  90. */
  91. function validateAuthKey($authKey) {
  92. return true;
  93. }
  94. /**
  95. * Create a hash from string using given method.
  96. * Fallback on next available method.
  97. *
  98. * @param string $string String to hash
  99. * @param string $type Method to use (sha1/sha256/md5)
  100. * @param boolean $salt If true, automatically appends the application's salt
  101. * value to $string (Security.salt)
  102. * @return string Hash
  103. * @access public
  104. * @static
  105. */
  106. function hash($string, $type = null, $salt = false) {
  107. $_this =& Security::getInstance();
  108. if ($salt) {
  109. if (is_string($salt)) {
  110. $string = $salt . $string;
  111. } else {
  112. $string = Configure::read('Security.salt') . $string;
  113. }
  114. }
  115. if (empty($type)) {
  116. $type = $_this->hashType;
  117. }
  118. $type = strtolower($type);
  119. if ($type == 'sha1' || $type == null) {
  120. if (function_exists('sha1')) {
  121. $return = sha1($string);
  122. return $return;
  123. }
  124. $type = 'sha256';
  125. }
  126. if ($type == 'sha256' && function_exists('mhash')) {
  127. return bin2hex(mhash(MHASH_SHA256, $string));
  128. }
  129. if (function_exists('hash')) {
  130. return hash($type, $string);
  131. }
  132. return md5($string);
  133. }
  134. /**
  135. * Sets the default hash method for the Security object. This affects all objects using
  136. * Security::hash().
  137. *
  138. * @param string $hash Method to use (sha1/sha256/md5)
  139. * @access public
  140. * @return void
  141. * @static
  142. * @see Security::hash()
  143. */
  144. function setHash($hash) {
  145. $_this =& Security::getInstance();
  146. $_this->hashType = $hash;
  147. }
  148. /**
  149. * Encrypts/Decrypts a text using the given key.
  150. *
  151. * @param string $text Encrypted string to decrypt, normal string to encrypt
  152. * @param string $key Key to use
  153. * @return string Encrypted/Decrypted string
  154. * @access public
  155. * @static
  156. */
  157. function cipher($text, $key) {
  158. if (empty($key)) {
  159. trigger_error(__('You cannot use an empty key for Security::cipher()', true), E_USER_WARNING);
  160. return '';
  161. }
  162. srand(Configure::read('Security.cipherSeed'));
  163. $out = '';
  164. $keyLength = strlen($key);
  165. for ($i = 0, $textLength = strlen($text); $i < $textLength; $i++) {
  166. $j = ord(substr($key, $i % $keyLength, 1));
  167. while ($j--) {
  168. rand(0, 255);
  169. }
  170. $mask = rand(0, 255);
  171. $out .= chr(ord(substr($text, $i, 1)) ^ $mask);
  172. }
  173. srand();
  174. return $out;
  175. }
  176. }