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
ValidationKey. The key used to encrypt data is
specified by
EncryptionKey. If the above keys are not
explicitly set, random keys will be generated and used.
To protected data with HMAC, call
hashData(); and to check if the data
is tampered, call
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
encrypt() and
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().
Property Details
the private key used to encrypt/decrypt data.
If the key is not explicitly set, a random one is generated and returned.
hashing algorithm used to generate HMAC. Defaults to 'SHA1'.
the private key used to generate HMAC.
If the key is not explicitly set, a random one is generated and returned.
Method Details
|
protected string computeHMAC(string $data)
|
| $data |
string |
data to be generated HMAC |
| {return} |
string |
the HMAC for the data |
Computes the HMAC for the data with ValidationKey.
|
public string decrypt(string $data)
|
| $data |
string |
data to be decrypted. |
| {return} |
string |
the decrypted data |
Decrypts data with EncryptionKey.
|
public string encrypt(string $data)
|
| $data |
string |
data to be encrypted. |
| {return} |
string |
the encrypted data |
Encrypts data with EncryptionKey.
generateRandomKey()
|
protected string generateRandomKey()
|
| {return} |
string |
a randomly generated key |
|
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 string getValidation()
|
| {return} |
string |
hashing algorithm used to generate HMAC. Defaults to 'SHA1'. |
|
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 string hashData(string $data)
|
| $data |
string |
data to be hashed. |
| {return} |
string |
data prefixed with HMAC |
Prefixes data with an HMAC.
|
public void setEncryptionKey(string $value)
|
| $value |
string |
the key used to encrypt/decrypt data. |
|
public void setValidation(string $value)
|
| $value |
string |
hashing algorithm used to generate HMAC. It must be either 'MD5' or 'SHA1'. |
|
public void setValidationKey(string $value)
|
| $value |
string |
the key used to generate HMAC |
|
public string validateData(string $data)
|
| $data |
string |
data to be validated. The data must be previously
generated using hashData(). |
| {return} |
string |
the real data with HMAC stripped off. False if the data
is tampered. |
Validates if data is tampered.