Last active
May 4, 2016 11:17
-
-
Save jan-krueger/137a70417d72f2fee09e to your computer and use it in GitHub Desktop.
Simple encryption class.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace SweetCode\Cipher; | |
class Cipher { | |
private $secureKey; | |
private $cryptCipher = MCRYPT_RIJNDAEL_256; | |
private $cryptMode = MCRYPT_MODE_ECB; | |
private $vectorSize; | |
public function __construct($passphrase, $hash = 'SHA256') { | |
$this->secureKey = hash($hash, $passphrase, true); | |
$this->vectorSize = mcrypt_get_iv_size($this->cryptCipher, $this->cryptMode); | |
} | |
/** | |
* encrypts the given value | |
* | |
* @param string $value | |
* @return string | |
*/ | |
public function encrypt($value) { | |
return (base64_encode( | |
mcrypt_encrypt( | |
$this->cryptCipher, | |
$this->secureKey, | |
$value, | |
$this->cryptMode, | |
mcrypt_create_iv( | |
$this->vectorSize, | |
MCRYPT_RAND | |
) | |
))); | |
} | |
/** | |
* decrypts the given encrypted value | |
* | |
* @param string $value | |
* @return string | |
*/ | |
public function decrypt($value) { | |
return (mcrypt_decrypt( | |
$this->cryptCipher, | |
$this->secureKey, | |
base64_decode($value), | |
$this->cryptMode, | |
mcrypt_create_iv( | |
$this->vectorSize, | |
MCRYPT_RAND | |
))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment