Phalcon crypt
NOTE
All classes are prefixed with Phalcon
Crypt¶
-
Namespace
Phalcon
-
Uses
Phalcon\Crypt\CryptInterface
Phalcon\Crypt\Exception\Exception
Phalcon\Crypt\Exception\Mismatch
Phalcon\Crypt\PadFactory
-
Extends
-
Implements
CryptInterface
Provides encryption capabilities to Phalcon applications.
use Phalcon\Crypt;
$crypt = new Crypt();
$crypt->setCipher('aes-256-ctr');
$key = "T4\xb1\x8d\xa9\x98\x05\\\x8c\xbe\x1d\x07&[\x99\x18\xa4~Lc1\xbeW\xb3";
$text = "The message to be encrypted";
$encrypted = $crypt->encrypt($text, $key);
echo $crypt->decrypt($encrypted, $key);
Constants¶
const PADDING_ANSI_X_923 = 1;
const PADDING_DEFAULT = 0;
const PADDING_ISO_10126 = 3;
const PADDING_ISO_IEC_7816_4 = 4;
const PADDING_PKCS7 = 2;
const PADDING_SPACE = 6;
const PADDING_ZERO = 5;
Properties¶
/**
* @var string
*/
protected $authTag;
/**
* @var string
*/
protected $authData = '';
/**
* @var int
*/
protected $authTagLength = 16;
/**
* @var string
*/
protected $key = ;
/**
* @var int
*/
protected $padding = 0;
/**
* @var string
*/
protected $cipher = 'aes-256-cfb';
/**
* Available cipher methods.
* @var array
*/
protected $availableCiphers;
/**
* The cipher iv length.
* @var int
*/
protected $ivLength = 16;
/**
* The name of hashing algorithm.
* @var string
*/
protected $hashAlgo = 'sha256';
/**
* Whether calculating message digest enabled or not.
*
* @var bool
*/
protected $useSigning = true;
Methods¶
Phalcon\Crypt constructor. Decrypts an encrypted text.$encrypted = $crypt->decrypt(
$encrypted,
"T4\xb1\x8d\xa9\x98\x05\\\x8c\xbe\x1d\x07&[\x99\x18\xa4~Lc1\xbeW\xb3"
);
$encrypted = $crypt->encrypt(
"Top secret",
"T4\xb1\x8d\xa9\x98\x05\\\x8c\xbe\x1d\x07&[\x99\x18\xa4~Lc1\xbeW\xb3"
);
The `aes-256-gcm' is the preferable cipher, but it is not usable until the openssl library is upgraded, which is available in PHP 7.1.
The `aes-256-ctr' is arguably the best choice for cipher algorithm for current openssl library version.
Set the name of hashing algorithm. Sets the encryption key.The `$key' should have been previously generated in a cryptographically safe way.
Bad key: "le password"
Better (but still unsafe): "#1dj8$=dp?.ak//j1V$~%*0X"
Good key: "T4\xb1\x8d\xa9\x98\x05\\x8c\xbe\x1d\x07&[\x99\x18\xa4~Lc1\xbeW\xb3"
Changes the padding scheme used. Sets if the calculating message digest must used. Assert the cipher is available. Assert the hash algorithm is available.protected function cryptPadText( string $text, string $mode, int $blockSize, int $paddingType ): string;
If the function detects that the text was not padded, it will return it unmodified.
Initialize available cipher algorithms. Initialize available cipher algorithms.Crypt\CryptInterface ¶
-
Namespace
Phalcon\Crypt
-
Uses
-
Extends
-
Implements
Interface for Phalcon\Crypt
Methods¶
Decrypts a text Decrypt a text that is coded as a base64 string Encrypts a text Encrypts a text returning the result as a base64 string Returns authentication data Returns the authentication tag Returns the authentication tag length Returns a list of available cyphers Returns the current cipher Returns the encryption key Sets authentication data Sets the authentication tag Sets the authentication tag length Sets the cipher algorithm Sets the encryption key Changes the padding scheme used.Crypt\Exception\Exception¶
-
Namespace
Phalcon\Crypt\Exception
-
Uses
-
Extends
\Exception
-
Implements
Exceptions thrown in Phalcon\Crypt use this class
Crypt\Exception\Mismatch¶
-
Namespace
Phalcon\Crypt\Exception
-
Uses
-
Extends
Exception
-
Implements
Exceptions thrown in Phalcon\Crypt will use this class.