| Package | system.base |
|---|---|
| Inheritance | class CSecurityManager » CApplicationComponent » CComponent |
| Implements | IApplicationComponent |
| Since | 1.0 |
| Source Code | framework/base/CSecurityManager.php |
| Property | Type | Description | Defined By |
|---|---|---|---|
| behaviors | array | the behaviors that should be attached to this component. | CApplicationComponent |
| cryptAlgorithm | mixed | the name of the crypt algorithm to be used by encrypt and decrypt. | CSecurityManager |
| encryptionKey | string | the private key used to encrypt/decrypt data. | CSecurityManager |
| hashAlgorithm | string | the name of the hashing algorithm to be used by computeHMAC. | CSecurityManager |
| isInitialized | boolean | Checks if this application component bas been initialized. | CApplicationComponent |
| validation | string | This method has been deprecated since version 1.1.3. | CSecurityManager |
| validationKey | string | the private key used to generate HMAC. | CSecurityManager |
| Method | Description | Defined By |
|---|---|---|
| __call() | Calls the named method which is not a class method. | CComponent |
| __get() | Returns a property value, an event handler list or a behavior based on its name. | CComponent |
| __isset() | Checks if a property value is null. | CComponent |
| __set() | Sets value of a component property. | CComponent |
| __unset() | Sets a component property to be null. | CComponent |
| asa() | Returns the named behavior object. | CComponent |
| attachBehavior() | Attaches a behavior to this component. | CComponent |
| attachBehaviors() | Attaches a list of behaviors to the component. | CComponent |
| attachEventHandler() | Attaches an event handler to an event. | CComponent |
| canGetProperty() | Determines whether a property can be read. | CComponent |
| canSetProperty() | Determines whether a property can be set. | CComponent |
| decrypt() | Decrypts data | CSecurityManager |
| detachBehavior() | Detaches a behavior from the component. | CComponent |
| detachBehaviors() | Detaches all behaviors from the component. | CComponent |
| detachEventHandler() | Detaches an existing event handler. | CComponent |
| disableBehavior() | Disables an attached behavior. | CComponent |
| disableBehaviors() | Disables all behaviors attached to this component. | CComponent |
| enableBehavior() | Enables an attached behavior. | CComponent |
| enableBehaviors() | Enables all behaviors attached to this component. | CComponent |
| encrypt() | Encrypts data. | CSecurityManager |
| evaluateExpression() | Evaluates a PHP expression or callback under the context of this component. | CComponent |
| getEncryptionKey() | Returns the private key used to encrypt/decrypt data. If the key is not explicitly set, a random one is generated and returned. | CSecurityManager |
| getEventHandlers() | Returns the list of attached event handlers for an event. | CComponent |
| getIsInitialized() | Checks if this application component bas been initialized. | CApplicationComponent |
| getValidation() | This method has been deprecated since version 1.1.3. | CSecurityManager |
| getValidationKey() | Returns the private key used to generate HMAC. If the key is not explicitly set, a random one is generated and returned. | CSecurityManager |
| hasEvent() | Determines whether an event is defined. | CComponent |
| hasEventHandler() | Checks whether the named event has attached handlers. | CComponent |
| hasProperty() | Determines whether a property is defined. | CComponent |
| hashData() | Prefixes data with an HMAC. | CSecurityManager |
| init() | CSecurityManager | |
| raiseEvent() | Raises an event. | CComponent |
| setEncryptionKey() | Sets the key used to encrypt/decrypt data. | CSecurityManager |
| setValidation() | This method has been deprecated since version 1.1.3. | CSecurityManager |
| setValidationKey() | Sets the key used to generate HMAC | CSecurityManager |
| validateData() | Validates if data is tampered. | CSecurityManager |
| Method | Description | Defined By |
|---|---|---|
| computeHMAC() | Computes the HMAC for the data with ValidationKey. | CSecurityManager |
| generateRandomKey() | CSecurityManager | |
| openCryptModule() | Opens the mcrypt module with the configuration specified in cryptAlgorithm. | CSecurityManager |
the name of the crypt algorithm to be used by encrypt and decrypt.
This will be passed as the first parameter to mcrypt_module_open.
This property can also be configured as an array. In this case, the array elements will be passed in order
as parameters to mcrypt_module_open. For example, array('rijndael-256', '', 'ofb', '').
Defaults to 'des', meaning using DES crypt algorithm.
the private key used to encrypt/decrypt data. If the key is not explicitly set, a random one is generated and returned.
the name of the hashing algorithm to be used by computeHMAC.
See hash-algos for the list of possible
hash algorithms. Note that if you are using PHP 5.1.1 or below, you can only use 'sha1' or 'md5'.
Defaults to 'sha1', meaning using SHA1 hash algorithm.
This method has been deprecated since version 1.1.3. Please use hashAlgorithm instead.
the private key used to generate HMAC. If the key is not explicitly set, a random one is generated and returned.
|
protected string computeHMAC(string $data, string $key=NULL)
| ||
| $data | string | data to be generated HMAC |
| $key | string | the private key to be used for generating HMAC. Defaults to null, meaning using validationKey. |
| {return} | string | the HMAC for the data |
protected function computeHMAC($data,$key=null)
{
if($key===null)
$key=$this->getValidationKey();
if(function_exists('hash_hmac'))
return hash_hmac($this->hashAlgorithm, $data, $key);
if(!strcasecmp($this->hashAlgorithm,'sha1'))
{
$pack='H40';
$func='sha1';
}
else
{
$pack='H32';
$func='md5';
}
if($this->strlen($key) > 64)
$key=pack($pack, $func($key));
if($this->strlen($key) < 64)
$key=str_pad($key, 64, chr(0));
$key=$this->substr($key,0,64);
return $func((str_repeat(chr(0x5C), 64) ^ $key) . pack($pack, $func((str_repeat(chr(0x36), 64) ^ $key) . $data)));
}
Computes the HMAC for the data with ValidationKey.
|
public string decrypt(string $data, string $key=NULL)
| ||
| $data | string | data to be decrypted. |
| $key | string | the decryption key. This defaults to null, meaning using EncryptionKey. |
| {return} | string | the decrypted data |
public function decrypt($data,$key=null)
{
$module=$this->openCryptModule();
$key=$this->substr($key===null ? md5($this->getEncryptionKey()) : $key,0,mcrypt_enc_get_key_size($module));
$ivSize=mcrypt_enc_get_iv_size($module);
$iv=$this->substr($data,0,$ivSize);
mcrypt_generic_init($module,$key,$iv);
$decrypted=mdecrypt_generic($module,$this->substr($data,$ivSize,$this->strlen($data)));
mcrypt_generic_deinit($module);
mcrypt_module_close($module);
return rtrim($decrypted,"\0");
}
Decrypts data
|
public string encrypt(string $data, string $key=NULL)
| ||
| $data | string | data to be encrypted. |
| $key | string | the decryption key. This defaults to null, meaning using EncryptionKey. |
| {return} | string | the encrypted data |
public function encrypt($data,$key=null)
{
$module=$this->openCryptModule();
$key=$this->substr($key===null ? md5($this->getEncryptionKey()) : $key,0,mcrypt_enc_get_key_size($module));
srand();
$iv=mcrypt_create_iv(mcrypt_enc_get_iv_size($module), MCRYPT_RAND);
mcrypt_generic_init($module,$key,$iv);
$encrypted=$iv.mcrypt_generic($module,$data);
mcrypt_generic_deinit($module);
mcrypt_module_close($module);
return $encrypted;
}
Encrypts data.
|
protected string generateRandomKey()
| ||
| {return} | string | a randomly generated private key |
protected function generateRandomKey()
{
return sprintf('%08x%08x%08x%08x',mt_rand(),mt_rand(),mt_rand(),mt_rand());
}
|
public string getEncryptionKey()
| ||
| {return} | string | the private key used to encrypt/decrypt data. If the key is not explicitly set, a random one is generated and returned. |
public function getEncryptionKey()
{
if($this->_encryptionKey!==null)
return $this->_encryptionKey;
else
{
if(($key=Yii::app()->getGlobalState(self::STATE_ENCRYPTION_KEY))!==null)
$this->setEncryptionKey($key);
else
{
$key=$this->generateRandomKey();
$this->setEncryptionKey($key);
Yii::app()->setGlobalState(self::STATE_ENCRYPTION_KEY,$key);
}
return $this->_encryptionKey;
}
}
|
public string getValidation()
| ||
| {return} | string | |
public function getValidation()
{
return $this->hashAlgorithm;
}
This method has been deprecated since version 1.1.3. Please use hashAlgorithm instead.
|
public string getValidationKey()
| ||
| {return} | string | the private key used to generate HMAC. If the key is not explicitly set, a random one is generated and returned. |
public function getValidationKey()
{
if($this->_validationKey!==null)
return $this->_validationKey;
else
{
if(($key=Yii::app()->getGlobalState(self::STATE_VALIDATION_KEY))!==null)
$this->setValidationKey($key);
else
{
$key=$this->generateRandomKey();
$this->setValidationKey($key);
Yii::app()->setGlobalState(self::STATE_VALIDATION_KEY,$key);
}
return $this->_validationKey;
}
}
|
public string hashData(string $data, string $key=NULL)
| ||
| $data | string | data to be hashed. |
| $key | string | the private key to be used for generating HMAC. Defaults to null, meaning using validationKey. |
| {return} | string | data prefixed with HMAC |
public function hashData($data,$key=null)
{
return $this->computeHMAC($data,$key).$data;
}
Prefixes data with an HMAC.
|
public void init()
|
public function init()
{
parent::init();
$this->_mbstring=extension_loaded('mbstring');
}
|
protected resource openCryptModule()
| ||
| {return} | resource | the mycrypt module handle. |
protected function openCryptModule()
{
if(extension_loaded('mcrypt'))
{
if(is_array($this->cryptAlgorithm))
$module=@call_user_func_array('mcrypt_module_open',$this->cryptAlgorithm);
else
$module=@mcrypt_module_open($this->cryptAlgorithm,'', MCRYPT_MODE_CBC,'');
if($module===false)
throw new CException(Yii::t('yii','Failed to initialize the mcrypt module.'));
return $module;
}
else
throw new CException(Yii::t('yii','CSecurityManager requires PHP mcrypt extension to be loaded in order to use data encryption feature.'));
}
Opens the mcrypt module with the configuration specified in cryptAlgorithm.
|
public void setEncryptionKey(string $value)
| ||
| $value | string | the key used to encrypt/decrypt data. |
public function setEncryptionKey($value)
{
if(!empty($value))
$this->_encryptionKey=$value;
else
throw new CException(Yii::t('yii','CSecurityManager.encryptionKey cannot be empty.'));
}
|
public void setValidation(string $value)
| ||
| $value | string | - |
public function setValidation($value)
{
$this->hashAlgorithm=$value;
}
This method has been deprecated since version 1.1.3. Please use hashAlgorithm instead.
|
public void setValidationKey(string $value)
| ||
| $value | string | the key used to generate HMAC |
public function setValidationKey($value)
{
if(!empty($value))
$this->_validationKey=$value;
else
throw new CException(Yii::t('yii','CSecurityManager.validationKey cannot be empty.'));
}
|
public string validateData(string $data, string $key=NULL)
| ||
| $data | string | data to be validated. The data must be previously generated using hashData(). |
| $key | string | the private key to be used for generating HMAC. Defaults to null, meaning using validationKey. |
| {return} | string | the real data with HMAC stripped off. False if the data is tampered. |
public function validateData($data,$key=null)
{
$len=$this->strlen($this->computeHMAC('test'));
if($this->strlen($data)>=$len)
{
$hmac=$this->substr($data,0,$len);
$data2=$this->substr($data,$len,$this->strlen($data));
return $hmac===$this->computeHMAC($data2,$key)?$data2:false;
}
else
return false;
}
Validates if data is tampered.
Be the first person to leave a comment
Please login to leave your comment.