Class CSecurityManager
CSecurityManager provides private keys, hashing and encryption functions.
CSecurityManager is used by Yii components and applications for security-related purpose. For example, it is used in cookie validation feature to prevent cookie data from being tampered.
CSecurityManager is mainly used to protect data from being tampered and viewed. It can generate HMAC and encrypt the data. The private key used to generate HMAC is set by setValidationKey ValidationKey. The key used to encrypt data is specified by setEncryptionKey EncryptionKey. If the above keys are not explicitly set, random keys will be generated and used.
To protected data with HMAC, call CSecurityManager::hashData()
; and to check if the
data is tampered, call CSecurityManager::validateData()
, which will return the real data
if it is not tampered. The algorithm used to generated HMAC is specified by
validation.
To encrypt and decrypt data, call CSecurityManager::encrypt()
and CSecurityManager::decrypt()
respectively, which uses 3DES encryption algorithm. Note, the PHP Mcrypt
extension must be installed and loaded.
CSecurityManager is a core application component that can be accessed via
CApplication::getSecurityManager()
.
- CComponent
- CApplicationComponent implements IApplicationComponent
- CSecurityManager
Copyright: 2008-2013 Yii Software LLC
License: http://www.yiiframework.com/license/
Author: Qiang Xue <qiang.xue@gmail.com>
Since: 1.0
Located at x2engine/framework/base/CSecurityManager.php
public
|
#
init( )
Initializes the application component. This method is required by |
public
string
|
|
public
|
|
public
string
|
|
public
|
|
public
string
|
|
public
string
|
|
protected
resource
|
#
openCryptModule( )
Opens the mcrypt module with the configuration specified in |
public
string
|
|
public
string
|
|
public
string
|
#
computeHMAC( string $data, string|null $key = null, string|null $hashAlgorithm = null )
Computes the HMAC for the data with getValidationKey validationKey. This method has been made public since 1.1.14. |
public
string|boolean
|
#
generateRandomString( integer $length, boolean $cryptographicallyStrong = true )
Generate a random ASCII string. Generates only [0-9a-zA-z_~] characters which are all transparent in raw URL encoding. |
public
boolean|string
|
#
generateRandomBytes( integer $length, boolean $cryptographicallyStrong = true )
Generates a string of random bytes. |
public
string
|
#
generatePseudoRandomBlock( )
Generate a pseudo random block of data using several sources. On some systems this may be a bit better than PHP's mt_rand built-in function, which is not really random. |
public
boolean|string
|
#
generateSessionRandomBlock( )
Get random bytes from the system entropy source via PHP session manager. |
protected
boolean
|
#
validateEncryptionKey( string $key )
Checks if a key is valid for |
public
boolean
|
#
compareString( string $expected, string $actual )
Performs string comparison using timing attack resistant approach. |
getIsInitialized()
|
string |
STATE_VALIDATION_KEY
|
'Yii.CSecurityManager.validationkey' |
|
string |
STATE_ENCRYPTION_KEY
|
'Yii.CSecurityManager.encryptionkey' |
protected static
array
|
$encryptionKeyMinimumLengths | array(
'blowfish'=>4,
'arcfour'=>5,
'rc2'=>5,
) |
#
known minimum lengths per encryption algorithm |
public
boolean
|
$validateEncryptionKey | true |
#
if encryption key should be validated |
public
string
|
$hashAlgorithm | 'sha1' |
#
the name of the hashing algorithm to be used by Defaults to 'sha1', meaning using SHA1 hash algorithm. |
public
mixed
|
$cryptAlgorithm | 'rijndael-128' |
#
the name of the crypt algorithm to be used by 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, Defaults to AES Note: MCRYPT_RIJNDAEL192 and MCRYPT_RIJNDAEL256 are not AES-192 and AES-256. The numbers of the MCRYPT_RIJNDAEL constants refer to the block size, whereas the numbers of the AES variants refer to the key length. AES is Rijndael with a block size of 128 bits and a key length of 128 bits, 192 bits or 256 bits. So to use AES in Mcrypt, you need MCRYPT_RIJNDAEL128 and a key with 16 bytes (AES-128), 24 bytes (AES-192) or 32 bytes (AES-256). The other two Rijndael variants in Mcrypt should be avoided, because they're not standardized and have been analyzed much less than AES. |
$behaviors
|
public
string
|
$validationKey |
#
The private key used to generate HMAC. If the key is not explicitly set, a random one is generated and returned. |
public
string
|
$encryptionKey |
#
The private key used to encrypt/decrypt data. If the key is not explicitly set, a random one is generated and returned. |
public
string
|
$validation |
$isInitialized
|