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

/tanora.org/www/framework/library/encryption.php

https://bitbucket.org/ekkl/tanora
PHP | 165 lines | 98 code | 20 blank | 47 comment | 17 complexity | 423b7d21c893c64759209f5839a08005 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * This class is intended to provide robust solutions for encrypting
  4. * and decrypting data.
  5. */
  6. class Encryption {
  7. /**
  8. * Encrypts data with the given data, key, cipher, and mode.
  9. */
  10. public static function encrypt($data, $key = NULL, $cipher = NULL, $mode = NULL) {
  11. if(is_null($key)) {
  12. $key = Framework::config('encryption', 'key');
  13. }
  14. if(is_null($cipher)) {
  15. $cipher = Framework::config('encryption', 'cipher');
  16. }
  17. if(is_null($mode)) {
  18. $mode = Framework::config('encryption', 'mode');
  19. }
  20. if(extension_loaded('mcrypt') !== FALSE) {
  21. $iv = self::_create_iv($cipher, $mode);
  22. $data = self::_pad($data, $cipher, $mode);
  23. return base64_encode(mcrypt_encrypt($cipher, $key, $data, $mode, $iv));
  24. } else {
  25. Framework::warn('Mcrypt extension is not loaded. Be warned: your data is not encrypted.');
  26. return $data;
  27. }
  28. }
  29. /**
  30. * Decrypts a string with the given string, key, cipher, and mode.
  31. */
  32. public static function decrypt($string, $key = NULL, $cipher = NULL, $mode = NULL) {
  33. if(is_null($key)) {
  34. $key = Framework::config('encryption', 'key');
  35. }
  36. if(is_null($cipher)) {
  37. $cipher = Framework::config('encryption', 'cipher');
  38. }
  39. if(is_null($mode)) {
  40. $mode = Framework::config('encryption', 'mode');
  41. }
  42. if(extension_loaded('mcrypt') !== FALSE) {
  43. $iv = self::_get_iv($cipher, $mode, $string);
  44. $data = mcrypt_decrypt($cipher, $key, base64_decode($string), $mode, $iv);
  45. return self::_unpad($data, $cipher, $mode);
  46. } else {
  47. Framework::warn('Mcrypt extension is not loaded. Be warned: your data is not encrypted.');
  48. return $string;
  49. }
  50. }
  51. /**
  52. * Returns a blowfish encrypted string.
  53. * @param string a string to encrpyt
  54. * @param cost base-2 logarithm of the iteration count
  55. * @param entropy 22 random alphanumeric characters
  56. */
  57. static public function blowfish($string, $cost = 10, $entropy = NULL) {
  58. $salt = '$2a$';
  59. // Security fix from 5.3.7 and up
  60. if(PHP_VERSION_ID >= 50307) {
  61. $salt = '$2y$';
  62. }
  63. // Base-2 logarithm of the iteration count.
  64. $salt .= $cost . '$';
  65. // 22 random digits "./0-9A-Za-z"
  66. if(is_null($entropy)) {
  67. $salt .= self::_keygen(22);
  68. } else {
  69. $salt .= $entropy;
  70. }
  71. return crypt($string, $salt);
  72. }
  73. /**
  74. * Compares a string encrypted with crypt() against an un-encrypted
  75. * string. Parameters can be in either order.
  76. * @param source an encrypted string
  77. * @param target an unencrypted string to compare
  78. */
  79. static public function crypt_compare($source, $target) {
  80. $attempt = crypt($target, $source) == $source;
  81. if($attempt === TRUE) {
  82. return TRUE;
  83. }
  84. $attempt = crypt($source, $target) == $target;
  85. if($attempt === TRUE) {
  86. return TRUE;
  87. }
  88. return FALSE;
  89. }
  90. /**
  91. * Creates additional padding.
  92. */
  93. private static function _pad($data, $cipher, $mode) {
  94. $block_size = self::_get_block_size($cipher, $mode);
  95. $pad_size = $block_size - (strlen($data) % $block_size);
  96. return $data . str_repeat(chr($pad_size), $pad_size);
  97. }
  98. /**
  99. * Removes additional padding.
  100. */
  101. private static function _unpad($data, $cipher, $mode) {
  102. $length = strlen($data);
  103. $pad_size = ord($data[$length - 1]);
  104. return substr($data, 0, -$pad_size);
  105. }
  106. /**
  107. * Gets the block size for the given cipher and mode.
  108. */
  109. private static function _get_block_size($cipher, $mode) {
  110. return mcrypt_get_block_size($cipher, $mode);
  111. }
  112. /**
  113. * Generates an initialization vector for the given cipher and
  114. * mode.
  115. */
  116. private static function _create_iv($cipher, $mode) {
  117. $iv_size = mcrypt_get_iv_size($cipher, $mode);
  118. return mcrypt_create_iv($iv_size, MCRYPT_RAND);
  119. }
  120. /**
  121. * Gets an initialization vector from an encrypted string.
  122. */
  123. private static function _get_iv($cipher, $mode, $string) {
  124. $iv_size = mcrypt_get_iv_size($cipher, $mode);
  125. return substr($string, 0, $iv_size);
  126. }
  127. /**
  128. * Returns a unique string of random alphanumeric characters.
  129. * @param length the length of the key
  130. */
  131. private static function _keygen($length = 32) {
  132. $seed = '';
  133. $hash = '';
  134. // Make the hash long enough.
  135. while(strlen($hash) < $length) {
  136. // Require the seed to be at least equal to the length in order to get a more unique hash.
  137. while(strlen($seed) < $length) {
  138. $seed .= uniqid((string) microtime().rand(), true);
  139. }
  140. $hash .= hash('sha256', $seed);
  141. }
  142. return substr($hash,0,$length);
  143. }
  144. }
  145. ?>