PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Utility/Security.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 157 lines | 74 code | 14 blank | 69 comment | 17 complexity | 11a91ce7d45111a91ecb51c32a662bb9 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-2011, 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-2011, 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. break;
  42. case 'medium':
  43. return 100;
  44. break;
  45. case 'low':
  46. default:
  47. return 300;
  48. break;
  49. }
  50. }
  51. /**
  52. * Generate authorization hash.
  53. *
  54. * @return string Hash
  55. */
  56. public static function generateAuthKey() {
  57. return Security::hash(String::uuid());
  58. }
  59. /**
  60. * Validate authorization hash.
  61. *
  62. * @param string $authKey Authorization hash
  63. * @return boolean Success
  64. * @todo Complete implementation
  65. */
  66. public static function validateAuthKey($authKey) {
  67. return true;
  68. }
  69. /**
  70. * Create a hash from string using given method.
  71. * Fallback on next available method.
  72. *
  73. * @param string $string String to hash
  74. * @param string $type Method to use (sha1/sha256/md5)
  75. * @param boolean $salt If true, automatically appends the application's salt
  76. * value to $string (Security.salt)
  77. * @return string Hash
  78. */
  79. public static function hash($string, $type = null, $salt = false) {
  80. if ($salt) {
  81. if (is_string($salt)) {
  82. $string = $salt . $string;
  83. } else {
  84. $string = Configure::read('Security.salt') . $string;
  85. }
  86. }
  87. if (empty($type)) {
  88. $type = self::$hashType;
  89. }
  90. $type = strtolower($type);
  91. if ($type == 'sha1' || $type == null) {
  92. if (function_exists('sha1')) {
  93. $return = sha1($string);
  94. return $return;
  95. }
  96. $type = 'sha256';
  97. }
  98. if ($type == 'sha256' && function_exists('mhash')) {
  99. return bin2hex(mhash(MHASH_SHA256, $string));
  100. }
  101. if (function_exists('hash')) {
  102. return hash($type, $string);
  103. }
  104. return md5($string);
  105. }
  106. /**
  107. * Sets the default hash method for the Security object. This affects all objects using
  108. * Security::hash().
  109. *
  110. * @param string $hash Method to use (sha1/sha256/md5)
  111. * @return void
  112. * @see Security::hash()
  113. */
  114. public static function setHash($hash) {
  115. self::$hashType = $hash;
  116. }
  117. /**
  118. * Encrypts/Decrypts a text using the given key.
  119. *
  120. * @param string $text Encrypted string to decrypt, normal string to encrypt
  121. * @param string $key Key to use
  122. * @return string Encrypted/Decrypted string
  123. */
  124. public static function cipher($text, $key) {
  125. if (empty($key)) {
  126. trigger_error(__d('cake_dev', 'You cannot use an empty key for Security::cipher()'), E_USER_WARNING);
  127. return '';
  128. }
  129. srand(Configure::read('Security.cipherSeed'));
  130. $out = '';
  131. $keyLength = strlen($key);
  132. for ($i = 0, $textLength = strlen($text); $i < $textLength; $i++) {
  133. $j = ord(substr($key, $i % $keyLength, 1));
  134. while ($j--) {
  135. rand(0, 255);
  136. }
  137. $mask = rand(0, 255);
  138. $out .= chr(ord(substr($text, $i, 1)) ^ $mask);
  139. }
  140. srand();
  141. return $out;
  142. }
  143. }