123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php
- namespace Cube;
- class Crypt
- {
-
- protected $_key;
-
- protected $_cipher = MCRYPT_RIJNDAEL_256;
-
- protected $_mode = MCRYPT_MODE_ECB;
-
- public function setKey($key)
- {
- $this->_key = hash('sha256', $key, true);
- return $this;
- }
-
- public function getKey()
- {
- if (empty($this->_key)) {
- throw new \RuntimeException("The encryption key has not been set.");
- }
- return $this->_key;
- }
-
- public function setCipher($cipher)
- {
- $this->_cipher = $cipher;
- return $this;
- }
-
- public function getCipher()
- {
- return $this->_cipher;
- }
-
- public function setMode($mode)
- {
- $this->_mode = $mode;
- }
-
- public function getMode()
- {
- return $this->_mode;
- }
-
- function encrypt($input)
- {
- return base64_encode(mcrypt_encrypt($this->getCipher(), $this->getKey(), $input, $this->getMode()));
- }
-
- function decrypt($input)
- {
- return trim(mcrypt_decrypt($this->getCipher(), $this->getKey(), base64_decode($input), $this->getMode()));
- }
- }
|