Advertisement
niksoft

Crypto Class

Aug 3rd, 2011
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.00 KB | None | 0 0
  1. class Crypto
  2. {
  3.     private $salt, $data, $iv, $mode, $method, $key;
  4.  
  5.     // Class constructor
  6.     function __construct($data=false, $salt=false, $method=false, $iv=false, $mode=false, $key=false) {
  7.         $this->setData($data);
  8.         $this->setSalt($salt);
  9.         $this->setIv($iv);
  10.         $this->setMode($mode);
  11.         $this->setMethod($method);
  12.         $this->setKey($key);
  13.     }
  14.  
  15.     function __toString() { return $this->data; }
  16.     public function getIvSize() { return mcrypt_get_iv_size(constant($this->method), constant($this->mode)); }
  17.     public function setIv($iv=false) { $this->iv=$iv; }
  18.     public function getIv() { return $this->iv; }
  19.     public function getData() { return $this->data; }
  20.     public function setSalt($salt=false) { $this->salt=$salt; }
  21.     public function setData($data=false) { $this->data=$data; }
  22.     public function setKey($key=false) { $this->key=$key; }
  23.  
  24.     /*This will set the method to a supported method by the mcrypt or hash
  25.     * library, agnostic to which one you "plan" on using
  26.     * Please try to use RIJNDAEL_256 as the encryption method (AES256)
  27.     */
  28.     function setMethod($method=false) {
  29.         if(!$method) { throw new Exception('An empty method could not be set'); }
  30.         $method=strtoupper($method);
  31.         //check if this is a hash method
  32.         foreach(hash_algos() as $algo) {
  33.             if($method === strtoupper($algo)) {
  34.                 $this->method=$method;
  35.                 return true;
  36.             }
  37.         }
  38.         //if not then check if it's a crypto method
  39.         foreach(mcrypt_list_algorithms() as $supportedMethod) {
  40.             $supportedMethod = strtoupper(str_replace("-","_",$supportedMethod)); //unfortunately the naming conventions seem to be, juuust a little bit different
  41.             if($method === $supportedMethod) {
  42.                 $this->method="MCRYPT_".$method;
  43.                 return true;
  44.             }
  45.         }
  46.         throw new Exception("The cryptographic method \"{$method}\" is not supported by the system");
  47.     }
  48.  
  49.     //Set the crypto mode, this is used for en/decryption, generally you'd want to use the ECB mode
  50.     function setMode($mode=false) {
  51.         if(!$mode) { throw new Exception('An empty crypto mode could not be set'); }
  52.         $mode = strtoupper($mode);
  53.         foreach(mcrypt_list_modes() as $supportedMode) {
  54.             if($mode === strtoupper($supportedMode)) {
  55.                 $this->mode="MCRYPT_MODE_".$mode;
  56.                 return true;
  57.             }
  58.         }
  59.         throw new Exception("The cryptographic mode \"{$mode}\" is not supported by the system");
  60.     }
  61.    
  62.     function hash() {
  63.         if(!$this->data) { throw new Exception('Hashing requires data to hash'); }
  64.         if(!$this->method || !preg_match('/^(MHASH)?/',$this->method)) { throw new Exception('Hashing requires a hashing algorithm to hash'); }
  65.         $this->data = (!$this->salt) ? hash($this->method, $this->data) : hash($this->method, $this->data.$this->salt);
  66.         return true;
  67.     }
  68.    
  69.     function encrypt($key=false) { 
  70.         if($key) { $this->setKey($key); }
  71.         if(!$this->data) { throw new Exception('Encryption requires data'); }
  72.         if(!$this->key) { throw new Exception('Encryption requires a key'); }
  73.         if(!$this->method || !preg_match('/^(MCRYPT)?/',$this->method)) { throw new Exception('Encryption requires an encryption algorithm'); }
  74.         if(!$this->mode) { throw new Exception('Encryption mode required'); }
  75.         //generate iv (note it is absolutely necessary to store the iv, valid iv is required at decryption time)
  76.         $this->iv = mcrypt_create_iv($this->getIvSize(), MCRYPT_DEV_URANDOM);
  77.         //Encrypt
  78.         return mcrypt_encrypt(constant($this->method), $this->key, trim($this->data), constant($this->mode), $this->iv);
  79.     }
  80.  
  81.     function decrypt($key=false) {
  82.         if($key) { $this->setKey($key); }
  83.         if(!$this->data) { throw new Exception('Decrypt requires data.'); }
  84.         if(!$this->key) { throw new Exception('Decryption requires a key'); }
  85.         if(!$this->iv) { throw new Exception('Decryption requires an iv'); }
  86.         if(!$this->method || !preg_match('/^(MCRYPT)?/',$this->method)) { throw new Exception('Decryption requires a decryption algorithm'); }
  87.         if(!$this->mode) { throw new Exception('Decryption mode required'); }
  88.         //Decrypt
  89.         return trim(mcrypt_decrypt(constant($this->method), $this->key, $this->data, constant($this->mode), $this->iv));
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement