summaryrefslogtreecommitdiffstats
path: root/system/libraries/Encrypt.php
diff options
context:
space:
mode:
Diffstat (limited to 'system/libraries/Encrypt.php')
-rw-r--r--system/libraries/Encrypt.php58
1 files changed, 47 insertions, 11 deletions
diff --git a/system/libraries/Encrypt.php b/system/libraries/Encrypt.php
index 54b5bf737..cdb0a6452 100644
--- a/system/libraries/Encrypt.php
+++ b/system/libraries/Encrypt.php
@@ -1,4 +1,4 @@
-<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+<?php
/**
* CodeIgniter
*
@@ -24,6 +24,7 @@
* @since Version 1.0
* @filesource
*/
+defined('BASEPATH') OR exit('No direct script access allowed');
/**
* CodeIgniter Encryption Class
@@ -38,12 +39,46 @@
*/
class CI_Encrypt {
+ /**
+ * Reference to the user's encryption key
+ *
+ * @var string
+ */
public $encryption_key = '';
+
+ /**
+ * Type of hash operation
+ *
+ * @var string
+ */
protected $_hash_type = 'sha1';
+
+ /**
+ * Flag for the existance of mcrypt
+ *
+ * @var bool
+ */
protected $_mcrypt_exists = FALSE;
+
+ /**
+ * Current cipher to be used with mcrypt
+ *
+ * @var string
+ */
protected $_mcrypt_cipher;
+
+ /**
+ * Method for encrypting/decrypting data
+ *
+ * @var int
+ */
protected $_mcrypt_mode;
+ /**
+ * Initialize Encryption class
+ *
+ * @return void
+ */
public function __construct()
{
$this->_mcrypt_exists = function_exists('mcrypt_encrypt');
@@ -63,15 +98,14 @@ class CI_Encrypt {
*/
public function get_key($key = '')
{
- if ($key == '')
+ if ($key === '')
{
- if ($this->encryption_key != '')
+ if ($this->encryption_key !== '')
{
return $this->encryption_key;
}
- $CI =& get_instance();
- $key = $CI->config->item('encryption_key');
+ $key = config_item('encryption_key');
if ($key === FALSE)
{
@@ -132,7 +166,7 @@ class CI_Encrypt {
*/
public function decode($string, $key = '')
{
- if (preg_match('/[^a-zA-Z0-9\/\+=]/', $string))
+ if (preg_match('/[^a-zA-Z0-9\/\+=]/', $string) OR base64_encode(base64_decode($string)) !== $string)
{
return FALSE;
}
@@ -180,6 +214,7 @@ class CI_Encrypt {
$dec = base64_decode($string);
if (($dec = $this->mcrypt_decode($dec, $key)) === FALSE)
{
+ $this->set_mode($current_mode);
return FALSE;
}
@@ -349,7 +384,8 @@ class CI_Encrypt {
*
* Function description
*
- * @param string
+ * @param string $data
+ * @param string $key
* @return string
*/
protected function _remove_cipher_noise($data, $key)
@@ -414,7 +450,7 @@ class CI_Encrypt {
*/
protected function _get_cipher()
{
- if ($this->_mcrypt_cipher == '')
+ if ($this->_mcrypt_cipher === NULL)
{
return $this->_mcrypt_cipher = MCRYPT_RIJNDAEL_256;
}
@@ -431,7 +467,7 @@ class CI_Encrypt {
*/
protected function _get_mode()
{
- if ($this->_mcrypt_mode == '')
+ if ($this->_mcrypt_mode === NULL)
{
return $this->_mcrypt_mode = MCRYPT_MODE_CBC;
}
@@ -449,7 +485,7 @@ class CI_Encrypt {
*/
public function set_hash($type = 'sha1')
{
- $this->_hash_type = ($type !== 'sha1' && $type !== 'md5') ? 'sha1' : $type;
+ $this->_hash_type = in_array($type, hash_algos()) ? $type : 'sha1';
}
// --------------------------------------------------------------------
@@ -462,7 +498,7 @@ class CI_Encrypt {
*/
public function hash($str)
{
- return ($this->_hash_type === 'sha1') ? sha1($str) : md5($str);
+ return hash($this->_hash_type, $str);
}
}