Last active
December 29, 2015 00:09
-
-
Save egulhan/7583650 to your computer and use it in GitHub Desktop.
Hash (encode/decode) test by a key
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 | |
/** | |
* Cipher Class | |
* | |
* Ciphers text by user defined key. | |
* @date 2013-11-21 | |
* @require php5-mcrypt | |
* @http://www.php.net/manual/en/function.mcrypt-encrypt.php#78531 | |
* @http://stackoverflow.com/questions/1289061/best-way-to-use-php-to-encrypt-and-decrypt-passwords | |
*/ | |
class Cipher | |
{ | |
private $securekey, $iv; | |
private $defaultTextKey='myPrivKey!'; | |
function __construct($textkey=null) | |
{ | |
if(isset($textkey)) | |
$textkey=$this->defaultTextKey; | |
$this->securekey=hash('sha256', $textkey, TRUE); | |
//$this->iv=mcrypt_create_iv(32); // make slow script to generating it | |
$this->iv=mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC),MCRYPT_DEV_URANDOM); | |
} | |
function encrypt($input) | |
{ | |
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->securekey, $input, MCRYPT_MODE_ECB, $this->iv)); | |
} | |
function decrypt($input) | |
{ | |
return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this->securekey, base64_decode($input), MCRYPT_MODE_ECB, $this->iv)); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment