From 85dbdee3f7dbd092efec888da77da8d5bf2630d7 Mon Sep 17 00:00:00 2001 From: Anton Vasiliev Date: Wed, 29 Jul 2020 21:21:23 +0100 Subject: [PATCH] #911 - Remove Legacy\Crypt --- Library/Phalcon/Legacy/Crypt.php | 546 --------------------- Library/Phalcon/Legacy/Crypt/Exception.php | 32 -- Library/Phalcon/Legacy/CryptInterface.php | 127 ----- tests/unit/Legacy/CryptTest.php | 179 ------- 4 files changed, 884 deletions(-) delete mode 100644 Library/Phalcon/Legacy/Crypt.php delete mode 100644 Library/Phalcon/Legacy/Crypt/Exception.php delete mode 100644 Library/Phalcon/Legacy/CryptInterface.php delete mode 100644 tests/unit/Legacy/CryptTest.php diff --git a/Library/Phalcon/Legacy/Crypt.php b/Library/Phalcon/Legacy/Crypt.php deleted file mode 100644 index 4dd465779..000000000 --- a/Library/Phalcon/Legacy/Crypt.php +++ /dev/null @@ -1,546 +0,0 @@ - | - | Eduar Carvajal | - +------------------------------------------------------------------------+ -*/ - -namespace Phalcon\Legacy; - -use Phalcon\Legacy\Crypt\Exception; - -/** - * Phalcon\Legacy\Crypt - * - * Port of Phalcon 2.0.x Phalcon\Crypt. - * Provides encryption facilities to phalcon applications. - * - * - * $crypt = new \Phalcon\Legacy\Crypt(); - * - * $key = 'le password'; - * $text = 'This is a secret text'; - * - * $encrypted = $crypt->encrypt($text, $key); - * - * echo $crypt->decrypt($encrypted, $key); - * - * - * @link https://github.com/phalcon/incubator/issues/563 - * @package Phalcon\Legacy - */ -class Crypt implements CryptInterface -{ - protected $key; - - protected $padding = 0; - - protected $mode = MCRYPT_MODE_CBC; - - protected $cipher = "rijndael-256"; - - const PADDING_DEFAULT = 0; - - const PADDING_ANSI_X_923 = 1; - - const PADDING_PKCS7 = 2; - - const PADDING_ISO_10126 = 3; - - const PADDING_ISO_IEC_7816_4 = 4; - - const PADDING_ZERO = 5; - - const PADDING_SPACE = 6; - - /** - * Changes the padding scheme used - * - * @param int $scheme - * @return CryptInterface - */ - public function setPadding($scheme) - { - $this->padding = $scheme; - - return $this; - } - - /** - * @inheritdoc - * - * @param string $cipher - * @return CryptInterface - */ - public function setCipher($cipher) - { - $this->cipher = $cipher; - - return $this; - } - - /** - * Returns the current cipher - * - * @return string - */ - public function getCipher() - { - return $this->cipher; - } - - /** - * Sets the encrypt/decrypt mode - * - * @param string $mode - * @return CryptInterface - */ - public function setMode($mode) - { - $this->mode = $mode; - - return $this; - } - - /** - * Returns the current encryption mode - * - * @return string - */ - public function getMode() - { - return $this->mode; - } - - /** - * Sets the encryption key - * - * @param string $key - * @return CryptInterface - */ - public function setKey($key) - { - $this->key = $key; - - return $this; - } - - /** - * Returns the encryption key - * - * @return string - */ - public function getKey() - { - return $this->key; - } - - /** - * Pads texts before encryption - * - * @link http://www.di-mgt.com.au/cryptopad.html - * - * @param string $text - * @param string $mode - * @param int $blockSize - * @param int $paddingType - * @return string - * @throws Exception - */ - protected function cryptPadText($text, $mode, $blockSize, $paddingType) - { - $paddingSize = 0; - $padding = null; - - if ($mode == MCRYPT_MODE_CBC || $mode == MCRYPT_MODE_ECB) { - $paddingSize = $blockSize - (strlen($text) % $blockSize); - - if ($paddingSize >= 256) { - throw new Exception("Block size is bigger than 256"); - } - - switch ($paddingType) { - case self::PADDING_ANSI_X_923: - $padding = str_repeat(chr(0), $paddingSize - 1) . chr($paddingSize); - - break; - - case self::PADDING_PKCS7: - $padding = str_repeat( - chr($paddingSize), - $paddingSize - ); - - break; - - case self::PADDING_ISO_10126: - $padding = ""; - - foreach (range(0, $paddingSize - 2) as $i) { - $padding .= chr( - rand() - ); - } - - $padding .= chr($paddingSize); - - break; - - case self::PADDING_ISO_IEC_7816_4: - $padding = chr(0x80) . str_repeat(chr(0), $paddingSize - 1); - - break; - - case self::PADDING_ZERO: - $padding = str_repeat( - chr(0), - $paddingSize - ); - - break; - - case self::PADDING_SPACE: - $padding = str_repeat(" ", $paddingSize); - - break; - - default: - $paddingSize = 0; - - break; - } - } - - if (!$paddingSize) { - return $text; - } - - if ($paddingSize > $blockSize) { - throw new Exception("Invalid padding size"); - } - - return $text . substr($padding, 0, $paddingSize); - } - - /** - * Removes $paddingType padding from text - * If the function detects that the text was not padded, it will return it unmodified - * - * @param string $text Message to be unpadded - * @param string $mode Encryption mode; unpadding is applied only in CBC or ECB mode - * @param int $blockSize Cipher block size - * @param int $paddingType Padding scheme - * @return string - */ - protected function cryptUnpadText($text, $mode, $blockSize, $paddingType) - { - $paddingSize = 0; - $length = strlen($text); - - if ($length > 0 && ($length % $blockSize == 0) && ($mode == MCRYPT_MODE_CBC || $mode == MCRYPT_MODE_ECB)) { - switch ($paddingType) { - case self::PADDING_ANSI_X_923: - $last = substr($text, $length - 1, 1); - $ord = (int) ord($last); - - if ($ord <= $blockSize) { - $paddingSize = $ord; - - $padding = str_repeat(chr(0), $paddingSize - 1) . $last; - - if (substr($text, $length - $paddingSize) != $padding) { - $paddingSize = 0; - } - } - - break; - - case self::PADDING_PKCS7: - $last = substr($text, $length - 1, 1); - $ord = (int) ord($last); - - if ($ord <= $blockSize) { - $paddingSize = $ord; - - $padding = str_repeat( - chr($paddingSize), - $paddingSize - ); - - if (substr($text, $length - $paddingSize) != $padding) { - $paddingSize = 0; - } - } - - break; - - case self::PADDING_ISO_10126: - $last = substr($text, $length - 1, 1); - $paddingSize = (int) ord($last); - - break; - - case self::PADDING_ISO_IEC_7816_4: - $i = $length - 1; - - while ($i > 0 && $text[$i] == 0x00 && $paddingSize < $blockSize) { - $paddingSize++; - $i--; - } - - if ($text[$i] == 0x80) { - $paddingSize++; - } else { - $paddingSize = 0; - } - - break; - - case self::PADDING_ZERO: - $i = $length - 1; - - while ($i >= 0 && $text[$i] == 0x00 && $paddingSize <= $blockSize) { - $paddingSize++; - $i--; - } - - break; - - case self::PADDING_SPACE: - $i = $length - 1; - - while ($i >= 0 && $text[$i] == 0x20 && $paddingSize <= $blockSize) { - $paddingSize++; - $i--; - } - - break; - - default: - break; - } - - if ($paddingSize && $paddingSize <= $blockSize) { - if ($paddingSize < $length) { - return substr($text, 0, $length - $paddingSize); - } - - return ""; - } - } - - return $text; - } - - /** - * Encrypts a text - * - * - * $encrypted = $crypt->encrypt("Ultra-secret text", "encrypt password"); - * - * - * @param string $text - * @param mixed $key - * @return string - * @throws Exception - */ - public function encrypt($text, $key = null) - { - if (!function_exists("mcrypt_get_iv_size")) { - throw new Exception("mcrypt extension is required"); - } - - if ($key === null) { - $encryptKey = $this->key; - } else { - $encryptKey = $key; - } - - if (empty($encryptKey)) { - throw new Exception("Encryption key cannot be empty"); - } - - $ivSize = mcrypt_get_iv_size($this->cipher, $this->mode); - if (strlen($encryptKey) > $ivSize) { - throw new Exception("Size of key is too large for this algorithm"); - } - - $iv = strval( - mcrypt_create_iv($ivSize, MCRYPT_RAND) - ); - - $blockSize = intval( - mcrypt_get_block_size($this->cipher, $this->mode) - ); - - if ($this->padding != 0 && ($this->mode == MCRYPT_MODE_CBC || $this->mode == MCRYPT_MODE_ECB)) { - $padded = $this->cryptPadText( - $text, - $this->mode, - $blockSize, - $this->padding - ); - } else { - $padded = $text; - } - - return $iv . mcrypt_encrypt( - $this->cipher, - $encryptKey, - $padded, - $this->mode, - $iv - ); - } - - /** - * Decrypts a text - * - * - * echo $crypt->decrypt($encrypted, "decrypt password"); - * - * - * @param string $text - * @param string $key - * @return string - * @throws Exception - */ - public function decrypt($text, $key = null) - { - if (!function_exists("mcrypt_get_iv_size")) { - throw new Exception("mcrypt extension is required"); - } - - if ($key === null) { - $decryptKey = $this->key; - } else { - $decryptKey = $key; - } - - if (empty($decryptKey)) { - throw new Exception("Decryption key cannot be empty"); - } - - $ivSize = mcrypt_get_iv_size($this->cipher, $this->mode); - $keySize = strlen($decryptKey); - if ($keySize > $ivSize) { - throw new Exception("Size of key is too large for this algorithm"); - } - - if ($keySize > strlen($text)) { - throw new Exception( - "Size of IV is larger than text to decrypt. Are you trying to decrypt an uncrypted text?" - ); - } - - $data = substr($text, $ivSize); - - $decrypted = mcrypt_decrypt( - $this->cipher, - $decryptKey, - $data, - $this->mode, - substr($text, 0, $ivSize) - ); - - $blockSize = mcrypt_get_block_size($this->cipher, $this->mode); - - if ($this->mode == MCRYPT_MODE_CBC || $this->mode == MCRYPT_MODE_ECB) { - return $this->cryptUnpadText( - $decrypted, - $this->mode, - $blockSize, - $this->padding - ); - } - - return $decrypted; - } - - /** - * Encrypts a text returning the result as a base64 string - * - * @param string $text - * @param mixed $key - * @param bool $safe - * @return string - */ - public function encryptBase64($text, $key = null, $safe = false) - { - if ($safe) { - return strtr( - base64_encode( - $this->encrypt($text, $key) - ), - "+/", - "-_" - ); - } - - return base64_encode( - $this->encrypt($text, $key) - ); - } - - /** - * Decrypt a text that is coded as a base64 string - * - * @param string $text - * @param mixed $key - * @param bool $safe - * @return string - */ - public function decryptBase64($text, $key = null, $safe = false) - { - if ($safe) { - return $this->decrypt( - base64_decode( - strtr( - $text, - "-_", - "+/" - ) - ), - $key - ); - } - - return $this->decrypt( - base64_decode($text), - $key - ); - } - - /** - * Returns a list of available cyphers - * - * @return array - */ - public function getAvailableCiphers() - { - return mcrypt_list_algorithms(); - } - - /** - * Returns a list of available modes - * - * @return array - */ - public function getAvailableModes() - { - return mcrypt_list_modes(); - } -} diff --git a/Library/Phalcon/Legacy/Crypt/Exception.php b/Library/Phalcon/Legacy/Crypt/Exception.php deleted file mode 100644 index 64fe616c0..000000000 --- a/Library/Phalcon/Legacy/Crypt/Exception.php +++ /dev/null @@ -1,32 +0,0 @@ - | - | Eduar Carvajal | - +------------------------------------------------------------------------+ -*/ - -namespace Phalcon\Legacy\Crypt; - -/** - * Phalcon\Legacy\Crypt\Exception - * - * Exceptions thrown in Phalcon\Crypt use this class - * - * @package Phalcon\Legacy - */ -class Exception extends \Phalcon\Exception -{ -} diff --git a/Library/Phalcon/Legacy/CryptInterface.php b/Library/Phalcon/Legacy/CryptInterface.php deleted file mode 100644 index 94faf8f9e..000000000 --- a/Library/Phalcon/Legacy/CryptInterface.php +++ /dev/null @@ -1,127 +0,0 @@ - | - | Eduar Carvajal | - +------------------------------------------------------------------------+ -*/ - -namespace Phalcon\Legacy; - -/** - * Phalcon\Legacy\CryptInterface - * - * Interface for Phalcon\Legacy\Crypt - * - * @link https://github.com/phalcon/incubator/issues/563 - * @package Phalcon\Legacy - */ -interface CryptInterface -{ - /** - * Sets the cipher algorithm - * - * @param string $cipher - * @return CryptInterface - */ - public function setCipher($cipher); - - /** - * Returns the current cipher - * - * @return string - */ - public function getCipher(); - - /** - * Sets the encrypt/decrypt mode - * - * @param string $mode - * @return CryptInterface - */ - public function setMode($mode); - - /** - * Returns the current encryption mode - * - * @return string - */ - public function getMode(); - - /** - * Sets the encryption key - * - * @param string $key - * @return CryptInterface - */ - public function setKey($key); - - /** - * Returns the encryption key - * - * @return string - */ - public function getKey(); - - /** - * Encrypts a text - * - * @param string $text - * @param mixed $key - * @return string - */ - public function encrypt($text, $key = null); - - /** - * Decrypts a text - * - * @param string $text - * @param string $key - * @return string - */ - public function decrypt($text, $key = null); - - /** - * Encrypts a text returning the result as a base64 string - * - * @param string $text - * @param mixed $key - * @return string - */ - public function encryptBase64($text, $key = null); - - /** - * Decrypt a text that is coded as a base64 string - * - * @param string $text - * @param mixed $key - * @return string - */ - public function decryptBase64($text, $key = null); - - /** - * Returns a list of available cyphers - * - * @return array - */ - public function getAvailableCiphers(); - - /** - * Returns a list of available modes - * - * @return array - */ - public function getAvailableModes(); -} diff --git a/tests/unit/Legacy/CryptTest.php b/tests/unit/Legacy/CryptTest.php deleted file mode 100644 index f27e6148a..000000000 --- a/tests/unit/Legacy/CryptTest.php +++ /dev/null @@ -1,179 +0,0 @@ - - * @package Phalcon\Test\Legacy - * @group crypt - * - * The contents of this file are subject to the New BSD License that is - * bundled with this package in the file docs/LICENSE.txt - * - * If you did not receive a copy of the license and are unable to obtain it - * through the world-wide-web, please send an email to license@phalconphp.com - * so that we can send you a copy immediately. - */ -class CryptTest extends Test -{ - use Specify; - - public function _before() - { - parent::_before(); - - if (!extension_loaded('mcrypt')) { - $this->markTestSkipped('Warning: mcrypt extension is not loaded'); - } - } - - - /** - * Tests the Crypt constants - * - * @author Serghei Iakovlev - * @since 2015-12-20 - */ - public function testCryptConstants() - { - $this->specify( - "Crypt constants are not correct", - function ($const, $expected) { - expect($const)->equals($expected); - }, - [ - 'examples' => [ - [Crypt::PADDING_DEFAULT, 0], - [Crypt::PADDING_ANSI_X_923, 1], - [Crypt::PADDING_PKCS7, 2], - [Crypt::PADDING_ISO_10126, 3], - [Crypt::PADDING_ISO_IEC_7816_4, 4], - [Crypt::PADDING_ZERO, 5], - [Crypt::PADDING_SPACE, 6], - ], - ] - ); - } - - /** - * Tests the encryption - * - * @author Nikolaos Dimopoulos - * @since 2014-10-17 - */ - public function testCryptEncryption() - { - $this->specify( - "encryption does not return correct results", - function ($key, $test) { - $modes = [ - MCRYPT_MODE_ECB, - MCRYPT_MODE_CBC, - MCRYPT_MODE_CFB, - MCRYPT_MODE_OFB, - MCRYPT_MODE_NOFB, - ]; - - $crypt = new Crypt(); - - foreach ($modes as $mode) { - $crypt->setMode($mode); - $crypt->setKey(substr($key, 0, 16)); - - $encryption = $crypt->encrypt($test); - expect(rtrim($crypt->decrypt($encryption), "\0"))->equals($test); - - $encryption = $crypt->encrypt($test, substr($key, 0, 16)); - expect(rtrim($crypt->decrypt($encryption, substr($key, 0, 16)), "\0"))->equals($test); - } - }, - [ - 'examples' => [ - [md5(uniqid()), str_repeat('x', mt_rand(1, 255))], - [time().time(), str_shuffle(join('', range('a', 'z')))], - ['le$ki12432543543543543', null], - ], - ] - ); - } - - /** - * Tests the padding - * - * @author Nikolaos Dimopoulos - * @since 2014-10-17 - */ - public function testCryptPadding() - { - $this->specify( - "padding not return correct results", - function () { - $texts = ['']; - $key = '0123456789ABCDEF0123456789ABCDEF'; - $modes = [MCRYPT_MODE_ECB, MCRYPT_MODE_CBC, MCRYPT_MODE_CFB]; - $pads = [ - Crypt::PADDING_ANSI_X_923, - Crypt::PADDING_PKCS7, - ]; - - for ($i = 1; $i < 128; ++$i) { - $texts[] = str_repeat('A', $i); - } - - $crypt = new Crypt(); - - $crypt->setCipher(MCRYPT_RIJNDAEL_256) - ->setKey(substr($key, 0, 16)); - - foreach ($pads as $padding) { - $crypt->setPadding($padding); - - foreach ($modes as $mode) { - $crypt->setMode($mode); - - foreach ($texts as $text) { - $encrypted = $crypt->encrypt($text); - expect($crypt->decrypt($encrypted))->equals($text); - } - } - } - } - ); - } - - /** - * Tests the encryption base 64 - * - * @author Nikolaos Dimopoulos - * @since 2014-10-17 - */ - public function testCryptEncryptBase64() - { - $this->specify( - "encryption base 64does not return correct results", - function () { - $crypt = new Crypt(); - - $crypt->setPadding(Crypt::PADDING_ANSI_X_923); - - $key = substr('phalcon notice 13123123', 0, 16); - $expected = 'https://github.com/phalcon/cphalcon/issues?state=open'; - - $encrypted = $crypt->encryptBase64($expected, substr($key, 0, 16)); - expect($crypt->decryptBase64($encrypted, $key))->equals($expected); - - $encrypted = $crypt->encryptBase64($expected, $key, true); - expect($crypt->decryptBase64($encrypted, $key, true))->equals($expected); - } - ); - } -}