/web/concrete/helpers/encryption.php

http://github.com/concrete5/concrete5 · PHP · 70 lines · 27 code · 10 blank · 33 comment · 2 complexity · 3dc647f1cf5dba1e7617f695f44a4205 MD5 · raw file

  1. <?php
  2. /**
  3. * Encryption helper
  4. *
  5. * A wrapper class for mcrypt.
  6. *
  7. * Used as follows:
  8. * <code>
  9. * $enc = Loader::helper('encryption');
  10. * $string = 'This is some random text.';
  11. * $crypted = $enc->encrypt($string);
  12. * echo $enc->decrypt($crypted);
  13. * </code>
  14. *
  15. * @package Helpers
  16. * @category Concrete
  17. * @author Andrew Embler <andrew@concrete5.org>
  18. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  19. * @license http://www.concrete5.org/license/ MIT License
  20. */
  21. defined('C5_EXECUTE') or die("Access Denied.");
  22. class EncryptionHelper {
  23. /**
  24. * Takes encrypted text and decrypts it.
  25. * @param string $text
  26. * @return string $text
  27. */
  28. static public function decrypt($text)
  29. {
  30. if (function_exists('mcrypt_decrypt')) {
  31. $iv_size = mcrypt_get_iv_size(MCRYPT_XTEA, MCRYPT_MODE_ECB);
  32. $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
  33. $len = mcrypt_get_key_size(MCRYPT_XTEA, MCRYPT_MODE_ECB);
  34. $text = trim(mcrypt_decrypt(MCRYPT_XTEA, substr(PASSWORD_SALT, 0, $len), base64_decode($text), MCRYPT_MODE_ECB, $iv));
  35. }
  36. return $text;
  37. }
  38. /**
  39. * Takes un-encrypted text and encrypts it.
  40. * @param string $text
  41. * @return string $text
  42. */
  43. static public function encrypt($text)
  44. {
  45. if (function_exists('mcrypt_encrypt')) {
  46. $iv_size = mcrypt_get_iv_size(MCRYPT_XTEA, MCRYPT_MODE_ECB);
  47. $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
  48. $len = mcrypt_get_key_size(MCRYPT_XTEA, MCRYPT_MODE_ECB);
  49. $text = base64_encode(mcrypt_encrypt(MCRYPT_XTEA, substr(PASSWORD_SALT, 0, $len), $text, MCRYPT_MODE_ECB, $iv));
  50. }
  51. return $text;
  52. }
  53. /**
  54. * Function to see if mcrypt is installed
  55. * @return bool
  56. */
  57. public function isAvailable() {
  58. return function_exists('mcrypt_encrypt');
  59. }
  60. }