Security Utility
- class
Cake\Utility\
Security
- The security libraryhandles basic security measures such as providing methods forhashing and encrypting data.
Encrypting and Decrypting Data
- static
Cake\Utility\Security::
encrypt
($text, $key, $hmacSalt = null) - static
Cake\Utility\Security::
decrypt
($cipher, $key, $hmacSalt = null) - Encrypt
$text
using AES-256. The$key
should be a value with alots of variance in the data much like a good password. The returned resultwill be the encrypted value with an HMAC checksum.
This method will use either openssl or mcrypt based on what is available on your system. Dataencrypted in one implementation is portable to the other.
Warning
The mcrypt extension has been deprecated inPHP7.1
This method should never be used to store passwords. Instead you should usethe one way hashing methods provided byUtility\Security::hash()
. An example use would be:
- // Assuming key is stored somewhere it can be re-used for
- // decryption later.
- $key = 'wt1U5MACWJFTXGenFoZoiLwQGrLgdbHA';
- $result = Security::encrypt($value, $key);
If you do not supply an HMAC salt, the Security.salt
value will be used.Encrypted values can be decrypted usingCake\Utility\Security::decrypt()
.
Decrypt a previously encrypted value. The $key
and $hmacSalt
parameters must match the values used to encrypt or decryption will fail. Anexample use would be:
- // Assuming the key is stored somewhere it can be re-used for
- // Decryption later.
- $key = 'wt1U5MACWJFTXGenFoZoiLwQGrLgdbHA';
- $cipher = $user->secrets;
- $result = Security::decrypt($cipher, $key);
If the value cannot be decrypted due to changes in the key or HMAC saltfalse
will be returned.
Choosing a Specific Crypto Implementation
If you are upgrading an application from CakePHP 2.x, data encrypted in 2.x isnot compatible with openssl. This is because the encrypted data is not fully AEScompliant. If you don’t want to go through the trouble of re-encrypting yourdata, you can force CakePHP to use mcrypt
using the engine()
method:
- // In config/bootstrap.php
- use Cake\Utility\Crypto\Mcrypt;
- Security::engine(new Mcrypt());
The above will allow you to seamlessly read data from older versions of CakePHP,and encrypt new data to be compatible with OpenSSL.
Hashing Data
- static
Cake\Utility\Security::
hash
($string, $type = NULL, $salt = false) - Create a hash from string using given method. Fallback on nextavailable method. If
$salt
is set totrue
, the application’s saltvalue will be used:
- // Using the application's salt value
- $sha1 = Security::hash('CakePHP Framework', 'sha1', true);
- // Using a custom salt value
- $sha1 = Security::hash('CakePHP Framework', 'sha1', 'my-salt');
- // Using the default hash algorithm
- $hash = Security::hash('CakePHP Framework');
The hash()
method supports the following hashing strategies:
- md5
- sha1
- sha256
And any other hash algorithmn that PHP’shash()
function supports.
Warning
You should not be using hash()
for passwords in new applications.Instead you should use the DefaultPasswordHasher
class which uses bcryptby default.
Getting Secure Random Data
- static
Cake\Utility\Security::
randomBytes
($length) Get
$length
number of bytes from a secure random source. This function drawsdata from one of the following sources:PHP’s
random_bytes
function.openssl_random_pseudo_bytes
from the SSL extension.
If neither source is available a warning will be emitted and an unsafe valuewill be used for backwards compatibility reasons.
New in version 3.2.3: The randomBytes method was added.
- static
Cake\Utility\Security::
randomString
($length) - Get a random string
$length
long from a secure random source. This methoddraws from the same random source asrandomBytes()
and will encode the dataas a hexadecimal string.
New in version 3.6.0: The randomString method was added.