/testXML/lib/Cake/Utility/Security.php

https://bitbucket.org/allanxyh/project · PHP · 188 lines · 96 code · 16 blank · 76 comment · 22 complexity · ce049637a489c7352d3dac7ddc94b32b MD5 · raw file

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