Crypt.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ FhMfxEkPTBUaiLQhOUBYX28DEOz+KrywLyvuOlkmHj0=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2014 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.0
  11. */
  12. /**
  13. * crypt class
  14. */
  15. namespace Cube;
  16. class Crypt
  17. {
  18. /**
  19. *
  20. * encryption key
  21. *
  22. * @var string
  23. */
  24. protected $_key;
  25. /**
  26. * One of the MCRYPT_cipher name constants, or the name of the algorithm as string.
  27. *
  28. * @var string
  29. */
  30. protected $_cipher = MCRYPT_RIJNDAEL_256;
  31. /**
  32. * One of the MCRYPT_MODE_modename constants,
  33. * or one of the following strings: "ecb", "cbc", "cfb", "ofb", "nofb" or "stream".
  34. *
  35. * @var string
  36. */
  37. protected $_mode = MCRYPT_MODE_ECB;
  38. /**
  39. *
  40. * set encryption key
  41. *
  42. * @param string $key
  43. * @return $this
  44. */
  45. public function setKey($key)
  46. {
  47. $this->_key = hash('sha256', $key, true);
  48. return $this;
  49. }
  50. /**
  51. *
  52. * get encryption key
  53. *
  54. * @throws \RuntimeException
  55. * @return string
  56. */
  57. public function getKey()
  58. {
  59. if (empty($this->_key)) {
  60. throw new \RuntimeException("The encryption key has not been set.");
  61. }
  62. return $this->_key;
  63. }
  64. /**
  65. *
  66. * set mcrypt cipher
  67. *
  68. * @param string $cipher
  69. * @return $this
  70. */
  71. public function setCipher($cipher)
  72. {
  73. $this->_cipher = $cipher;
  74. return $this;
  75. }
  76. /**
  77. *
  78. * get mcrypt cipher
  79. *
  80. * @return string
  81. */
  82. public function getCipher()
  83. {
  84. return $this->_cipher;
  85. }
  86. /**
  87. *
  88. * set mcrypt mode
  89. *
  90. * @param string $mode
  91. * @return $this
  92. */
  93. public function setMode($mode)
  94. {
  95. $this->_mode = $mode;
  96. }
  97. /**
  98. *
  99. * get mcrypt mode
  100. *
  101. * @return string
  102. */
  103. public function getMode()
  104. {
  105. return $this->_mode;
  106. }
  107. /**
  108. *
  109. * encrypt a string
  110. *
  111. * @param $input
  112. * @return string
  113. */
  114. function encrypt($input)
  115. {
  116. return base64_encode(mcrypt_encrypt($this->getCipher(), $this->getKey(), $input, $this->getMode()));
  117. }
  118. /**
  119. *
  120. * decrypt an encrypted string
  121. *
  122. * @param $input
  123. * @return string
  124. */
  125. function decrypt($input)
  126. {
  127. return trim(mcrypt_decrypt($this->getCipher(), $this->getKey(), base64_decode($input), $this->getMode()));
  128. }
  129. }