_key = hash('sha256', $key, true); return $this; } /** * * get encryption key * * @throws \RuntimeException * @return string */ public function getKey() { if (empty($this->_key)) { throw new \RuntimeException("The encryption key has not been set."); } return $this->_key; } /** * * set mcrypt cipher * * @param string $cipher * @return $this */ public function setCipher($cipher) { $this->_cipher = $cipher; return $this; } /** * * get mcrypt cipher * * @return string */ public function getCipher() { return $this->_cipher; } /** * * set mcrypt mode * * @param string $mode * @return $this */ public function setMode($mode) { $this->_mode = $mode; } /** * * get mcrypt mode * * @return string */ public function getMode() { return $this->_mode; } /** * * encrypt a string * * @param $input * @return string */ function encrypt($input) { return base64_encode(mcrypt_encrypt($this->getCipher(), $this->getKey(), $input, $this->getMode())); } /** * * decrypt an encrypted string * * @param $input * @return string */ function decrypt($input) { return trim(mcrypt_decrypt($this->getCipher(), $this->getKey(), base64_decode($input), $this->getMode())); } }