Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Crypto
- {
- private $salt, $data, $iv, $mode, $method, $key;
- // Class constructor
- function __construct($data=false, $salt=false, $method=false, $iv=false, $mode=false, $key=false) {
- $this->setData($data);
- $this->setSalt($salt);
- $this->setIv($iv);
- $this->setMode($mode);
- $this->setMethod($method);
- $this->setKey($key);
- }
- function __toString() { return $this->data; }
- public function getIvSize() { return mcrypt_get_iv_size(constant($this->method), constant($this->mode)); }
- public function setIv($iv=false) { $this->iv=$iv; }
- public function getIv() { return $this->iv; }
- public function getData() { return $this->data; }
- public function setSalt($salt=false) { $this->salt=$salt; }
- public function setData($data=false) { $this->data=$data; }
- public function setKey($key=false) { $this->key=$key; }
- /*This will set the method to a supported method by the mcrypt or hash
- * library, agnostic to which one you "plan" on using
- * Please try to use RIJNDAEL_256 as the encryption method (AES256)
- */
- function setMethod($method=false) {
- if(!$method) { throw new Exception('An empty method could not be set'); }
- $method=strtoupper($method);
- //check if this is a hash method
- foreach(hash_algos() as $algo) {
- if($method === strtoupper($algo)) {
- $this->method=$method;
- return true;
- }
- }
- //if not then check if it's a crypto method
- foreach(mcrypt_list_algorithms() as $supportedMethod) {
- $supportedMethod = strtoupper(str_replace("-","_",$supportedMethod)); //unfortunately the naming conventions seem to be, juuust a little bit different
- if($method === $supportedMethod) {
- $this->method="MCRYPT_".$method;
- return true;
- }
- }
- throw new Exception("The cryptographic method \"{$method}\" is not supported by the system");
- }
- //Set the crypto mode, this is used for en/decryption, generally you'd want to use the ECB mode
- function setMode($mode=false) {
- if(!$mode) { throw new Exception('An empty crypto mode could not be set'); }
- $mode = strtoupper($mode);
- foreach(mcrypt_list_modes() as $supportedMode) {
- if($mode === strtoupper($supportedMode)) {
- $this->mode="MCRYPT_MODE_".$mode;
- return true;
- }
- }
- throw new Exception("The cryptographic mode \"{$mode}\" is not supported by the system");
- }
- function hash() {
- if(!$this->data) { throw new Exception('Hashing requires data to hash'); }
- if(!$this->method || !preg_match('/^(MHASH)?/',$this->method)) { throw new Exception('Hashing requires a hashing algorithm to hash'); }
- $this->data = (!$this->salt) ? hash($this->method, $this->data) : hash($this->method, $this->data.$this->salt);
- return true;
- }
- function encrypt($key=false) {
- if($key) { $this->setKey($key); }
- if(!$this->data) { throw new Exception('Encryption requires data'); }
- if(!$this->key) { throw new Exception('Encryption requires a key'); }
- if(!$this->method || !preg_match('/^(MCRYPT)?/',$this->method)) { throw new Exception('Encryption requires an encryption algorithm'); }
- if(!$this->mode) { throw new Exception('Encryption mode required'); }
- //generate iv (note it is absolutely necessary to store the iv, valid iv is required at decryption time)
- $this->iv = mcrypt_create_iv($this->getIvSize(), MCRYPT_DEV_URANDOM);
- //Encrypt
- return mcrypt_encrypt(constant($this->method), $this->key, trim($this->data), constant($this->mode), $this->iv);
- }
- function decrypt($key=false) {
- if($key) { $this->setKey($key); }
- if(!$this->data) { throw new Exception('Decrypt requires data.'); }
- if(!$this->key) { throw new Exception('Decryption requires a key'); }
- if(!$this->iv) { throw new Exception('Decryption requires an iv'); }
- if(!$this->method || !preg_match('/^(MCRYPT)?/',$this->method)) { throw new Exception('Decryption requires a decryption algorithm'); }
- if(!$this->mode) { throw new Exception('Decryption mode required'); }
- //Decrypt
- return trim(mcrypt_decrypt(constant($this->method), $this->key, $this->data, constant($this->mode), $this->iv));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement