Advertisement
widana

Crypt

Nov 14th, 2018
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.96 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Sts\Account\Helpers;
  4.  
  5.  
  6. use Exception;
  7. /**
  8.  * Crypt PHP
  9.  *
  10.  * Provides cryptography functionality, including hashing and symmetric-key encryption
  11.  *
  12.  * @package    Crypt
  13.  * @author     Osman Üngür <osmanungur@gmail.com>
  14.  * @copyright  2010-2011 Osman Üngür
  15.  * @license    http://www.opensource.org/licenses/bsd-license.php BSD License
  16.  * @version    Version @package_version@
  17.  * @since      Class available since Version 1.0.0
  18.  * @link       http://github.com/osmanungur/crypt-php
  19.  */
  20.  
  21. class Crypt {
  22.  
  23.     private $data;
  24.     private $key;
  25.     private $module;
  26.     private $complexTypes = false;
  27.     const HMAC_ALGORITHM = 'sha1';
  28.     const DELIMITER = '#';
  29.     const MCRYPT_MODULE = 'rijndael-192';
  30.     const MCRYPT_MOD = 'cfb';
  31.     const PREFIX = 'Crypt';
  32.     const MINIMUM_KEY_LENGTH = 8;
  33.  
  34.     function __construct() {
  35.         $this->checkEnvironment();
  36.         $this->setModule(@mcrypt_module_open(self::MCRYPT_MODULE, '', self::MCRYPT_MOD, ''));
  37.     }
  38.  
  39.     /**
  40.      * Checks the environment for mcrypt and mcrypt module
  41.      *
  42.      * @return void
  43.      * @author Osman Üngür
  44.      */
  45.     private function checkEnvironment() {
  46.         if ((!extension_loaded('mcrypt')) || (!function_exists('mcrypt_module_open'))) {
  47.             throw new Exception('The PHP mcrypt extension must be installed for encryption', 1);
  48.         }
  49.         if (!in_array(self::MCRYPT_MODULE, @mcrypt_list_algorithms())) {
  50.             throw new Exception("The cipher used self::MCRYPT_MODULE does not appear to be supported by the installed version of libmcrypt", 1);
  51.         }
  52.     }
  53.  
  54.     /**
  55.      * Sets the data for encryption or decryption
  56.      *
  57.      * @param mixed $data
  58.      * @return void
  59.      * @author Osman Üngür
  60.      */
  61.     public function setData($data) {
  62.         $this->data = $data;
  63.     }
  64.  
  65.     /**
  66.      * Sets the secret key for encryption or decryption, at least 8 character long
  67.      *
  68.      * @param string $key
  69.      * @return void
  70.      * @author Osman Üngür
  71.      */
  72.     public function setKey($key) {
  73.         if (strlen($key) < self::MINIMUM_KEY_LENGTH) {
  74.             $message = sprintf('The secret key must be a minimum %s character long', self::MINIMUM_KEY_LENGTH);
  75.             throw new Exception($message, 1);
  76.         }
  77.         $this->key = $key;
  78.     }
  79.  
  80.     /**
  81.      * Sets the mcrypt module
  82.      *
  83.      * @param resource $module
  84.      * @return void
  85.      * @author Osman Üngür
  86.      */
  87.     private function setModule($module) {
  88.         $this->module = $module;
  89.     }
  90.  
  91.     /**
  92.      * Sets using complex data types like arrays and objects for serialization
  93.      *
  94.      * @param bool $complexTypes
  95.      * @return void
  96.      * @author Osman Üngür
  97.      */
  98.     public function setComplexTypes($complexTypes) {
  99.         $this->complexTypes = $complexTypes;
  100.     }
  101.  
  102.     /**
  103.      * Returns the encrypted or decrypted data
  104.      *
  105.      * @return mixed
  106.      * @author Osman Üngür
  107.      */
  108.     private function getData() {
  109.         return $this->data;
  110.     }
  111.  
  112.     /**
  113.      * Returns the secret key for encryption
  114.      *
  115.      * @return string
  116.      * @author Osman Üngür
  117.      */
  118.     private function getKey() {
  119.         return $this->key;
  120.     }
  121.  
  122.     /**
  123.      * Returns the mcrypt module resource
  124.      *
  125.      * @return resource
  126.      * @author Osman Üngür
  127.      */
  128.     private function getModule() {
  129.         return $this->module;
  130.     }
  131.  
  132.     /**
  133.      * Returns true if using complex data types like arrays and objects declared
  134.      *
  135.      * @return bool
  136.      * @author Osman Üngür
  137.      */
  138.     private function getComplexTypes() {
  139.         return $this->complexTypes;
  140.     }
  141.  
  142.     /**
  143.      * Encrypts the given data using symmetric-key encryption
  144.      *
  145.      * @return string
  146.      * @author Osman Üngür
  147.      */
  148.     public function encrypt() {
  149.         mt_srand();
  150.         $init_vector = @mcrypt_create_iv(@mcrypt_enc_get_iv_size($this->getModule()), MCRYPT_RAND);
  151.         $key = substr(sha1($this->getKey()), 0, @mcrypt_enc_get_key_size($this->getModule()));
  152.         @mcrypt_generic_init($this->getModule(), $key, $init_vector);
  153.         if ($this->getComplexTypes()) {
  154.             $this->setData(serialize($this->getData()));
  155.         }
  156.         $cipher = @mcrypt_generic($this->getModule(), $this->getData());
  157.         $hmac = hash_hmac(self::HMAC_ALGORITHM, $init_vector . self::DELIMITER . $cipher, $this->getKey());
  158.         $encoded_init_vector = base64_encode($init_vector);
  159.         $encoded_cipher = base64_encode($cipher);
  160.         return self::PREFIX . self::DELIMITER . $encoded_init_vector . self::DELIMITER . $encoded_cipher . self::DELIMITER . $hmac;
  161.     }
  162.  
  163.     /**
  164.      * Decrypts encrypted cipher using symmetric-key encryption
  165.      *
  166.      * @return mixedl
  167.      * @author Osman Üngür
  168.      */
  169.     public function decrypt() {
  170.         $elements = explode(self::DELIMITER, $this->getData());
  171.         if (count($elements) != 4 || $elements[0] != self::PREFIX) {
  172.             $message = sprintf('The given data does not appear to be encrypted with %s', __CLASS__);
  173.             throw new Exception($message, 1);
  174.         }
  175.         $init_vector = base64_decode($elements[1]);
  176.         $cipher = base64_decode($elements[2]);
  177.         $given_hmac = $elements[3];
  178.         $hmac = hash_hmac(self::HMAC_ALGORITHM, $init_vector . self::DELIMITER . $cipher, $this->getKey());
  179.         if ($given_hmac != $hmac) {
  180.             throw new Exception('The given data appears tampered or corrupted', 1);
  181.         }
  182.         $key = substr(sha1($this->getKey()), 0, @mcrypt_enc_get_key_size($this->getModule()));
  183.         @mcrypt_generic_init($this->getModule(), $key, $init_vector);
  184.         $result = @mdecrypt_generic($this->getModule(), $cipher);
  185.         if ($this->getComplexTypes()) {
  186.             return unserialize($result);
  187.         }
  188.         return $result;
  189.     }
  190.  
  191.     public function __destruct() {
  192.         @mcrypt_generic_deinit($this->getModule());
  193.         @mcrypt_module_close($this->getModule());
  194.     }
  195.  
  196. }
  197.  
  198. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement